SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast archive's Avatar
    Join Date
    Mar 2007
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    default selection in drop-down, based on dynamic data

    Hi people!

    Well, I have this user registration form. Once submit is pressed (before sending the data to the db) The user is presented with the data he has entered so as to verify with a second glance.

    If he is happy, he presses register, and guess what(!), he is registered in the db.
    But if he wants to correct any of the data provided, he is sent back to the registration form (along with his data that are sent back through hidden fields).
    Now, most of the data are found in drop down lists like city, education etc.

    What i want is the value in each of the dd lists to have as default value the selection the user has already made. I don't really seem to figure out how would this work with the 'selected' attribute of the dd lists.

    Any idea??
    Thank you all.

  2. #2
    Keep it simple, stupid! bokehman's Avatar
    Join Date
    Jul 2005
    Posts
    1,935
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    post your form

  3. #3
    SitePoint Enthusiast archive's Avatar
    Join Date
    Mar 2007
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok so the following is just a couple of dd-fields. it is impossible to post the whole form, as it is encoded in greek and i don't have the time to translate all of it.


    <form method="post" onsubmit="return validate_form(this)" action="verify.php">

    echo 'Gender: <SELECT name="gender">
    <OPTION VALUE="male">Male</OPTION>
    <OPTION VALUE="female">Female</OPTION>
    </SELECT>
    <br>';
    echo 'marital status: <SELECT name="maritalstatus">
    <OPTION VALUE="single">single</OPTION>
    <OPTION VALUE="married">married</OPTION>
    <OPTION VALUE="divorced">divorced</OPTION>
    </SELECT>
    <br>';

    <input type="submit" value="Submit">
    </form>

  4. #4
    SitePoint Wizard wonshikee's Avatar
    Join Date
    Jan 2007
    Posts
    1,223
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Since your not looping out your drop downs, you have to check it individually.

    But before that you shouldn't echo out those selects if they're not dynamically created, just wasting php's time.

    PHP Code:
    echo 'Gender: <SELECT name="gender">
    <OPTION VALUE="male">Male</OPTION>
    <OPTION VALUE="female">Female</OPTION>
    </SELECT> 
    Could simply be:
    HTML Code:
    ?>
    Gender: 
    <SELECT name="gender">
    <OPTION VALUE="male">Male</OPTION>
    <OPTION VALUE="female">Female</OPTION>
    </SELECT>
    <?php
    Anyway, here's an example of how to "select" value, assuming your bringing the selected data via $_POST

    HTML Code:
    Gender: 
    <SELECT name="gender">
    <OPTION VALUE="male" <?php if ($_POST['gender'] == 'male') echo "selected='selected'" ?>>Male</OPTION>
    <OPTION VALUE="female" <?php if ($_POST['gender'] == 'female') echo "selected='selected'" ?>>Female</OPTION>
    </SELECT>

  5. #5
    SitePoint Enthusiast archive's Avatar
    Join Date
    Mar 2007
    Posts
    89
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes yes yes!!!!!

    Aaarg, i have to start using my brains.....

    Thank you sooo much wonshikee!


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
  •