Results 1 to 5 of 5

Thread: <select> option selected onload

  1. #1
    Join Date
    May 2008
    Posts
    144
    Thanks
    6
    Thanked 11 Times in 11 Posts

    Default <select> option selected onload

    im having a bit of trouble working out a problem... i have a PHP form with a drop down. if the form is called to add a new record into the database, the default select option is set. if the form is called to edit a pre-existing record, then the selected option depends on what is already in the record. whats the easiest, cleanest way to do this?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    If your page is outputting the HTML via PHP, have PHP add the selected attribute to the item that should be selected. Example output HTML:

    Code:
    <select name="whatever">
    <option value="whatever">something</option>
    <option value="whatever">another</option>
    <option value="whatever">yet another</option>
    <option value="whatever" selected>still more</option>
    </select>
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. The Following User Says Thank You to jscheuer1 For This Useful Post:

    thenajsays (10-18-2009)

  4. #3
    Join Date
    May 2008
    Posts
    144
    Thanks
    6
    Thanked 11 Times in 11 Posts

    Default

    that doesnt really make sense to me? heres my script:
    <?php

    $action = $_REQUEST['action'];
    //$id = $_REQUEST['id'];
    echo "<h2>Edit Listing Type</h2>\n";
    if ($action == 'delete')
    {
    $id = $_REQUEST['id'];
    $query = "DELETE FROM listtype WHERE id = $id";
    $result = mysql_query($query) or die(mysql_error());
    if ($result)
    {
    echo "You have successfully deleted that listing type.<br>\n";
    echo "<a href=\"index.php?content=types\">Return Listing Types</a>\n";
    exit;
    } else
    {
    echo "Sorry, there was a problem deleting that listing type.<br>\n";
    echo "<a href=\"index.php?content=types\">Return Listing Types</a>\n";
    exit;
    }
    }
    else if($action == 'add')
    {
    $type = $_POST['type'];
    $query = "INSERT INTO `listtype` (`type`) VALUES ('$type')";
    $result = mysql_query($query) or die(mysql_error());
    if ($result)
    {
    echo "You have successfully added '$type.'<br>\n";
    echo "<a href=\"index.php?content=types\">Return Listing Types</a>\n";
    exit;
    } else
    {
    echo "Sorry, there was a problem adding '$type.'<br>\n";
    echo "<a href=\"index.php?content=types\">Return Listing Types</a>\n";
    exit;
    }
    }
    else if($action == 'update')
    {
    $id = $_REQUEST['id'];
    $type = $_POST['type'];
    $query = "UPDATE listtype SET type='$type' WHERE id = $id";
    $result = mysql_query($query) or die(mysql_error());
    if ($result)
    {
    echo "You have successfully updated '$type.'<br>\n";
    echo "<a href=\"index.php?content=types\">Return Listing Types</a>\n";
    exit;
    } else
    {
    echo "Sorry, there was a problem updating '$type.'<br>\n";
    echo "<a href=\"index.php?content=types\">Return Listing Types</a>\n";
    exit;
    }
    }
    ?>

  5. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    No, I meant the PHP which outputs the select element and its options. But I did say (emphasis added):

    If your page is outputting the HTML via PHP, have PHP add the selected attribute to the item that should be selected.
    So either you have other PHP code that does this (other than that in your posts so far), or you don't. If not, you should consider using it, as it is more certain (users may turn off javascript) and more secure (users may easily hack your javascript).

    However, it doesn't sound like security is much of an issue here - for just selecting an option element in a select element - though it may be. Certainly though certainty may be an issue - otherwise why would you want to have this happen in the first place?

    That said, there are a number of ways to select an option onload using javascript, here's one:

    Code:
    window.onload = function(){
     var f = document.forms.formName, s = f.elements.selectName;
     s.options[optionNumber].selected = true;
    };
    The two highlighted names should be hard coded as the actual name of the form and the actual name of the select element on the page. The highlighted and red number should be replaced with a PHP token that will resolve the option number (numbered 0 to whatever) of the option you want to be selected.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  6. #5
    Join Date
    May 2008
    Posts
    144
    Thanks
    6
    Thanked 11 Times in 11 Posts

    Default

    thanks... completely obvious to me now! i was having a doorknob moment! sorry!

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
  •