Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Drop down default option

    Hi

    I have coded the following which creates me a database driven drop down menu, however i want to be able have a default option, which is automatically selected based on the value of a varaible.

    Any help would be great.

    <?php

    $query = "select scriptname from script_summary";
    $result = mysql_query ($query) or die (mysql_error());
    echo "<td><b>Please Select A SCRIPT:</b></td> <td><SELECT name=\"scriptname\">";
    if (mysql_num_rows($result)>0)

    {
    while($row=mysql_fetch_array($result))

    {
    echo "<option value=\"$row[scriptname]\">$row[scriptname]</option>";
    }
    }
    echo "</SELECT></td>";
    ?>

    Thanks in advance.

  • #2
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    echo '<option value="' $row['scriptname'] . '"';
        if(
    $row['scriptname'] == $myvar){
            echo 
    ' selected';
        }
    echo 
    '>' $row['scriptname'] . '</option>'

  • #3
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for that works a treat.

    i dont suppose you know how to populate a second drop down list based on the first one do you?

  • #4
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Create A second query that depends on the value of the submitted select box value

    Here is an example
    http://degs.cybertechhelp.com/select.php

  • #5
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Do i need to submit the the selection from the first drop down, and then populate the second one. Cause i wouldnt want to submit anything until the rest of my form was filled in.

  • #6
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts
    If you don't want to submit it then you would need to use javascript

    http://degs.cybertechhelp.com/mx_select.php

    You would need to output all of your queries into a javascript array that you can then use to populate the drop downs.

  • #7
    Kal
    Kal is offline
    Regular Coder
    Join Date
    Dec 2005
    Posts
    309
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Right okay cheers, im ok at doing it that way, did it before, just thought it would be good if i could do it all with PHP.

    thanks anyway

  • #8
    Senior Coder
    Join Date
    Nov 2002
    Location
    North-East, UK
    Posts
    1,265
    Thanks
    0
    Thanked 0 Times in 0 Posts


  •  

    Posting Permissions

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