I have a HTML/PHP/MYSQL setup where I want to use a value chosen from one HTML <SELECT> to restrict values shown in the next HTML <SELECT>. It's a reasonably common requirement but I just can't get it. A good example is where the user chooses a country and then the next choice is restricted to valid states for that country and then when they choose a state the next choice is only valid cities for that state.

I can do the database queries Ok, but its knowing exactly what to query for that bugs me.

<td valign="middle">
        <select name="ttypec" id="ttypec" onchange="return getTypeCPtr();\">
            <?php  typec_list('con','no');   ?>
    <td valign="middle">
        <select name="tcomp">
            <?php   comp_list('con','no',$TCPtr);   ?>
// javascript
function getTypeCPtr()
    var sel = document.getElementById("ttypec").value;

    window.alert("value chosen is -" + sel + "--");
//    document.getElementById("ttypecret").innerHTML = sel;

    return sel;

In this bit of code "getTypeCPtr()" is a javascript routine that can correctly identify the choice made in the select statement that is assembled by the PHP code "typec_list()"
What I want to do is get that choice (by number or string) and use it in the php routine "comp_list()" to restrict that choices shown in that list.

As you can see, the javascript has a return statement, but how can I get that value in the HTML/PHP code to pass it to the next bit of code.

Maybe this isn't the best way to do what I want, so I would welcome comments on that as well. Any help would be greatly appreciated.


7 Years
Discussion Span
Last Post by McLaren

You can send the value to PHP that returns getTypeCPtr using AJAX, then with PHP gererate something and echo back to your page. I suggest learning jQuery AJAX. Actually for me teh simplest way to use AJAX is to use jquer post method.


$.post("test.php", { name: "John", time: "2pm" },
   function(data) {
     alert("Data Loaded: " + data);

you make a php file that gets the information through $_POST - the information is name and time in this case. You the do what you want with it in php. And then use

echo 'something';

thats what you echoed will be variable data and in this case it will be alerted.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.