Page 1 of 1

Lists To Maintain Values On Self Processing Page

Posted: Tue Dec 14, 2004 7:27 pm
by Subliminal
Hi,
I don't even know how to properly descrbe this. but here goes.

I have a page with a form on it including multiple selection capable lists. Theses lists values are being pulled from an sql database. Once submitted the page will self process and continue if error free yada yada yada... But if not we are back at square one..

how do i get the selected values from the list to stay when the page reloads when there was an entry error?

In text fields I just <?php echo $_POST['value']; ?> in the value attribute.

Thanks a million,

Posted: Tue Dec 14, 2004 7:36 pm
by timvw
<option value='foo[]' SELECTED>sbar</option>

now it is up to you to write code that tests if the selected thing was there, and if yes, then echo SELECTED...

Posted: Wed Dec 15, 2004 3:47 am
by AGISB
The problem is browser dependant. Some keep the value of the initial selection even though you might assign a new value with the selected statement. You can also set the cache-control on the form site but it does not entirely fix the problem.
I normally use a verification page and don't use php-self for the call. In this case you can simply display the correct option by using selected.

Posted: Wed Dec 15, 2004 6:01 am
by Subliminal
Thanks guys.. I am just a little confused as to how though? For every menu item I put an if statement to search the array for that value? similar to the field example?

This is the code I use currently... works maybe there is a better way to do it and have what i am trying to do integrate a bit better?

Code: Select all

<select name="menu[]" size="7" multiple id="menu">
    <?php
    // Get Menu Values From Database
    $menu_query = mysql_query("SELECT * FROM menus WHERE menu='MENUa'");
	 
    while ($row = mysql_fetch_array($menu_query))
	    { 
             echo "<option value='{$row["value"]}'>{$row["item"]}</option>\n"; 
            }
     ?>
</select>
THANKS

Posted: Wed Dec 15, 2004 8:30 am
by timvw
i think code would look like:

Code: Select all

echo "<select name='{$column['name']}'>";

 foreach($column['options'] as $option)
{
  $selected = '';
   if (in_array($option['value'] == $POST[$column['name']]))
   {
      $selected = ' selected';
   }
echo "<option value={$option['value']}{$selected}>{$option['name']}</option>";
}
echo "</select>";