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 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Arrow keeping selection in drop down box after submit form

    Hi
    I have a form which validates on the same page with error messages coming up if information has been inputed incorrectly. The problem is that if radio and dropdown selection has been chosen but an error message comes up for another field the selection the dropdown selection and radio box check does not keep its value. so far i have this

    Code:
    <option value ="$_POST[exercise]"></option>
    <option selected></option>
    <select name ="exercise">
    <select name ="exercise">
    PHP Code:
    <?php
    $options 
    = array('',0,2,3,4,5,6,7,;
    foreach (
    $options as $option)
    {
    echo 
    '<option value="'.$option.'"';
    }
    if (isset(
    $_POST['exercise'])&&$_POST['exercise']==$option) echo ' selected';
    {
    echo 
    '>'.$option.'</option>';
    }
    ?>
    </select> hours
    <font size="2" color="red"><?php print "$exmessage" ?></font><p>
    this keeps the selection but only prints the last character in the drop down list - so you can only see number 8.
    or
    I have this
    Code:
    <option value=" " <?php (isset($_POST[exercise]) && $_POST[exercise] == ' ')? ' selected ':null ?>> </option>
    <option value="0" <?php (isset($_POST[exercise]) && $_POST[exercise] == '0')? ' selected ':null ?>>0</option>
    <option value="1" <?php (isset($_POST[exercise]) && $_POST[exercise] == '1')? ' selected ':null ?>>1</option>
    <option value="2" <?php (isset($_POST[exercise]) && $_POST[exercise] == '2')? ' selected ':null ?>>2</option>
    <option value="3" <?php (isset($_POST[exercise]) && $_POST[exercise] == '3')? ' selected ':null ?>>3</option>
    <option value="4" <?php (isset($_POST[exercise]) && $_POST[exercise] == '4')? ' selected ':null ?>>4</option>
    <option value="5" <?php (isset($_POST[exercise]) && $_POST[exercise] == '5')? ' selected ':null ?>>5</option>
    <option value="6" <?php (isset($_POST[exercise]) && $_POST[exercise] == '6')? ' selected ':null ?>>6</option>
    <option value="7" <?php (isset($_POST[exercise]) && $_POST[exercise] == '7')? ' selected ':null ?>>7</option>
    <option value="8" <?php (isset($_POST[exercise]) && $_POST[exercise] == '8')? ' selected ':null ?>>8</option>
    which doesn't keep the selection selected and does not seem to bring up the error message when nothing selected.

    does anyone know what im doing wrong? i know its probably a simple-ish problem but i have been stuck for days on this so am really hoping someone will reply many thanks

  • #2
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Try something like this:
    PHP Code:
    $options = array('',0,2,3,4,5,6,7,8);
    foreach (
    $options as $option)
    {
        echo 
    '<option value="' $option '"' . (isset($_POST['exercise']) && $_POST['exercise'] == $option ' selected' '') . '>' $option '</option>';


  • #3
    New to the CF scene
    Join Date
    Nov 2007
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool

    Thanks Inigoesdr
    That works wonders i was trying with ternary operators for a while but didn't quite get them right.

    is there a chance you could check this out for me? its a gender radio box and it works fine by keeping the radio checked when checking validation but i am trying to make an if saying if female then do one thing else.. blah. but when i print $gender it stays at male no matter what i do! i was thinking of using the (isset($_POST['gender']) in the below code but can't seem to get the syntax right. sorry i am a real newbie but with this massive learning curve i will be a master by next month

    <input type="radio" name="gender" value="male" <?=($gender=='male')? 'checked' : '' ?> >male
    <input type="radio" name="gender" value="female" <?=($gender=='female')? 'checked' : '' ?> >female

  • #4
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,638
    Thanks
    2
    Thanked 404 Times in 396 Posts
    Quote Originally Posted by noobyphpchick View Post
    <input type="radio" name="gender" value="male" <?=($gender=='male')? 'checked' : '' ?> >male
    <input type="radio" name="gender" value="female" <?=($gender=='female')? 'checked' : '' ?> >female
    When you have short tags on and you use that <?= syntax that tells PHP to echo the command that follows it, but if you want that code to work properly you need to wrap the rest of the commands in parenthesis:
    PHP Code:
    <input type="radio" name="gender" value="male" <?=($gender == 'male' 'checked' ''); ?> >male
    <input type="radio" name="gender" value="female" <?=($gender == 'female' 'checked' ''); ?> >female
    Keep in mind though, that if you intend to distribute this code to servers you don't control, some people don't have short tags on.


  •  

    Posting Permissions

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