Skip to content Skip to sidebar Skip to footer

Populate Drop Down Menus Through PHP From 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, 'userdet

Solution 1:

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.

<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

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

Post a Comment for "Populate Drop Down Menus Through PHP From MySql"