SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    echo value from POST

    I'm populating a pulldown from the db. After the form is submitted, I would like for this pulldown to remember the user's selection - in case I have to print an error.

    PHP Code:
    function states() {
        echo 
    '<select name="state" size="1">'."\n";
        
    $sql mysql_query("SELECT st_id,st_name FROM {$database}.menu_states") or die(sql_error('Could not retrieve states'));
        while(
    $row mysql_fetch_assoc($sql)) {
            echo 
    '<option value="'.$row['st_id'].'">'.$row['st_name'].'</option>'."\n";
        }
        echo 
    "</select>\n";

    How would I do that?

  2. #2
    SitePoint Zealot t0WL337's Avatar
    Join Date
    Mar 2007
    Location
    Ottawa, Ontario!
    Posts
    149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    print_r($_POST);

    This thread may be of help to you.
    http://www.sitepoint.com/forums/showthread.php?t=481247
    <3php && SitePoint ?>

  3. #3
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by t0WL337 View Post
    print_r($_POST);

    This thread may be of help to you.
    http://www.sitepoint.com/forums/showthread.php?t=481247
    Maybe I just don't understand. How will that help me?

    I may not have been clear enough in my original post.
    1. User selects Florida for the states pulldown
    2. User submits form that is missing a required value
    3. Form is displayed back to the user, with error message and any submitted data. Florida is automatically selected in the states pulldown.
    4. Now, user only has to fill in the missing fields and re-submit form.

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    You need to set up an if(isset($_POST['selectname']))...
    have a look at http://www.sitepoint.com/forums/showthread.php?t=483627
    It should be what you need
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by spikeZ View Post
    You need to set up an if(isset($_POST['selectname']))...
    have a look at http://www.sitepoint.com/forums/showthread.php?t=483627
    It should be what you need
    If I do that, then every <option> comes back selected="selected", because the select menu was POSTed.

    I think I need a way to compare what was POSTed to what's in the menu. I just can't figure out how.

  6. #6
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    well if you look further into the thread that thought was covered.
    If you have your loop coming from a database in a while loop you would nee something like:
    PHP Code:
    while($rows=mysql_fetch_assoc($query)) {
    if(
    $row['st_id'] == $_POST['st_id']) {
         echo 
    '<option value="'.$row['st_id'].'" selected>'.$row['st_name'].'</option>'."\n";
    } else {
        echo 
    '<option value="'.$row['st_id'].'">'.$row['st_name'].'</option>'."\n";

    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  7. #7
    SitePoint Wizard tgavin's Avatar
    Join Date
    Feb 2003
    Location
    FL
    Posts
    1,051
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I looked through the thread but didn't see it. Guess I didn't look hard enough.

    Got it working with a minor adjustment
    PHP Code:
    if($row['st_id'] == $_POST['state']) { 
    thanks

  8. #8
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,807
    Mentioned
    158 Post(s)
    Tagged
    3 Thread(s)
    GLad you got it up and running
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....


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
  •