Results 1 to 5 of 5

Thread: Chained dropdowns dynamically created PHP!!

  1. #1
    Join Date
    Jul 2010
    Location
    Manchester, UK
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Chained dropdowns dynamically created PHP!!

    Hi,

    I have created two dynamic drop down menus from ms sql server database using PHP. The code is the following:
    $sql="SELECT DISTINCT collector from dbo.missourifinal$ where long IS NOT NULL order by collector";

    PHP Code:
    $sql="SELECT DISTINCT collector from dbo.table where long IS NOT NULL order by collector";

    $result sqlsrv_query$conn$sql);


    echo 
    "<select id='collector' name='collector'>\n";    //create the drop down menu
    echo "\t<option value=\"SELECT COLLECTOR\" ";
    if (
    $_POST['collector']=="SELECT COLLECTOR") echo "selected=\"selected\"";
    echo 
    ">---SELECT COLLECTOR---</option>\n";
    while(
    $nt=sqlsrv_fetch_array($result)){//Array or records stored in $nt
    echo "\t<option value=\"{$nt['collector']}\"";
    if (
    $_POST['collector']==$nt['collector']) echo "selected=\"selected\"";
    echo 
    ">$nt[collector]</option>\n";   
    /* Option values are added by looping through the array */
    }
    echo 
    "</select>";// Closing of list box 
    and the other drop down is created exactly the same way.

    So the question now is:
    How do I make them dependant so that when I select an option in the first drop down then the values in the second will change??

    I have seen plenty examples on how to implement (with javascript and AJAX) this but none for something like the above..

    Thank you in advance
    Kat

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    I don't believe this is related to your MySQL or PHP code. This will be using Javascript. In order to create it using PHP/MySQL, you first need to find the way to do this using Javascript and the HTML output. Once you've done that, then just find the way to make PHP generate it automatically.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jul 2010
    Location
    Manchester, UK
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    djr33 thanx for the reply,

    Quote Originally Posted by djr33 View Post
    I don't believe this is related to your MySQL or PHP code. This will be using Javascript.
    Well, as I mentioned I know that. I just wanted to post it here first before 4warding it to a js forum.


    Quote Originally Posted by djr33 View Post
    In order to create it using PHP/MySQL, you first need to find the way to do this using Javascript and the HTML output. Once you've done that, then just find the way to make PHP generate it automatically.
    Ehm.. Obviously if I had figured this out, then I wouldn't have to ask for help.

    Thanx anyway

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Looks like you're farther than I thought. Sorry for not realizing that.

    My point is that the approach you should take is first creating the Javascript and only after that all works try to generate it dynamically with PHP. This means that the two processes are almost entirely unrelated and it will make it easier.

    The question you should post here (so that it isn't confusing) is: How do I make this PHP code [paste it] generate this Javascript code [paste it]?

    If you can paste the version you want to generate in addition to the PHP code, then it should be very easy to compare the two and figure out how to generate it. In fact, you may not even need to reply here for help if you can easily spot the difference. Remember to compare the PHP's output to the desired output and just modify the PHP step-by-step to get the desired output.

    How do I make them dependant so that when I select an option in the first drop down then the values in the second will change??
    The problem here is that it sounds like you have not yet figured out the Javascript aspect of this: PHP operates only when the page first loads, so it won't make them "dependent", just generate them in the first place.

    If you need help with the Javascript aspect, you'll probably need to post in that forum, but once you know what Javascript you'll need to generate, someone here can help with the PHP.

    I hope that gets you started.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jul 2010
    Location
    Manchester, UK
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default

    Hey djr33,

    I just posted this to the js forum 'cause I have no problem on the PHP part.

    Thanx for replying however!

    Kat

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •