DotDragnet
May 24, 2012, 05:28:24 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Mobile users - Our forum is Tapatalk enabled. http://www.tapatalk.com/
 
   Home   Help Search Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: retail values in form php  (Read 281 times)
SLEE
Hero Member
*****
Posts: 824



View Profile WWW Awards
« on: July 14, 2011, 04:28:03 PM »

I want to retain the values of a form so they user doesnt have to keep filling them out but I have some select boxes that have  a lot of values is there a way to loop thorugh them with the value posted and then select the one that is the same?
Logged

SLEE - still the most confusing ddner...

Follow me on Twitter
JasonD
Global Moderator
Hero Member
*****
Posts: 551



View Profile Awards
« Reply #1 on: July 14, 2011, 05:49:55 PM »

<select name="foo">
<option value="bar" <?php if ($_POST['foo'] == 'bar') { echo 'selected="selected"'; } ?>>Bar</option>
...
</select>
Logged
SLEE
Hero Member
*****
Posts: 824



View Profile WWW Awards
« Reply #2 on: July 15, 2011, 07:45:12 AM »

thanks Jason, so I would need to put that in every option is there any other quicker way?
Logged

SLEE - still the most confusing ddner...

Follow me on Twitter
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #3 on: July 15, 2011, 07:47:30 AM »

I usually use an array. Put the value and label in that and then loop through with a foreach and just compare the key to the posted item. useful then for also data/security checking so you can check if the item submitted is in the array too using in_array().
Logged

SLEE
Hero Member
*****
Posts: 824



View Profile WWW Awards
« Reply #4 on: July 15, 2011, 07:52:49 AM »

thanks Sarah thatw bas quick! Do you have an example you can show me?
Logged

SLEE - still the most confusing ddner...

Follow me on Twitter
sarahA
DDN Contribs
Hero Member
*****
Posts: 2184



View Profile WWW Awards
« Reply #5 on: July 15, 2011, 08:33:33 AM »

Code:
<select id="selectlist" name="selectlist">
<?php
$somearray 
= array("option1" => "label1""option2" => "label2"etc);
foreach (
$somearray AS $key => $value) :
    echo 
"<option value='".$key."'".($key == $_POST['selectlist'] ? ' selected="selected"' '').">".$value."</option>\n";
endforeach;
?>

Then if you put the array at the top of the page (or in a config file if it's used on multiple pages), you could, in your form processing/checks do

Code:
if (!empty($_POST['selectlist']) && in_array($_POST['selectlist'], $somearray)) :
// okay to proceed
endif;

Am sure there is probably a better way to do all of this but it cuts down large chunks of html and unnecessary repeats with long select lists (anything over 3 options I tend to use the above method).
Logged

SLEE
Hero Member
*****
Posts: 824



View Profile WWW Awards
« Reply #6 on: July 15, 2011, 08:34:15 AM »

thanks Sarah smile
Logged

SLEE - still the most confusing ddner...

Follow me on Twitter
Pages: [1]   Go Up
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF | SMF © 2006-2008, Simple Machines Valid XHTML 1.0! Valid CSS!