通过PHP从MySql填充下拉菜单 [英] Populate Drop Down Menus Through PHP From MySql

查看:275
本文介绍了通过PHP从MySql填充下拉菜单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮助我。

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

我有一个包含多个表的MySql数据库。我的问题是围绕使用用户详细信息,检测器和搜索头这三个中的数据。

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

我想做的是填充一个HTML表单上的下拉菜单,其中显示了特定于用户的检测器,而另一个下拉菜单又是特定于用户的,但仅显示了适用于第一个选择中的检测器的搜索头

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.

这三个表都具有 userid字段,检测器和搜索头表均具有 detectorid字段。

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.

非常感谢和问候

Chris。

更新了JS代码

<script type="text/javascript" language="javascript">
            $(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");     
            }); 
            }); 
        </script> 

更新的PHP代码

<?php     
$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 .= "<option value=\"detector" . $data["id"] . "\">" . $data["text"] . 
"</option>";     }     
echo $return_results; ?>


推荐答案

这比复杂的MySQL查询更多的是Javascript。

This is more Javascript than complex MySQL queries.

form.php

此页面是您的表单所在的位置。我创建了一个Ajax函数,可以获取必要的数据。我还没有完成所有事情,但我向您概述了您需要完成的工作。从 select 输入收集的数据通过jQuery中的ajax post 请求进行传输。然后将数据处理并格式化为html数据,可以通过 select 输入来理解。

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.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script> 
<script>
$(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");
    });
});
</script>
<form>
    <label>Detectors</label>
    <select name="detectors">

    </select>
    <label>Search Heads</label>
    <select name="searchead">

    </select>
</form>

url / to / ajax.php

在这里您将不得不调整数据的处理方式。您可以使用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.

<?php
    $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 .= "<option value=\"" . $data["id"] . "\">" . $data["text"] . "</option>";
    }
    echo $return_results;
?>

这篇关于通过PHP从MySql填充下拉菜单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆