Recently i ask how to populate years and i thought that i have all the answer i need. Thing is that i have a new problem with days. The problem seems to be more of mysql than php. In my table users, birthday field, i put "date" as the type which is in this format: 0000-00-00. Now when i use the current select box "day", it inserts 2 instead of 02 and then compromises the whole format when i join $year.$month.$day giving me and output like 1988-11-. How can i solve this? This is the code for days (the year is similar, and it's in the link above). Thanks
<?
$bday = $r['birthday'];
$part = explode("-", $bday);
$year = $part[0];
$month = $part[1];
$day = $part[2]; ?>
<select name="day">
<? for($i = 1; $i <= 31; $i++){?>
<option value="<? echo $i. '"'; if ($day == $i) {echo 'selected="selected"';}?>"><?echo $i;?></option> <? }?>
</select>
You can try
$bday = $r['birthday'];
$part = explode("-", $bday);
$year = $part[0];
$month = $part[1];
$day = $part[2]; ?>
<select name="day">
<? for($i = 1; $i <= 31; $i++){?>
<option value="<? echo str_pad($i, 2, "0", STR_PAD_LEFT) . '"'; if ($day == $i) {echo 'selected="selected"';}?>"><?echo str_pad($i, 2, "0", STR_PAD_LEFT);?></option> <? }?>
</select>
<select name="day">
<? for($i = 1; $i <= 31; $i++){?>
<option value="<? echo ($i < 10 ? '0' . $i : $i). '"'; if ($day == $i) {echo 'selected="selected"';}?>"><?echo $i;?></option> <? }?>
</select>
Related
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>
I am trying to create a drop down with just the working days of the week on. Monday - Friday. Here's my code:
<?php if ($_SESSION['month'] == $current_month) { $current_day = date("j") + 1;} else {$current_day = 1;} ?>
<form action="" method="post">
<select name="day" onchange="this.form.submit()">
<option value="">-- Day --</option>
<?php for ($i = $current_day; $i < 31; $i++) { ?>
<option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
<?php $tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i;
$weekday = date('D', strtotime($tmp_date));
echo $weekday." "; ?>
<?php echo $i; ?>
</option>
<?php } ?>
</select>
</form>
This gives me the days of the week for the current month, but it shows all days. How can I only show Monday - Friday?
It looks like $weekday is getting the names for you. Just do a nocase string compare:
$weekday = date('D', strtotime($tmp_date));
if (strcasecmp($weekday, 'Sun') != 0
&& strcasecmp($weekday, 'Sat') != 0){
// Do something with valid days
}
<?php $tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i; ?>
<?php if (!in_array(date('w', strtotime($tmp_date)), array(0, 6)) { ?>
<option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
$weekday = date('D', strtotime($tmp_date));
echo $weekday." "; ?>
<?php echo $i; ?>
</option>
<?php } ?>
This is a lot clearer then strtotime():
$start = DateTime::createFromFormat('Y-n-j', $_SESSION['year'].'-'.$_SESSION['month'].'-01');
$daysInMonth = $start->format('t');
$end = new DateTime("+{$daysInMonth} Days");
$interval = new DateInterval('P1D');
$period = new DatePeriod($start, $interval, $end);
foreach ($period as $day) {
if (in_array($day->format('D'), array('Sat', 'Sun'))) continue;
printf('<option value="%s"%s>%s %u</option>',
$day->format('j'),
($_SESSION['day'] == $day->format('j')) ? ' selected' : '',
$day->format('D'),
$day->format('j')
);
}
Demo
This works for me:
<form action="" method="post">
<select name="day" onchange="this.form.submit()">
<option value="">-- Day --</option>
<?php for ($i = $current_day; $i < 31; $i++) {
$tmp_date = $_SESSION['year']."/".$_SESSION['month']."/".$i;
$weekday = date('D', strtotime($tmp_date));
if (strcasecmp($weekday, 'Sun') != 0
&& strcasecmp($weekday, 'Sat') != 0){ ?>
<option value="<?php echo $i; ?>" <?php echo $i == $_SESSION['day'] ? "selected='selected'":""; ?> >
<?php echo $weekday." ".$i; ?>
<?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);
}
?>
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"));
I created a drop down list with the below code, but I want to set a default choice on the first load of the page (i.e., show most recent date, not the first set of options). How can I do this?
<?php
$startyear = "";
$startmonth = "";
$startday = "";
$endyear = "";
$endmonth = "";
$endday = "";
$year = range(1998,2012);
$month = range(01,12);
$day = range(01,31);
if($_SERVER['REQUEST_METHOD']=='POST')
{
foreach($_POST as $key=>$value)
{
if(is_numeric($value))
{
$$key = $value;
}
}
}
?>
Form stuff here
<form name='update' action='' method='POST'>
Start: <select name='startyear'>
<?php foreach(array_reverse($year) as $y):?>
<option value="<?php echo $y?>"<?php echo((isset($startyear) && $startyear == $y)?' selected':null)?>><?php echo $y?></option>
<?php endforeach;?>
</select>
<select name='startmonth'>
<?php foreach($month as $m): $m = str_pad($m, 2, "0", STR_PAD_LEFT);?>
<option value="<?php echo $m;?>"<?php echo ((isset($startmonth) && $startmonth == $m)?' selected':null)?>><?php echo $m;?></option>
<?php endforeach;?>
</select>
<select name='startday'>
<?php foreach($day as $d): $d = str_pad($d, 2, "0", STR_PAD_LEFT);?>
<option value="<?php echo $d;?>"<?php echo ((isset($startday) && $startday == $d)?' selected':null)?>><?php echo $d;?></option>
<?php endforeach;?>
</select>
<input type='submit' value='View'/>
</form>
if($_SERVER['REQUEST_METHOD']=='POST')
{
...
} else {
// Set your defaults here.
}
Is this you want ?
$cur_day = date('j');
$cur_mon = date('n');
$cur_year = date('Y');
and then inside the options loop something like this
if($y == $cur_year) echo 'selected';
then current year will be selected by default, do the same for month and day.
Since your check for being selected is done like this:
<?php echo ((isset($startday) && $startday == $d)?' selected':null)?>
Simply define your variables with a default value, they are empty!
$startyear = "";
$startmonth = "";
$startday = "";
$endyear = "";
$endmonth = "";
$endday = "";