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!
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 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 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>
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.