Refilling drop down menu selection after form submission validation - php

I'm working on a sign up/registration form in php that resubmits/retains the users input if everything doesn't validate properly. I've got text box, password input, and radio buttons all working but these drop down menus have been more trouble. The php code I used works for the text boxes but not these select/options, is there a better way to do this? I've cut out the majority of the options just to save space, but each goes from 0-11 months, 1-31 days, and 1900-2013 years respectively.
<select id="month" name="month" value="<?php
if(isset($_POST['month']))
echo htmlspecialchars($_POST['month'])?>">
<option value="default">Month</option>
<option value="0">January</option>
...
<option value="11">December</option>
</select>
<select id="formDay" name="day" value="<?php
if(isset($_POST['day']))
echo htmlspecialchars($_POST['day'])?>">
<option value="default">Day</option>
<option value="1">1</option>
...
<option value="31">31</option>
</select>
<select id="formYear" name="year" value="<?php
if(isset($_POST['year']))
echo htmlspecialchars($_POST['year'])?>">
<option value="default">Year</option>
<option value="2013">2013</option>
...
<option value="1900">1900</option>
</select>

You may try this, generate values dynamically
Day:
echo "<select name='day'>";
for( $i = 1; $i <= 31; $i++ )
{
$selectedDay = isset($_POST['day']) && $_POST['day'] == $i ? 'selected="selected"' : '';
echo "<option $selectedDay value=$i>$i</option>";
}
echo "</select>";
Month:
$months = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
echo "<select name='month'>";
for( $i = 0; $i <= 11; $i++ )
{
$m = $months[$i];
$selectedMonth = isset($_POST['month']) && $_POST['month'] == $i ? 'selected="selected"' : '';
echo "<option $selectedMonth value=$i>$m</option>";
}
echo "</select>";
Year:
echo "<select name='year'>";
for( $i = 2013; $i >= 1900; $i-- )
{
$selectedYear = isset($_POST['year']) && $_POST['year'] == $y ? 'selected="selected"' : '';
echo "<option $selectedYear value=$i>$i</option>";
}
echo "</select>";
Demo Normal and Demo Selected

You are doing in the wrong way.
please make the condition inside the each option like below
<option value="2013" <?php if(isset($_POST['year']) && $_POST['year']==2013){ echo "selected";}?>>2013</option>
in same manner for month.

With selects you can't set a value ... instead you have to add a selected attribute to the selection option element.
I usually use a function like this to build out my selects
function showSelect($name, $options, $selected, $attr = array()){
$str = "<select name='".$name.'"';
foreach($attr as $name=>$val){
$str.= " ".$name."='".$val."'";
}
$str.=">";
foreach($options as $k=>$val){
$str.= "<option value='".$val."'".($val==$selected?" selected='selected'":"").">".$k.'</option>';
}
$str.="</select>";
}
$name is the name of the element
$options is an array in the form "option_value"=>"option_label"
$selected is the value of the selection option
$attr is an array of the additional attributes to put on the select element (style id etc.)
For example
$days = array();
for($d = 1; $x<=31; $x++){
$days[(string)$d] = (string)$d;
}
echo showSelect("formDays", $days, $_POST["formDays"], array("id"=>"formDays"));

Related

In php: How to set <select name=[php variable name] >?

for($i=0; $i++; $i<4)
{
$nameselected = "a".$i;
echo "<select name="$nameselected" onChange='document.changet_form.submit()'>
<option value="apple">Volvo</option>
<option value="orange">Saab</option>
<option value="grape">Opel</option>
<option value="mango">Audi</option>
</select>";
}
Get the name of :
if(($_REQUEST[$nameselected]) == "a0")
Hi, How to set the select name=[php variable name] ?
The way i set and get is it correct?
THANK YOU :)
This should work:
for($i = 0; $i < 4; $i++) {
$nameselected = "a".$i;
echo '<select name="' . $nameselected . '" onChange="document.changet_form.submit()">
<option value="apple">Volvo</option>
<option value="orange">Saab</option>
<option value="grape">Opel</option>
<option value="mango">Audi</option>
</select>';
}
I think you made a typo in your for loop!

PHP Displaying current month in option select

How to make the current month selected by default in option select using php
Here is what i have tried so far.
$curmonth = date("F");
And to display the entire month
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
And according to the above code, I am assigning the current month as $curmonth, and inside loop assigning the $allmonth for entire, month.
And inside the Value
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
for checking if the current month equals all month and displaying the selected to make it select. But i am not getting result.. What i am getting is all the items are being displayed in the option select.
What i am missing ?
What you have missed is,
You are trying to display the selected inside the value
What you need to do is
<option value="<?php
echo $i;
?>"
<?php
if($allmonth==$curmonth)
{
echo ' selected';
}
?>
>
<?php
echo $allmonth;
}
?>
</option>
So, the result will be
<select >
<option value="1">
January<option value="2">
February<option value="3">
March<option value="4">
April<option value="5">
May<option value="6">
June<option value="7">
July<option value="8">
August<option value="9" selected>
September<option value="10">
October<option value="11">
November<option value="12">
December</option>
You're not correctly closing the <option> tags you are creating. Indenting you're code makes these issues more apparent:
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php
echo $i;
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
//Close tag inside loop
?>
</option>
<?php
}
Hope you are wrong here
<option value="<?php
echo $i; ?>"
<?php
if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
>
<?php
echo date("F",mktime(0,0,0,$i,1,date("Y")));
}
?>
</option>
You have missed closing quotes for the value
Take a look on this example:
$current = date('F');
for($i = 1 ; $i <= 12; $i++) {
$month = date("F",mktime(0,0,0,$i,1,date("Y")));
if( $current == $month )
echo $month . " - Selected \r\n";
else
echo $month . "\r\n";
}
IN HTML FORMAT:
echo "<select>";
$current = date('F');
for($i = 1 ; $i <= 12; $i++) {
$month = date("F",mktime(0,0,0,$i,1,date("Y")));
if( $current == $month )
echo "<option value='$month' selected='selected'>" $month . "</option>";
else
echo "<option value='$month'>" $month . "</option>";
}
echo "</select>";
DEMO
You have error with closing quotes in the value of options in the line
<option value="<?php
echo $i; if($curmonth==$allmonth)
{
echo 'selected';
}
?>"
^ // This is the quotes opened for value and is not properly closed
>
Here you have to close the quotes. As per your code it will display as:
<option value="9 selected" >
Also you are not closing <option> properly. Change your code to :
<select>
<?php
for($i = 1 ; $i <= 12; $i++)
{
$allmonth = date("F",mktime(0,0,0,$i,1,date("Y")))
?>
<option value="<?php echo $i;?>" <?php if($curmonth==$allmonth){echo 'selected';}?> >
^ // close quotes here
<?php
echo $allmonth;?>
</option>
}
</select>

How to preserve selected year option from a drop-down list after form validation in PHP

I have a form where the user inserts and selects some data in a form, one of the select fields is his/her birth year.
The drop-down list is populated fine, BUT after submitting the form for validation, I can not preserve the selected year and the user has to re-select it again!
This is what I've done:
<select size="1" name="birthYear" tabindex="7">
// Please Select Option
<option selected value="-1" <?php if(isset($_POST['birthYear']) && $_POST['birthYear'] == '-1') { echo 'selected="selected"'; } ?> >Please Select</option>
// populate birth years range
<?php
$currentYear = date('Y');
$minimumBirthYear = $currentYear - 10;
$MaximumBirthYear = $currentYear - 100;
for($i = $minimumBirthYear; $i >= $MaximumBirthYear; $i--){
echo '<option value="'.$i.'">'.$i.'</option><br />';
}
?>
</select>
Can you please help me in applying the
<?php if(isset($_POST['birthYear']) && $_POST['birthYear'] == '-1') { echo 'selected="selected"'; } ?> >
in the for loop? I've tried it in different ways but with no luck!
Thanks in advance...
Try this:
for($i = $minimumBirthYear; $i >= $MaximumBirthYear; $i--){
echo '<option '.(isset($_POST['birthYear']) && $_POST['birthYear'] == $i ? 'selected' : '').' value="'.$i.'">'.$i.'</option><br />';
}
<select size="1" name="birthYear" tabindex="7">
// Please Select Option
<option selected value="-1">Please Select</option>
// populate birth years range
<?php
$currentYear = date('Y');
$minimumBirthYear = $currentYear - 10;
$MaximumBirthYear = $currentYear - 100;
for($i = $minimumBirthYear; $i >= $MaximumBirthYear; $i--)
{
echo sprintf("<option value='%s' %s >%s</option><br/>", $i, isset($_POST['birthYear']) && $_POST['birthYear']? "selected='selected'": "", $i);
}
?>

current years in dropdown select box

I have this function for print current year in select box but option value not equal with option text. i.e :current years in selected value is 2013 but html text output is 2012. how to fix this?
PHP:
<select name="year">
<?php
for($i=date("Y")-5;$i<=date("Y");$i++) {
$sel = ($i == date('Y')) ? 'selected' : '';
echo "<option value=".$i." ".$sel.">".date("Y", mktime(0,0,0,0,1,$i))."</option>";
}
?>
</select>
Output:
<select name="year">
<option value=2008 >2007</option>
<option value=2009 >2008</option>
<option value=2010 >2009</option>
<option value=2011 >2010</option>
<option value=2012 >2011</option>
<option value=2013 selected>2012</option>
</select>
Maybe not a good wayŁˆ But just Add +1 for $i:
<select name="year">
<?php
for($i=date("Y")-5;$i<=date("Y");$i++) {
$sel = ($i == date('Y')) ? 'selected' : '';
echo "<option value=".$i." ".$sel.">".date("Y", mktime(0,0,0,0,1,$i+1))."</option>"; // change This Line
}
?>
</select>
Online Demo Here
Problem with your value printing
<select name="year">
<?php
for($i=date("Y")-5;$i<=date("Y");$i++) {
$sel = ($i == date('Y')) ? 'selected' : '';
echo "<option value=".$i." ".$sel.">".$i."</option>"; // here I have changed
}
?>
</select>
<?php
$yearArray = range(2015, 2050);
?>
Year
<select name="year" id="year">
<option value="">Select Year</option>
<?php
foreach ($yearArray as $year) {
$selected = ($year == 2017) ? 'selected' : '';
echo '<option '.$selected.' value="'.$year.'">'.$year.'</option>';
}
?>
</select>

PHP drop down menu

I am doing a select on the database and some time it returns me 10 records and sometimes 1000, depending on the criteria of search. What I want is a drop down that will have break after every 30 records. something like this
<select id="dd" >
<option value="0">1-30</option>
<option value="30">31-60</option>
<option value="60">61-90</option>
<option value="91">91-120</option>
</select>
How do I do that in PHP dynamically?
Try this :)
<select id="dd" >
<?php
$start = 0;
$end = mysql_num_rows($resultquery);
while ( ($start+30) < $end || ($end-$start)>0)
echo "<option value=\"". $start . "\">". $start ."-" . ($start+=30) . "</option>";
?>
</select>
if $end = 95; output will be:
<select id="dd" >
<option value="0">0-30</option>
<option value="30">30-60</option>
<option value="60">60-90</option>
<option value="90">90-120</option>
</select>
Here's my take on it:
echo '<select id="dd">';
$j = 0;
for($i=0;$i<$numResults;$i++)
{
if($i%30==0)
echo '<option value="'.$j.'">'.($j+1).'-'.($j+=30).'</option>';
}
echo '</select>';
for $numResults == 130 output would be:
<select id="dd">
<option value="0">1-30</option>
<option value="30">31-60</option>
<option value="60">61-90</option>
<option value="90">91-120</option>
<option value="120">121-150</option>
</select>
Maybe something along the lines of this:
<select id="dd" >
<?php
$rows = mysql_num_rows($result);
for($count=0;$count<$rows;$count++)
{
if($count % 30 == 0)
{
$end = $count+30;
if($end > $rows)
$end = $rows;
echo '<option value="'.$count.'">'.$count.'-'.$end.'</option>';
}
}
?>
</select>
This is untested.

Categories