Lists To Maintain Values On Self Processing Page

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Subliminal
Forum Commoner
Posts: 40
Joined: Thu Oct 23, 2003 8:12 pm

Lists To Maintain Values On Self Processing Page

Post 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,
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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...
AGISB
Forum Contributor
Posts: 422
Joined: Fri Jul 09, 2004 1:23 am

Post 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.
Subliminal
Forum Commoner
Posts: 40
Joined: Thu Oct 23, 2003 8:12 pm

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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>";
Post Reply