Forum Moderators: coopster

Message Too Old, No Replies

Redisplaying select box values

         

moroose

1:10 am on Jun 12, 2007 (gmt 0)

10+ Year Member



I tried different variations of redispalying submitted select values to no avail.Can anybody tell me what's the best option to achieve this?

<select name="options">
<option value=$option1 SELECTED>option1</option>
<option value=$option2 SELECTED>option2</option>
<option value=$option3 SELECTED>option3</option>
</select>

I even tried a code posted by Hakre back in 2005,but it gave me a parse error:

Parse error: parse error, unexpected T_STRING, expecting ')' in ...

Hakre's way:
<form action="mypage.php">
<select name="selbox" size="1">
<option value="">Select one</option>
<? foreach(array(1,2,3,4) AS $value) {?>
<option value="<?=$value?>"<?=($selbox==$value)?' selected="selected"':''?>><?=$value?></option>
<? }?>
</select>
</form>

My sincere gratitude in advance!

IndiaMaster

4:09 am on Jun 12, 2007 (gmt 0)

10+ Year Member



Hi Moroose,

I have developed a small piece of code for redisplaying the submitted values of a select list. If you want to use multiple select values to be displayed then you have to use nested loop. (As far as I think. Can anyone have a better idea?)

My code is as follows:

<form name="f1" action="thispage.php" method="post">
<?php
if(!isset($_POST['submit']))
{
?>
<select name="options" size="5">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
</select>
<?php
}
else
{
?>
<select name="options" multiple size="5">
<?php
for($value=1;$value<=10;$value++)
{
if($_POST['options']==$value)
{
echo("<option value=".$value." selected>".$value."</option>");
}
else
{
echo("<option value=".$value.">".$value."</option>");
}
}
?>
</select>
<?php
}
?>
<br/>
<input type="submit" value="Submit Form" name="submit">
</form>

moroose

3:48 pm on Jun 13, 2007 (gmt 0)

10+ Year Member



Thank you IndiaMaster for your suggested code.
For some reason,my browser complained when i modified that code to my page,dont know the reason.the action is not set to the same page,but rather to a second page that redisplays,not sure this could be the reason

jatar_k

3:54 pm on Jun 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



what did it say?

moroose

4:52 pm on Jun 13, 2007 (gmt 0)

10+ Year Member



browser stopped rendering the page,halted :)

"Pain never really goes away; you just elevate and get used to it by growing stronger“

Said by someone else.

jatar_k

9:07 pm on Jun 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



sounds like there was a parse error that it didn't show you

do you have error reporting turned up?

do you have display_errors set to on?

moroose

10:13 pm on Jun 13, 2007 (gmt 0)

10+ Year Member



Actually,it is turned off
I'll try it with error reporting on and see how it turns out

moroose

11:23 pm on Jun 13, 2007 (gmt 0)

10+ Year Member



i figured out why the browser chockes at it.
the script is number-specific and when changing numbers to words the browser chockes.

Habtom

5:03 am on Jun 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



>> the script is number-specific and when changing numbers to words the browser chockes.

Is that so? Your for loop might be getting into an infinite loop. That seems to me the only reason why the browser could do so related to your code.

Habtom

moroose

5:18 am on Jun 14, 2007 (gmt 0)

10+ Year Member



obviously am not good at php
would be great if someone modify the code to get it work with words.Yes,the browser gets in an infinite loop,trying to compute non-math stuff.

Habtom

6:11 am on Jun 14, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What does your code look like now? Can you post it?

Hab