NEWBIE: sticky select from fields

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
I have a form, with some error checking, that works fine after "submit"
inserting into DB ---but I have a little problem, I cannot seem to make
my "select" fields sticky --I have tried several different solutions
and nothing works. The solution below works for errors (not selecting
from the pulldown) and for inserting into DB ...but does not work if
error checking stops the porcessing ...I want it to remain on the last
selection for user convenience ...but it doesn't!

<select name="session">
    <option value="" <? echo ($session=='')  ? "" : ""; ?>>SELECT
    <option value="1" <? echo ($session=='1')  ? "selected=" : "";
?>>SESSION 1</option>></select>

<select name="employment_status">
    <option value="" <? echo ($session=='')  ? "selected" : ""; ?>>SELECT
    <option value="1" <? echo ($employment_status=='1')  ? "selected" :
""; ?>>STATUS 1</option>></select>

Re: NEWBIE: sticky select from fields

Quoted text here. Click to load it

Something based on this should work:

    <title>Test form</title>
    <form method="post">
      <select name="session">

$options = array ('1' => 'Session 1', '2' => 'Session 2', '3' => 'Session 3');

foreach ($options as $value => $name)
  if (isset($_POST['session']) && $_POST['session'] == $value)
    echo "        <option value=\"\" selected=\"1\"></option>\n";
    echo "        <option value=\"\"></option>\n";

      <input type="submit" name="submit">

Does that help?

Re: NEWBIE: sticky select from fields

Hi Zoe

I copied & pasted your script
into t.php and added action="t.php"
to the form tag.

It worked fine using linux/apache/php
from the server itself (using firefox
browser) as well as from another machine
running XP+IE6

What problems are you having with your
'real' script. Are your other fields
'sticky' and just the <select>'s not ?

I am however used to simply doing
<option value="2" SELECTED> instead of selected=1
but I don't know if this has anything
to do with anything :)

Etienne Marais
Cosmic Link
South Africa

Site Timeline