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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题