简体   繁体   English

onchange select做不同的mysql查询

[英]onchange select do different mysql query

As title says I need help with onchange. 标题说我需要onchange的帮助。 I have select tag,and I need to do different mysql query when I choose something from select list. 我有select标签,当我从选择列表中选择一些东西时,我需要做不同的mysql查询。 Example: 例:

<select name="list">
<option>cars</option>
<option>busses</option>
<option>trucks</option>
</select>

and then when i select cars it do this 然后当我选择cars它会这样做

$query="select * from table where type='cars'";

and if I choose trucks it do 如果我选择trucks

$query="select * from table where type='trucks'";

and so on... 等等...
then I need to display the result in div under the list example 然后我需要在列表示例下显示div中的结果

<select name="list">
<option>cars</option>
<option>busses</option>
<option>trucks</option>
</select>
<div> this is where I need to display results from query</div> 

Please help!!! 请帮忙!!!

If you want to change the result by ajax : 如果要通过ajax更改结果:

HTML : HTML:

 <select id="list" name="list">

    <option>cars</option>

    <option>busses</option>

    <option>trucks</option>
    </select>

in JS File: 在JS文件中:

$(document).ready(function() {
  $('#list').change(function() {
var selected=$(this).val();
  $.get("change_query.php?selected="+selected, function(data){
      $('.result').html(data);

    });
    });
});

and you should create change_query.php file and your query and code in it and return the result in it 并且您应该在其中创建change_query.php文件以及您的查询和代码并将结果返回到其中

$type = $_POST["selected"];

$query="select * from table where type='".$type."'";

print result here .... ; 在这里打印结果....;

  • Tell me if you need any help ,I just guided you in Jquery not all code 告诉我,如果你需要任何帮助,我只是引导你进入Jquery而不是所有代码

You should add your SELECT code to a form with method attribute set to post , and also a submit button of course. 您应该将SELECT代码添加到method属性设置为post的表单,当然还有提交按钮。 Then , php will get the value of those INPUTS and do whatever you want. 然后,php将获得那些INPUTS的值,并做任何你想做的事情。

   $term = $_POST['list'];

/*
In order to prevent unwanted queries or injection add an array with those terms and
check if the posted value is in this array:

*/
    $terms = array("cars" , "trucks");
    if(!array_key_exists($term , $terms))
     die(); //bad bad bad boy.

    $query = "select * from table where type='$term'";

you could use 你可以用

onchange="this.form.submit();"

as well. 同样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM