Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rating: Thread Rating: 5 votes, 3.40 average.
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-07-2007, 04:36 PM   PM User | #1
noobyphpchick
New to the CF scene

 
Join Date: Nov 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
noobyphpchick is an unknown quantity at this point
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
noobyphpchick is offline   Reply With Quote
Old 11-07-2007, 05:32 PM   PM User | #2
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
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>';

Inigoesdr is offline   Reply With Quote
Old 11-08-2007, 12:29 AM   PM User | #3
noobyphpchick
New to the CF scene

 
Join Date: Nov 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
noobyphpchick is an unknown quantity at this point
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
noobyphpchick is offline   Reply With Quote
Old 11-08-2007, 04:02 AM   PM User | #4
Inigoesdr
Super Moderator


 
Inigoesdr's Avatar
 
Join Date: Mar 2007
Location: Florida, USA
Posts: 3,601
Thanks: 2
Thanked 397 Times in 390 Posts
Inigoesdr is a jewel in the roughInigoesdr is a jewel in the roughInigoesdr is a jewel in the rough
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.
Inigoesdr is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 08:04 PM.


Advertisement
Log in to turn off these ads.