HTML <select> default option - php

I know this question has been asked before, but I have an unaddressed concern about it.
How can you have a default value that is generated by serverside script?
Ex:
Weekday: <select name="weekday" class="right" value="<% =rs("Weekday")%>">
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday">Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>
I know you can have the selected attribute with regular elements, but I'm thrown for a loop here.
Any ideas?

You want your HTML to look something like this eventually - notice the selected after "Friday" to select Friday by default:
<select name="weekday" class="right">
<option value="Monday">Monday</option>
<option value="Tuesday">Tuesday</option>
<option value="Wednesday">Wednesday</option>
<option value="Thursday">Thursday</option>
<option value="Friday" selected>Friday</option>
<option value="Saturday">Saturday</option>
<option value="Sunday">Sunday</option>
</select>
A quick and dirty PHP example can be used like so, where each option is asked to verify whether it is the correct option for the current date. If it is, show selected otherwise don't show anything. You can try this code on http://phpfiddle.org/lite.
<html>
<body>
<select name="weekday" class="right">
<option value="Monday" <?php echo setSelected('Monday'); ?>>Monday</option>
<option value="Tuesday" <?php echo setSelected('Tuesday'); ?>>Tuesday</option>
<option value="Wednesday" <?php echo setSelected('Wednesday'); ?>>Wednesday</option>
<option value="Thursday" <?php echo setSelected('Thursday'); ?>>Thursday</option>
<option value="Friday" <?php echo setSelected('Friday'); ?>>Friday</option>
<option value="Saturday" <?php echo setSelected('Saturday'); ?>>Saturday</option>
<option value="Sunday" <?php echo setSelected('Sunday'); ?>>Sunday</option>
</select>
</body>
</html>
<?php
function setSelected($day)
{
date_default_timezone_set('America/Chicago');
return strtolower($day) === strtolower(date('l')) ? ' selected' : '';
}
?>
I'd be more inclined to create the select/option on the server side itself. Again, quick and dirty PHP example:
<html>
<body>
<select name="weekday" class="right">
<?php
date_default_timezone_set('America/Chicago');
$dayOfWeek = strtolower(date('l'));
$days = array('Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday');
foreach ($days as $day)
{
echo sprintf("<option value=\"%s\" %s>%s</option>\n",
$day,
$dayOfWeek == strtolower($day) ? 'selected' : '',
$day
);
}
?>
</select>
</body>
</html>

Related

How can I achieve years on drop down menu

Hello everyone one I want to get this kind of drop down menu which has years need to be selected
For example season 2021/2022, 2020/2021, all these value should be generated from 1990 to 2022
If I understood correctly, you want to populate the select with options with all seasons until now since a specific year?
You could modify a for loop so the years appears in a descending order, and then output the option tag with the "previous year / year" as a value like this:
<div class="season-select-wrapper">
<?php
// Define variables for the select
$currentYear = 2022;
$startYear = 1990;
?>
<label for="season">Select a season</label>
<select name="season" id="season">
<?php for ($year = $currentYear; $year > $startYear; $year--) { $prevYear = $year - 1; ?>
<option value="<?php echo "{$prevYear}/{$year}"; ?>"><?php echo "{$prevYear}/{$year}"; ?></option>
<?php } ?>
</select>
</div>
This would create HTML markup like this:
<div class="season-select-wrapper">
<label for="season">Select a season</label>
<select name="season" id="season">
<option value="2021/2022">2021/2022</option>
<option value="2020/2021">2020/2021</option>
<option value="2019/2020">2019/2020</option>
<option value="2018/2019">2018/2019</option>
<option value="2017/2018">2017/2018</option>
<option value="2016/2017">2016/2017</option>
<option value="2015/2016">2015/2016</option>
<option value="2014/2015">2014/2015</option>
<option value="2013/2014">2013/2014</option>
<option value="2012/2013">2012/2013</option>
<option value="2011/2012">2011/2012</option>
<option value="2010/2011">2010/2011</option>
<option value="2009/2010">2009/2010</option>
<option value="2008/2009">2008/2009</option>
<option value="2007/2008">2007/2008</option>
<option value="2006/2007">2006/2007</option>
<option value="2005/2006">2005/2006</option>
<option value="2004/2005">2004/2005</option>
<option value="2003/2004">2003/2004</option>
<option value="2002/2003">2002/2003</option>
<option value="2001/2002">2001/2002</option>
<option value="2000/2001">2000/2001</option>
<option value="1999/2000">1999/2000</option>
<option value="1998/1999">1998/1999</option>
<option value="1997/1998">1997/1998</option>
<option value="1996/1997">1996/1997</option>
<option value="1995/1996">1995/1996</option>
<option value="1994/1995">1994/1995</option>
<option value="1993/1994">1993/1994</option>
<option value="1992/1993">1992/1993</option>
<option value="1991/1992">1991/1992</option>
<option value="1990/1991">1990/1991</option>
</select>
</div>

Form select option selected PHP

I try to find the way how to make option tag selected if contain current day.
For example if my string $currentDay=03; called by PHP date('d'); I would like my select like this:
<select>
<option value="01">01</option>
<option value="02">02</option>
<option value="03" selected>03</option>
<option value="04">04</option>
...
...
</select>
<?php $day=date('d');?>
<select>
<option value="01" <?=($day=='01')?'selected':'';?> >01</option>
<option value="02" <?=($day=='02')?'selected':'';?> >02</option>
<option value="03" <?=($day=='03')?'selected':'';?> >03</option>
<option value="04" <?=($day=='04')?'selected':'';?> >04</option>
...
...
</select>
You can use the following code:
$day = date('d');
$selected = ' selected="selected" ';
<select>
<option value="01" <?php if($day=='01') echo $selected; ?>>01</option>
<option value="02" <?php if($day=='02') echo $selected; ?>>02</option>
<option value="03" <?php if($day=='03') echo $selected; ?>>03</option>
<option value="04" <?php if($day=='04') echo $selected; ?>>04</option>
...
...
</select>
<select>
<? foreach ($options as $option) : ?>
<option value="<?= $option ?>" <? if ($selected_option==$option) echo 'selected' ?>>
<?= $option ?>
</option>
<? endforeach ?>
</select>

How to set Dropdown List default value based on user selection

I have a dropdown list. However i want to set a default based on user selection. Therefore the default value is not consistent. What can i do to achieve this? I have set $country = $_POST['country']; as user selection.
<td>Country:</td>
<td colspan="2"><select name="country">
<option value="93">93-Afghanistan</option>
<option value="355">355-Albania</option>
<option value="213">213-Algeria</option>
<option value="1-684">1-684-American Samoa</option>
<option value="376">376-Andorra</option>
<option value="244">244-Angola</option>
<option value="1-264">1-264-Anguilla</option>
<option value="672">672-Antarctica</option>
<option value="1-268">1-268-Antigua and Barbuda</option>
<option value="54">54-Argentina</option>
<option value="374">374-Armenia</option>
<option value="297">297-Aruba</option>
<option value="61">61-Australia</option>
</select>
To add on. There are 200 over options (I never list all down) so i hope to get a convenience way to achieve this
You can use 'selected' for relevant option
<option value="93" <?php if($country==93) echo "selected" ?> >93-Afghanistan</option>
<option value="355" <?php if($country==355) echo "selected" ?> >355-Albania</option>
like this add if conditon for each option.
Try using session for it.
The php code:
session_start();
$_SESSION['selectedCountry'] = $_POST['country'];
Then you can use JQuery to make dropdown selected:
$(document).ready(function(){
$(function() {
$("#country").val("<?php echo $_SESSION['selectedCountry'];?>");
});
})
Please try out this ..
HTML :-
<td>Country:</td>
<td colspan="2"><select id="country" name="country">
<option value="93">93-Afghanistan</option>
<option value="355">355-Albania</option>
<option value="213">213-Algeria</option>
<option value="1-684">1-684-American Samoa</option>
<option value="376">376-Andorra</option>
<option value="244">244-Angola</option>
<option value="1-264">1-264-Anguilla</option>
<option value="672">672-Antarctica</option>
<option value="1-268">1-268-Antigua and Barbuda</option>
<option value="54">54-Argentina</option>
<option value="374">374-Armenia</option>
<option value="297">297-Aruba</option>
<option value="61">61-Australia</option>
</select>
</td>
JQuery :-
$(document).ready(function(){
$(function() {
$("#country").val("<?php echo $_POST['country'];?>");
});
})
Try this...
<?php
(isset($_POST["country"])) ? $country = $_POST["country"] : $country=93;
?>
<form action='stackover.php' method='POST'>
<select id="country" name="country">
<option <?php if ($country == 93 ) echo 'selected' ; ?> value="93">93-Afghanistan</option>
<option <?php if ($country == 355 ) echo 'selected' ; ?> value="355">355-Albania</option>
<option <?php if ($country == 213 ) echo 'selected' ; ?> value="213">213-Algeria</option>
</select>
<input type="submit" value="Submit">
</form>

How to temporarily hold the option value I selected in session?

I am new to PHP. I have a problem. I have a simple HTML form where when I select the country from select / option list provided, i want PHP to echo message on country I selected. Also I want PHP include function to list the form specific to that country name (by concatanating). Can someone please help me by pointing where I went wrong. I am new to PHP, self taught and have no idea if this approach is correct. Thanks in advance for anticipated help.
<pre>
<form name="form1" method="post" action="component_project_in_countries.php">
<label for="country">View outlets in which country? </label>
<select name="country_chosen" onchange="document.form1.submit()" id="country">
<option value="">All Middle Eastern Countries</option>
<option value="Iraq">Iraq</option>
<option value="Kuwait">Kuwait</option>
<option value="Bahrain">Bahrain</option>
<option value="Saudi">Saudi Arabia</option>
<option value="Qatar">Qatar</option>
<option value="Oman">Oman</option>
<option value="Yemen">Yemen</option>
<option value="Jordan">Jordan</option>
<option value="Israel">Israel</option>
</select>
</form>
<br>
<?php
$country_selected = '';
$country_selected = $_POST['country_chosen'];
echo "You have chosen to view outlets in " . $country_selected;
include ("inc/OutletsIn" . $country_selected .".php";)
?>
</pre>
Thank you for your help on my earlier post. I am able to echo out and concatenate file name in PHP include. But I have a new problem. The option value i selected (country name) does not get displayed in the box. How can i hold the chosen value for the session. Please advise.
Thank you again.
First check that a value has been given to $_POST['country_chosen'] with isset(). Then remove that extra semi-colon that is causing a syntax error (in your include()) statement:
if(isset($_POST['country_chosen'])){
$country_selected = $_POST['country_chosen'];
echo "You have chosen to view outlets in " . $country_selected;
include("inc/OutletsIn" . $country_selected .".php");
}
Includes something (like) inc/OutletsInIsrael.php
if(!empty($_POST['country_chosen'])){
$country_selected = $_POST['country_chosen'];
echo "You have chosen to view outlets in " . $country_selected;
require "inc/OutletsIn".$country_selected.".php";
//no brackets required
} else { /* throw error */ }
Here is your updated code, just copy & replace :
component_project_in_countries.php
<pre>
<form name="form1" method="post" action="component_project_in_countries.php">
<label for="country">View outlets in which country? </label>
<select name="country_chosen" onchange="document.form1.submit()" id="country">
<option value="">All Middle Eastern Countries</option>
<option value="Iraq">Iraq</option>
<option value="Kuwait">Kuwait</option>
<option value="Bahrain">Bahrain</option>
<option value="Saudi">Saudi Arabia</option>
<option value="Qatar">Qatar</option>
<option value="Oman">Oman</option>
<option value="Yemen">Yemen</option>
<option value="Jordan">Jordan</option>
<option value="Israel">Israel</option>
</select>
</form>
<br>
<?php
if(isset($_POST['country_chosen']))
{
$country_selected = '';
$country_selected = $_POST['country_chosen'];
echo "You have chosen to view outlets in " . $country_selected;
include ("inc/OutletsIn" . $country_selected .".php");// here you have added `;` on wrong place.
}
?>
</pre>
New answer, Just replace the code of select box :
<select name="country_chosen" onchange="document.form1.submit()" id="country">
<option value="">All Middle Eastern Countries</option>
<option value="Iraq" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Iraq')) echo 'selected' ?> >Iraq</option>
<option value="Kuwait" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Kuwait')) echo 'selected' ?> >Kuwait</option>
<option value="Bahrain" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Bahrain')) echo 'selected' ?> >Bahrain</option>
<option value="Saudi" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Saudi')) echo 'selected' ?> >Saudi Arabia</option>
<option value="Qatar" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Qatar')) echo 'selected' ?> >Qatar</option>
<option value="Oman" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Oman')) echo 'selected' ?> >Oman</option>
<option value="Yemen" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Yemen')) echo 'selected' ?> >Yemen</option>
<option value="Jordan" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Jordan')) echo 'selected' ?> >Jordan</option>
<option value="Israel" <?php if(isset($_POST['country_chosen']) && ($_POST['country_chosen']=='Israel')) echo 'selected' ?> >Israel</option>
</select>

PHP Display label and selected value

:::EDIT:::
Ok so apparently I should have put in the larger picture here, and that is my fault. Ok so let's say there are 2 sets of dropdowns,
Consider the following HTML:
<form name="myform" action="process.php" method="POST">
<input type="hidden" name="check_submit" value="1" />
<select name="month">
<option selected="--">--</option>
<option value="January">01</option>
<option value="February">02</option>
<option value="March">03</option>
<option value="April">04</option>
<option value="May">05</option>
<option value="June">06</option>
<option value="July">07</option>
<option value="August">08</option>
<option value="September">09</option>
<option value="October">10</option>
<option value="November">11</option>
<option value="December">12</option>
</select>
<select name="year">
<option selected="--">--</option>
<option value="1965">65</option>
<option value="1966">66</option>
<option value="1967">67</option>
<option value="1968">68</option>
</select>
<input type="submit" />
</form>
I am using $_POST in 'process.php' to display the year selected by the user like this:
<?php
echo "{$_POST['month']} ";
echo "{$_POST['year']} ";
?>
If "67" is selected "1967" displays AND when "09" is selected, "September" is displayed so the question still stands...
Is there a way that I can display the option and the label on the same page? This is simple HTML with a PHP processor, nothing more.
Thanks!
you can't achieve it by php alone you can use jquery with this like follow
<select name="month" onchange="document.getElementById('month_text').value=this.options[this.selectedIndex].text">
<option selected="--">--</option>
<option value="January">01</option>
<option value="February">02</option>
<option value="March">03</option>
............
<option value="December">12</option>
</select>
<select name="year" onchange="document.getElementById('year_text').value=this.options[this.selectedIndex].text">
<option selected="--">--</option>
<option value="1965">65</option>
<option value="1966">66</option>
<option value="1967">67</option>
<option value="1968">68</option>
</select>
<input type="hidden" name="year_text" id="year_text" value="" />
<input type="hidden" name="month_text" id="month_text" value=""/>
in php
echo $_POST['year']; // will print 1967
echo $_POST['year_text']; // will print 67
echo $_POST['month']; // will print January
echo $_POST['month_text']; // will print 01
You can do like this:
Modify html like:
<option value="1965-65">65</option>
In PHP:
$explode = explode("-",$_POST['year']);
echo $explode[0]; //will be 1965
echo $explode[1]; //will be 65
OR
your html
<option value="1965">65</option>
PHP
echo substr($_POST['year'], 2, 2); // 65
<?php
$monthArray = array('01'=>'January','02'=>'February','03'=>'March','04'=>'April',
'05'=>'May','06'=>'June','07'=>'July','08'=>'August',
'09'=>'September','10'=>'October','11'=>'November',
'12'=>'December');
$month = $_POST['month'];
$year = $_POST['year'];
echo $month;
echo $year;
echo "You have selected ".substr($year, 2)." Year.";
echo "You have selected ".$monthArray[$month]." month.";
?>
I have updated the code for month also.

Categories