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

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!

Related

HTML select option selected does not work

I want to set the current date by default but it works only on year's select (the last one).
I wouldn't like to use javascript in this case. Just html and php.
I have already tried "selected='selected'" and "autocomplete=off" as shown in different other posts.
Here my code
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if (sprintf('%02d', $i) == date("d")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<select id="orders-month">
<?php
for ($i=1; $i <= 12; $i++) {
if (sprintf('%02d', $i) == date("m")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if (sprintf('%02d', $i) == date("Y")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<input type="submit" value="OK" />
</form>
here the plain HTML when the PHP has been run:
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<option value="01">01</option>
<option value="02" selected>02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select id="orders-month">
<option value="01">01</option>
<option value="02">02</option>
<option value="03" selected>03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<select id="orders-year">
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017" selected>2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
<input type="submit" value="OK" />
</form>
This is the code you are looking for
Remember to use single quotes (') when including double quotes inside the PHP echo or else if using double quotes use " inside the outer double quotes.
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if ($i == date("d")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
<select id="orders-month">
<?php
for ($i=1; $i <= 12; $i++) {
if ($i == date("m")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if ($i == date("Y")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
Can you try like this.
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if ($i == date("d")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
?>
</select>
<select id="orders-month">
for ($i=1; $i <= 12; $i++) {
if ($i == date("m")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if ($i == date("Y")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
?>
</select>
<input type="submit" value="OK" />
</form>
Here's a more compact way of achieving this:
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php for ($d = 1; $d <= 31; $d++) { ?>
<option value="<?php echo $d; ?>" <?php if ($d == date('j')) { echo 'selected'; ?>><?php echo $d; ?></option>
<?php } ?>
</select>
<select id="orders-month">
<?php for ($m = 1; $m <= 12; $m++) { ?>
<option value="<?php echo $m; ?>" <?php if ($m == date('n')) { echo 'selected'; ?>><?php echo $m; ?></option>
<?php } ?>
</select>
<select id="orders-year">
<?php for ($y = 2012; $y <= 2020; $y++) { ?>
<option value="<?php echo $y; ?>" <?php if ($y == date('Y')) { echo 'selected'; ?>><?php echo $y; ?></option>
<?php } ?>
</select>
<input type="submit" value="OK" />
</form>
Have you tried autocomplete="off" for select? It works for me.
I know you said you tried this already, but your sample code doesn't show it. Here's how your tag should be formed:
<select>
<option value="01">01</option>
<option value="02" selected="selected">02</option>
<option value="03">03</option>
...
</select>
Your example above looks very different for the selected element. I'd suggest using whatever inspection tool is built into your web browser to see what exactly was rendered, and that will probably make it the solution obvious.
For reference, see:
Definition of select tag
Definition of option tag

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 display 01 instead of 1 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Formatting a number with leading zeros in PHP
I am populating a select box with php.
This is the code:
$select_month_control = '<select name="month" id="month">';
for($x = 1; $x <= 12; $x++) {
$select_month_control.= '<option value="'.$x.'"'.($x != $month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
}
$select_month_control.= '</select>';
This is creating this:
<select id="month" name="month">
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
<option value="4">April</option>
<option value="5">May</option>
<option value="6">June</option>
<option value="7">July</option>
<option value="8">August</option>
<option selected="selected" value="9">September</option>
<option value="10">October</option>
<option value="11">November</option>
<option value="12">December</option>
</select>
The problem is that I need the 1, 2, 3 etc to be 01, 02, 03 etc... Like this:
<option value="01">January</option>
instead of:
<option value="1">January</option>
How can I do this?
You can use sprintf("%02d", $number) to format strings.
Edit: See http://www.php.net/manual/en/function.sprintf.php for more information about format strings
user str_pad ref : http://php.net/manual/en/function.str-pad.php
$select_month_control = '<select name="month" id="month">';
for($x = 1; $x <= 12; $x++) {
$select_month_control.= '<option value="'.str_pad($x, 2, "0", STR_PAD_LEFT).'"'.($x != $month ? '' : ' selected="selected"').'>'.date('F',mktime(0,0,0,$x,1,$year)).'</option>';
}
$select_month_control.= '</select>';
You might want to try using the str_pad() function within your loop :
str_pad — Pad a string to a certain length with another string
for($x = 1; $x <= 12; $x++) {
$value = str_pad($x,2,"0",STR_PAD_LEFT);
$select_month_control.= '<option value="'.$value.'">'.$value.'</option>';
}

Mark elements in a selectbox from parameter in URL

I have a little problem with a selectbox that have to have some marked options from a URL parameter.
I have an URL that can look like this
index.php?page=edit&lid=4&recs=1,4,7&hl=Test&lhash=7c2cd87dad07ac99a00e92041a5d6a38
Where I want to use 1,4,7 from the recs parameter to mark the mailgroups with ID 1, 4 and 7 in my selectbox like
<select name="groups" multiple="multiple">
<option value="1" selected="selected">Group 1</option>
<option value="2">Group 2</option>
<option value="3">Group 3</option>
<option value="4" selected="selected">Group 4</option>
<option value="5">Group 5</option>
<option value="6">Group 6</option>
<option value="7" selected="selected">Group 7</option>
<option value="8">Group 8</option>
</select>
I tried to use this to make a variable with the selected="selected" value
$recis = explode(",", $_GET["recs"]);
foreach($recis as $rec) {
if($row["mailgroup_id"] == $rec) {
$sel = 'selected="selected"';
} else {
$sel = '';
}
}
And then
<option value="'. $row["mailgroup_id"] .'" '. $sel .'>'. $row["mailgroup_name"] .' - '. $row["country"] .'</option>
But that doesn't seem to work as all groups become marked
why don't you try loop on mailgroup_id record set
$recis = explode(",", $_GET["recs"]);
while($row=mysql_fetch_array($rs))
{
if( in_array($row["mailgroup_id"], $recis))
{
$sel = 'selected="selected"';
}
else
{
$sel = '';
}
// Option Code
}
Option Code
<option value="'. $row["mailgroup_id"] .'" '. $sel .'>'. $row["mailgroup_name"] .' - '. $row["country"] .'</option>
IT seems to me that the value of $sel just keeps changing throughout the loop, but then when you print it is just whatever it was set in the last loop iteration. What you need is this
foreach($recis as $rec) {
if($row["mailgroup_id"] == $rec) {
$sel = 'selected="selected"';
} else {
$sel = '';
}
// print the option tag here or store the $sel value
// for the given mailgroup_id
}
p.s. why call it $sel instead of $selected? "sel" can mean many things, don't be afraid of longer names - it improves readbility
Try this:
$recis = explode(",", $_GET["recs"]);
foreach($recis as $rec) {
if($row["mailgroup_id"] == $rec) {
$row["selected"] = 'selected="selected"';
} else {
$row["selected"] = '';
}
}
And in the html you can do this:
<option value="'. $row["mailgroup_id"] .'" '. $row['selected'].'>'. $row["mailgroup_name"] .' - '. $row["country"] .'</option>

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