Click here to Skip to main content
15,905,028 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have three listboxes on a webpage. I would like to save the value from the first listbox so that I can populate the second. I can't chain the first to the second because of the problem of duplicates. Its a many to many relationship. How would I save the value temporarly so that I can use it to populate the second listbox. Thank you.


PHP
<?php
error_reporting(E_ALL);
ini_set("display_errors", 0);

include("conndb.php");

function createoptions($table , $id , $field , $condition_field , $value)
{
    $sql = sprintf("select * from $table WHERE $condition_field=%id ORDER BY $field" , $value);
    $res = mysql_query($sql) or die(mysql_error());
    if (mysql_num_rows($res) > 0) {
        while ($a = mysql_fetch_assoc($res))
        $out[] = "{optionValue: {$a[$id]}, optionDisplay: '$a[$field]'}";
        return "[" . implode("," , $out) . "]";
    } else

        return "[{optionValue: -1 , optionDisplay: 'No result'}]";
}

if (isset($_GET['make'])) {
    echo createoptions("model" , "model_id" , "model" , "make_id" , $_GET['make']);
}


die();


This is the other page...

PHP
<?Php
include("conndb.php");
function createoptions($table , $id , $field)
{
    $sql = "select * from $table ORDER BY $field";
    $res = mysql_query($sql) or die(mysql_error());
    while ($a = mysql_fetch_assoc($res))
    echo "<option value=\"{$a[$id]}\">$a[$field]</option>";
}

?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
    <head>
        <meta http-equiv="Content-type" content="text/html; charset=utf-8">
        <title>Select test</title>
            <script type="text/javascript" src="jquery-1.2.3.js"></script>
            <script type="text/javascript" charset="utf-8">
            $(function(){
              $("select#make").change(function(){
                $.getJSON("select.php",{make: $(this).val(), ajax: 'true'}, function(j){
                  var options = '';
                  for (var i = 0; i < j.length; i++) {
                    options += '<option value="' + j[i].optionValue + '">' + j[i].optionDisplay + '</option>';
                  }
                  $("select#model").html(options);
                })
              })


            })
            </script>
    </head>

        <select id="make">
        <option value="-1">--Select--</option>
		<?php
        createoptions("make", "make_id", "make");
        ?>
        </select>
        <select id="model">
        </select>

    </body>
</html>


And I am using jquery-1.2.3.js
Posted
Updated 8-Jan-13 14:31pm
v4

1 solution

I would make these selections with AJAX, no-one wants to have their whole page update to select a list, in this day and age. Which means your AJAX call sends the id that is the root for the data in the second list, and there's no need to store it anywhere beyond that.
 
Share this answer
 
Comments
Member 7766180 8-Jan-13 19:19pm    
Sounds good! I am coming from an MS Access background, so my thinking isn't up to speed yet! Are there any examples out there that I can start with? Thank you.
Christian Graus 8-Jan-13 19:20pm    
Why are you using PHP ? Either way, jquery is what you want to use, there's got to be tons of examples for what you want to do out there.
Member 7766180 8-Jan-13 20:15pm    
Like I said. Lost, Lost, Lost. I have MySQL. I was told to use PHP, Ajax and JScript. Yes I have samples but none are totally working. The best one fills in the first listbox but refuses to update the second one. Is there something amiss in this update code? Cant seem to get the code to paste properly! See original question, its there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900