FRIHOSTFORUMSSEARCHFAQTOSBLOGSCOMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP Dropdown Selection





moejoe
Hey,

On my website it has a form with a drop down box. This form also has a PHP error/incomplete checker on the form. However. When the error checker is run, the DD box is reset to the first default value.

Is there a way to recall the Drop Down box's position using PHP (or even JS).

Of course I don't want the form just to be that value.
Example:
Code:

<?php
$DD = $_REQUEST['DD'];

if(empty($name)) {
   echo '<select name="DD">
         <option value='.$DD.'>'.$DD.'</option>
</select>';
?>
 

Crappy Example with probably heaps of error, you hopefully get my point...

This would return only the 1 value. Whereas i want the DD box to return all the values, But the Selected Value is the one posted.

Thanks For All your help.
mathiaus
It would be easier if you posted the form Wink but something like this should work.

Code:
<?php
// array for the different values for the dropdown
$values = array('hello', 'bye', 'pie');

echo "<select name=\"what\">";
// loop throuh values and create html output
foreach($values as $key => $value) {
   if($_POST['what'] == $key)
      // Print as selected!
      echo "<option value=\"$key\" selected=\"selected\">$value</option>";
    else
      // Print normally
      echo "<option value=\"$key\">$value</option>";
}

echo "</select>";

?>


This is my preferd method you could also do the below but its messy.
Code:
<select name="what">
<option value="1"<?php echo ($_POST['what']==1) ? ' selected="selected"' : ''; ?>>hello</option>
<option value="2"<?php echo ($_POST['what']==2) ? ' selected="selected"' : ''; ?>>bye</option>
<option value="3"<?php echo ($_POST['what']==3) ? ' selected="selected"' : ''; ?>>pie</option>
</select>
moejoe
Yeah, I had thought about your second method.
However these drop down boxes has about 140+ values...
hexkid
moejoe wrote:
Yeah, I had thought about your second method.
However these drop down boxes has about 140+ values...


Code:
<?php
function expand($n) {
  if ($n < 100) return $n . ' is less than 100';
  else return $n . ' is 100 or larger';
}
?>
<form action="" method="post">
<select name="what[]" multiple="multiple">
<?php
$revpost = array_flip($_POST['what']);
foreach (range(0, 140) as $ndx) {
  echo '<option value="', $ndx, '"';
  if (isset($revpost[$ndx])) echo ' selected="selected"';
  echo '>', expand($ndx), '</option>';
}
?>
</select>
<input type="submit">
</form>
moejoe
Hi,
Thanks for taking the time to help me.

Could you explain how that last code works?

Could you also put that code into an example with 2-3 countries.
Thanks!
hexkid
moejoe wrote:
Could you explain how that last code works?


The "secret" is the array_flip() call. The $_POST array has the checked countries, but they're in the value part of the array. I find it easier to work with the index part, so after the array_flip() they're switched.


moejoe wrote:
Could you also put that code into an example with 2-3 countries.

Add a $countries array to the code and change the for loop:
Code:
$countries = array('USA', 'Canada', 'England', 'Australia');

// ...

foreach ($countries as $ndx=>$country_name) {
  echo '<option value="', $ndx, '"';
  if (isset($revpost[$ndx])) echo ' selected="selected"';
  echo '>', $country_name, '</option>';
}
Related topics
http://tuvanonline.com/library/index.php
PHP-Nuke platium?
PHP Write to line
need some help - php switch code
dropdown menus howto?
php dropdown lists with if statements or switch
HALFMODS the site for Natural Selection, and Half Life Mods
great asp and PHP programming software
how to put what you have query in a drop down list?
I could use some help with dropdown lists in php
PHP inside HTML tags?
selecçao de emails no php . email selection on php
PHP/MySQL Dropdown value, and insert into database
Help displaying pdfs using PHP
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.