Jump to content

<select> question...


tefuzz

Recommended Posts

I have a form. It has 4 different <select> in it. If the lists are all valid (an item is selected) but there are other errors, how would I make PHP keep the selection when the page refreshes to show the fields with errors.  Right now my <select> are borught in by a function like this:

 

function getDays ($name, $tabIndex) {
echo ("\n\n<select name=\"$name\" id=\"$name\" tabindex=\"$tabIndex\">\n  <option selected=\"day\" value=\"\">");
 $days = array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
				"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
				"21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
				"31");
 foreach ($days as $day) {
	 echo("\n\t<option value=\"$day\">$day</option>");
 }
 echo ("\n</select>");
}

Link to comment
Share on other sites

This should work, but may not depending on your particular situation.

 

function getDays ($name, $tabIndex) {
echo ("\n\n<select name=\"$name\" id=\"$name\" tabindex=\"$tabIndex\">\n  <option selected=\"day\" value=\"\">");
 $days = array ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
				"11", "12", "13", "14", "15", "16", "17", "18", "19", "20",
				"21", "22", "23", "24", "25", "26", "27", "28", "29", "30",
				"31");

if ($_POST[$name])
$extra = 'selected';

 foreach ($days as $day) {
	 echo("\n\t<option value=\"$day\" $extra>$day</option>");
 }
 echo ("\n</select>");
}

Link to comment
Share on other sites

function getDays ($name, $tabIndex) {
    echo "\n\n"."<select name='$name' id='$name' tabindex='$tabIndex'>";
    for( $i = 1; $i <= 31; $i++ )
    {
        echo sprintf( "\n\t".'<option %svalue="%s">%s</option>',
            isset( $_GET[$name] ) && $_GET[$name] == $i ? 'selected="selected" ' : '', 
            $i, 
            $i );
    }
    echo "\n</select>";
}

 

I guess, going by http://www.w3schools.com/TAGS/att_option_selected.asp

Link to comment
Share on other sites

You can do this with only a couple lines.

 

Define your array at the start of the script and make a function:

function makeSelect($arrOptions,$sel,$name) {
  echo '<select name="'.$name.'">';
  foreach ($arrElement as $e) {
    echo '<option value="'.$e.'"'.($e==$sel ? ' selected' : '').'>'.$e.'</option>';
  }
  echo '</select>';
}

 

Or if you want to use keys:

function makeSelectWithKey($arrOptions,$sel,$name) {
  echo '<select name="'.$name.'">';
  foreach ($arrElement as $k => $e) {
    echo '<option value="'.$k.'"'.($k==$sel ? ' selected' : '').'>'.$e.'</option>';
  }
  echo '</select>';
}

 

$arrOptions: This is your array containg all your data

$sel: This is the option selected by the user

$name: This is the name of the select

 

$userdays=$_POST['days'];
makeSelect($days,$userdays,'days');

Link to comment
Share on other sites

Mmmh range() .. didn't know that one :)

btw, in the solution I posted if you're going to use it for differents months you may want to make the the hardcoded 31 an optional parameter, so:

function getDays ( $name, $tabIndex, $totDays = 31 /*<- Optional param, defaults to 31 if ommited*/) {
    echo "\n\n"."<select name='$name' id='$name' tabindex='$tabIndex'>";
    for( $i = 1; $i <= $totDays /*<- There it is!*/; $i++ )
    {
        echo sprintf( "\n\t".'<option %svalue="%s">%s</option>',
            isset( $_GET[$name] ) && $_GET[$name] == $i ? 'selected="selected" ' : '', 
            $i, 
            $i );
    }
    echo "\n</select>";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.