php下拉列表读取mysql数据_php – 从下拉列表中获取值以在MySQL查询中使用

1:创建PHP脚本以返回数据

基本上只是根据$_GET输入生成值.

2:在jquery中创建一个json请求

调用将返回数据的PHP文件,您将使用该数据向select中添加更多值.

//Step 1 - The posted ajax data that will return our json request.

if(isset($_GET['fetchrow'])) //Is our ajax request called on page load? If yes, go to this code block

{

//Other stuff like DB connection

$pesq = mysql_escape_string($_GET['fetchrow']); //Put our variable as the variable sent through ajax

$sql4 = ("SELECT DISTINCT new_name_freg FROM freguesias WHERE codg_cc = '$pesq'"); //Run our query

$result4 = mysql_query($sql4, $link); //Please change from mysql_* to mysqli

$data = array(); //The array in which the data is in

while($row = mysql_fetch_assoc($result4)) //Look through all rows

{

array_push($data, $row); //Put the data into the array

}

echo json_encode($data); //Send all the data to our ajax request in json format.

die; //Don't show any more of the page if ajax request.

}

?>

//Step #2:

//The jquery script calls ajax request on change of the first select

$( "#desig_act" ).change(function() {

$.getJSON('thisfilename.php', {fetchrow:$("#desig_act").val()}, function(data){ //Get the json data from the script above

var html = '';

var len = data.length;

for (var i = 0; i< len; i++) { //Loop through all results

html += '' + data[i].new_name_freg + ''; // Add data to string for each row

}

$('#otherselect').html(html); //Add data to the select.

});

});

Designação atual :

while ($row2 = mysql_fetch_assoc($result4)) {

echo "".$row2["new_name_freg"]."";

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值