php mysql 下拉菜单,通过PHP从MySql填充下拉菜单

I wonder whether someone may be able to help me please.

I have a MySql database containing several tables. My question revolves around the use of data from three of these, 'userdetails', 'detectors' and 'searchheads'.

What I would like to do is to populate a drop down menu on my HTML form that shows the 'detectors' that are user specific and, in turn, another drop down menu that again is user specific but also only shows the 'searchhead' applicable to the 'detector' selection made in the first drop down menu.

All three tables have a 'userid' field and the tables for the 'detectors' and 'search heads' have a 'detectorid' field.

Could someone perhaps please show me how I would go about setting this up because I must admit I haven't a clue where to start.

Many thanks and regards

Chris.

UPDATED JS CODE

$(document).ready(function(){

$.fetch_data = function (what) {

var send = {

"what" : what,

"data" : $("select[name=detectors]").val()

};

$.post ("dropdownmenus.php", send, function(data){

$("select[name=" + what + "]").html(data);

});

};

$.fetch_data ("detectors");

$("select[name=detectors]").live("change", function(){

// reset the searchhead as new data will be implemented

$("select[name=detectorsearchheads]").html("");

$.fetch_data ("detectorsearchhead");

});

});

UPDATED PHP CODE

$request = array (

"detector" => @$_POST["detector"],

"detector" => @$_POST["detector"] );

// clean $request.

$build_query = "select * from " . $request["what"] . " "

. "where userid = \"" . $user_id . "\"";

$build_results = mysql_query ($build_query);

$return_results = "";

foreach ($build_results as $index => $data) {

$return_results .= "" . $data["text"] .

""; }

echo $return_results; ?>

解决方案

This is more Javascript than complex MySQL queries.

form.php

This page is where your form is located. I have created an Ajax function which grabs necessary data. I haven't completed everything but I gave you an outline of what you need to complete. The data collected from the select inputs are transferred via an ajax post request in jQuery. The data is then manipulated and formatted into html data which can be understood by the select input.

$(document).ready(function(){

$.fetch_data = function (what) {

var send = {

"what" : what,

"data" : $("select[name=detectors]").val()

};

$.post ("url/to/ajax.php", send, functio(data){

$("select[name=" + what + "]").html(data);

});

};

$.fetch_data ("detectors");

$("select[name=detectors]").live("change", function(){

// reset the searchhead as new data will be implemented

$("select[name=searchhead]").html("");

$.fetch_data ("searchhead");

});

});

Detectors

Search Heads

url/to/ajax.php

Here you will have to make adjustments to how your data is manipulated. You could use a PHP switch depending on the what variable, which would change the query depending on what's requested.

$request = array (

"what" => @$_POST["what"],

"data" => @$_POST["data"]

);

// clean $request.

$build_query = "select * from " . $request["what"] . " "

. "where userid = \"" . $user_id . "\"";

$build_results = mysql_query ($build_query);

$return_results = "";

foreach ($build_results as $index => $data) {

$reurn_results .= "" . $data["text"] . "";

}

echo $return_results;

?>

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值