drol55885602 2017-06-03 05:07
浏览 80
已采纳

将选定的两个下拉值发送到PHP脚本,而不使用AJAX提交

I have two dropdown list, I want to know what user has selected in these dropdowns, based on that I need fetch data from DB using PHP. Please note that There is no submit button. I don't know how send javascript var value to PHP code.

Here it is what I've done so far

 <head>

<script src="http://code.jquery.com/jquery-latest.min.js"></script>

<script>

    $(document).ready(function(){   
        $('select.day').on('change',function(){     
        var select = $(this).val();         
        alert(select);          

        });

        $.ajax({

            Type : "POST",              
            url : "new.php",
            data : {name : select},     
            success : function(msg)
            {
                $('#d').text(msg);      
                alert(msg);
            }   
        }); 
    });

</script>
</head>
   <body>
<p id="d"></p>
<Select class="day" onchange="getTimeFrame(this.value)">
        <Option >Today</Option>
        <Option>This Week</Option>
    <Option>Last Week</Option> 
    </Select>

    <Select class="filter" onchange="getFilterType(this.value)">
        <Option >User</Option>
        <Option>Client</Option>
    <Option>Project</Option>
    </Select>
</body>
</html>

Here it is PHP file

<?php

    // $time = $_POST['select'];         
     $filter = $_POST['name'];     
     //echo "Hello from new";      
     echo $filter;

?>

But nothing seems working for me. Anybody know how to achieve this?

  • 写回答

2条回答 默认 最新

  • dongwanqiang_2017 2017-06-03 05:10
    关注

    Add your Ajax code inside the onchange event of the select .

    Updated

    typo change type instead Type ,is not caps .All are small letter.Javascript is case sensitive

    $('select.day').on('change', function() {
        var select = $(this).val();
      $.ajax({
        type: "POST",
        url: "new.php",
        data: {
          name: select
        },
        success: function(msg) {
          $('#d').text(msg);
          alert(msg);
        }
      });
      })
    

    Change Html as like this

    <Select class="day">
            <Option >Today</Option>
            <Option>This Week</Option>
        <Option>Last Week</Option> 
        </Select>
    

    For send both drop down value try this

    $('select.day').on('change', function() {
       var select =  $('select.day').map(function(){
        return $(this).val()
        }).get();
        console.log(select)
      $.ajax({
        type: "POST",
        url: "new.php",
        data: {
          name: select
        },
        success: function(msg) {
          $('#d').text(msg);
          alert(msg);
        }
      });
      })
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <Select class="day" >
            <Option >Today</Option>
            <Option>This Week</Option>
        <Option>Last Week</Option> 
        </Select>
    
        <Select class="day filter" >
            <Option >User</Option>
            <Option>Client</Option>
        <Option>Project</Option>
        </Select>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?