如何jQuery ajax多选

时间:2016-07-16 10:15:14

标签: php jquery mysql ajax

好的,我的表格是:

<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>

我的jQuery是:

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

我想要实现的是:当页面加载时,应显示所有学生。所以我的问题是:

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

如果选择了select中的任何选项,则只应在SELECT语句中更改该值。因此,例如,如果SELECT HOUSE中的任何内容发生更改,则新的SELECT语句将为:

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

此后SELECT LOCATION的任何内容都会发生变化,只有位置值应传递给现有的SELECT,并且上一次操作中选择了房屋的条件。

所以基本上我希望先前的条件保持不变,并在SELECT更改时传递新条件。

我希望我能够解释我的问题。我不知道如何创建jQuery / PHP查询。

感谢。

2 个答案:

答案 0 :(得分:0)

SELECT * FROM&#39; mytable&#39;在哪里&#39;学生&#39; =&#39; students.val()&#39; AND&#39; house&#39; =&#39; changedVALUE&#39; AND&#39; location&#39; =&#39; location.val()&#39;;

<强>已更新

在上面的查询中,您使用了表&amp; mWebview.setWebViewClient(new WebViewClient()()); mWebview.setWebChromeClient(new WebChromeClient()); 内的列名称。 正确的语法是:

''

Javascript 中,为了制作变量,请使用SELECT * FROM mytable WHERE students='students.val()' && house='changedVALUE' && location='location.val()'; 而不是var,因为它在PHP中使用。您的代码包含许多错误。< / p>

答案 1 :(得分:0)

你的jquery应该是这样的:

  $("#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);
    }
  });
});

然后你的selectdata.php应该是这样的

<?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;"
?>

修改

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();
  });

并在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'"