Forum Moderators: coopster

Message Too Old, No Replies

Make drop down retain selected values after failed validation

         

humanexperience

2:53 am on May 5, 2010 (gmt 0)

10+ Year Member



1. I would like to get my gender drop down to retain selected values after submit button has been clicked.

2. I would also like to create a validation script for it using php not javascript.


This is how I got my gender drop down to retain selected values after signup button was clicked and something else on my form failed validation
<tr>
<td>I am</td>
<td>
<select name="sex" class="select" id="sex" >
<option value="0" <?php if (isset($_POST['sex']) && $_POST['sex'] == 0) {
echo 'selected="selected"';} ?> >-Gender-</option>
<option value="1" <?php if (isset($_POST['sex']) && $_POST['sex'] == 1) {
echo 'selected="selected"';} ?>>Male</option>
<option value="2" <?php if (isset($_POST['sex']) && $_POST['sex'] == 2) {
echo 'selected="selected"';} ?>>Female</option>
</select>
</td></tr>


I would like to do the same with my birthday selection drop down and here is the code on my homepage:

 <tr>
<td>Birthday</td>
<td>
<?php
require_once('includes/bdayselect/bdayselect.php');
$myCalendar = new tc_calendar("dob", true);
// $myCalendar->setIcon("images/iconCalendar.gif");
$myCalendar->setDate(01, 03, 1900);
$myCalendar->setPath("./");
$myCalendar->setYearInterval(1900, 2010);
$myCalendar->dateAllow('1900-01-01', '2010-12-31');
//$myCalendar->setHeight(350);
//$myCalendar->autoSubmit(true, "form1");
$myCalendar->writeScript();
?>


</td>


and here is the code on my birthday script page:

//write the select box of days
function writeDay(){
echo("<select name=\"".$this->objname."_day\" id=\"".$this->objname."_day\" onChange=\"javascript:tc_setDay('".$this->objname."', this[this.selectedIndex].value, '$this->path');\" class=\"tcday\">");
echo("<option value=\"00\">Day:</option>");
for($i=1; $i<=31; $i++){
$selected = ((int)$this->day == $i) ? " selected" : "";
echo("<option value=\"".str_pad($i, 2 , "0", STR_PAD_LEFT)."\"$selected>$i</option>");
}
echo("</select> ");
}

//write the select box of months
function writeMonth(){
echo("<select name=\"".$this->objname."_month\" id=\"".$this->objname."_month\" onChange=\"javascript:tc_setMonth('".$this->objname."', this[this.selectedIndex].value, '$this->path');\" class=\"tcmonth\">");
echo("<option value=\"00\">Month:</option>");

$monthnames = $this->getMonthNames();
for($i=1; $i<=sizeof($monthnames); $i++){
$selected = ((int)$this->month == $i) ? " selected" : "";
echo("<option value=\"".str_pad($i, 2, "0", STR_PAD_LEFT)."\"$selected>".$monthnames[$i-1]."</option>");
}
echo("</select> ");
}

//write the year textbox
function writeYear(){
//echo("<input type=\"textbox\" name=\"".$this->objname."_year\" id=\"".$this->objname."_year\" value=\"$this->year\" maxlength=4 size=5 onBlur=\"javascript:tc_setYear('".$this->objname."', this.value, '$this->path');\" onKeyPress=\"javascript:if(yearEnter(event)){ tc_setYear('".$this->objname."', this.value, '$this->path'); return false; }\"> ");
echo("<select name=\"".$this->objname."_year\" id=\"".$this->objname."_year\" onChange=\"javascript:tc_setYear('".$this->objname."', this[this.selectedIndex].value, '$this->path');\" class=\"tcyear\">");
echo("<option value=\"0000\">Year:</option>");


I would appreciate all help given.
Thanks in advance.

jatar_k

1:45 pm on May 5, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



it looks for the values in these lines

$selected = ((int)$this->day == $i) ? " selected" : "";
$selected = ((int)$this->month == $i) ? " selected" : "";

though in the year function I think you missed it in your paste as the closing select is also missing

so you need to get your value into
$this->day
$this->month
and probably $this->year

then they will properly display selected values

humanexperience

3:00 pm on May 5, 2010 (gmt 0)

10+ Year Member



How would I enter this?

Also when I added the closing select it causes a fault.
Everything is working fine as date correctly inserts into my database.

humanexperience

3:34 pm on May 5, 2010 (gmt 0)

10+ Year Member



I actually meant I want to get my date drop down to retain selected values not gender. I have already dealt with the gender drop down. Sorry if I confused any body.

Readie

4:00 pm on May 5, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Simple method, build the select menu values you want as an array, and loop through them, building the select menu as you go:
$values = array(
'a',
'b',
'c',
'd',
'e'
);

$count = count($values);
$sel = '<select name="whatever">' . "\n\t" . '<option value=""></option>';
for($i = 0; $i < $count; $i++) {
$se = (isset($_POST['whatever']) && $_POST['whatever'] === $values[$i])? ' selected' : '';
$sel .= "\n\t" . '<option value="' . $values[$i] . '"' . $se . '>' . $values[$i] . '</option>';
}
$sel .= "\n" . '</select>';
echo $sel;