dongzecai0684 2016-07-16 10:15
浏览 86
已采纳

如何jQuery ajax多选

OK, my form is :

<form>

<select name="students" id="students">
<option value="0">Select Value First</option>
<option value="a">Value A</option>
<option value="b">Value B</option>
<option value="c">Value C</option>
</select>
<br /><br />
<select name="house" id="house">
<option value="0">Select Value Second</option>
<option value="a">Value A</option>
<option value="b">Value B</option>
<option value="c">Value C</option>
</select>
<br /><br />
<select name="location" id="location">
<option value="0">Select Value Third</option>
<option value="a">Value A</option>
<option value="b">Value B</option>
<option value="c">Value C</option>
</select>
<br /><br />

</form>

My jQuery is:

$(document).on("change","select",function() {
$val1 = $(this).val();
$val2 = $(this).attr('name');
    $.get("selectdata.php",{"name":$val2,"id":$val1},function($data){
            $("#result").html($data);
        });
});

What I want to achieve is: when the page loads, all students should be displayed. So my query is:

SELECT * 
FROM 'mytable' 
WHERE 'students' = 'students.val()' AND 'house'='house.val()' 
  AND 'location'='location.val()';

When any option from select is selected, only that value should be changed in the SELECT statement. So for example, if anything from SELECT HOUSE is changed, the new SELECT statement will be:

SELECT * 
FROM 'mytable' 
WHERE 'students' = 'students.val()' 
  AND 'house' = 'changedVALUE' 
  AND 'location' = 'location.val()';`

After this is anything from the SELECT LOCATION is changed, only the location value should be passed to the existing SELECT with condition for house selected in previous operation.

So basically I want the previous condition to be intact and pass the new condition on SELECT change.

I hope I am able to explain my issue. I am not sure how to create the jQuery / PHP query.

Thanks.

  • 写回答

2条回答 默认 最新

  • dqwnxdhb88531 2016-07-16 10:37
    关注

    Your jquery should be like this:

      $("#students, #house, #location").on("change", function(){
      var students = $("#students").val();
      var house = $("#house").val();  
      var location = $("#location").val();
      $.ajax({ 
        type: "POST", 
        url: 'selectdata.php',
        data : { 'students': students, 'house':house, 'location':location },
        success: function(data){
          $("#result").html(data);
        }
      });
    });
    

    Then your selectdata.php should be like this

    <?php 
       $students = $_POST['students'];
       $house = $_POST['house'];
       $location = $_POST['location'];
    
       //sql query
       $sql = "SELECT * FROM 'mytable' WHERE 'students'= $students AND 'house'= $house AND 'location'= $location;"
    ?>
    

    Edit

    jquery:

    function getData(){
      var students = $("#students").val();
      var house = $("#house").val();  
      var location = $("#location").val();
      $.ajax({ 
        type: "POST", 
        url: 'selectdata.php',
        data : { 'students': students, 'house':house, 'location':location },
        success: function(data){
          $("#result").html(data);
        }
      });
      }
    
      $( document ).ready(function() {
        getData();
      });      
    
    
      $("#students, #house, #location").on("change", function(){
          getData();
      });
    

    And in selectdata.php

    <?php 
       $students = $_POST['students'];
       $house = $_POST['house'];
       $location = $_POST['location'];
    
       $conditions = array(); //initialize array for conditions in the where clause
       if($students != 0){ //if value is not equal to 0, add to array.
           $conditions[] = "students = " . $students;
       }
       if($house != 0){ //if value is not equal to 0, add to array.
           $conditions[] = "house = " . $house;
       }
       if($location!= 0){ //if value is not equal to 0, add to array.
           $conditions[] = "location = " . $location;
       }
    
       $where = "";
       if(count($conditions) > 0){ //if array is not empty
          $where = "WHERE " . implode(" AND ", $conditions); // sample output: "WHERE students = 1 AND house = 2 AND location = 3"
       }
    
       //now your sql
       $sql = "SELECT * FROM 'mytable' " . $where; // "SELECT * FROM 'mytable' WHERE students = 1 AND house = 2 AND location = 3"
       // if the $condition array is empty, the sql should look like this: "SELECT * from 'mytable'"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能