how to preserve data in <select> element - php

For multiple steps form I preserve entered value in <input> elements with this code:
<input name="city" type="text" value="<?php echo isset($_POST['city']) ? $_POST['city'] : '' ?>"/>
but I don't know how to implement it for <select> elements in order to preserve selected option.

Add "selected" to the desired option:
<select>
<option value="a" <?php echo ($_POST['select'] == 'a') ? 'selected' : ''; ?>>a</option>;
<option value="b" <?php echo ($_POST['select'] == 'b') ? 'selected' : '' ?>>b</option>
<option value="c" <?php echo ($_POST['select'] == 'c') ? 'selected' : ''; ?>>c</option>
</select>
Or, if you create your select with a loop, do it once for every options:
<?php $list = array('a', 'b', 'c'); ?>
<select>
<?php foreach($list as $value): ?>
<option value="<?php echo $value; ?>" <?php echo ($_POST['select'] == $value) ? 'selected' : ''; ?>><?php echo $value; ?></option>
<?php endforeach; ?>
</select>

Related

Selecting default option in PHP dropdown list

This is likely a lot simpler than I'm thinking but I can't seem to figure it out.
I have the following code that's part of a image results list:
<option value="30" <?php echo ($_SESSION['results']== 30) ? 'selected' : ''; ?>>30</option>
<option value="40" <?php echo ($_SESSION['results']== 40) ? 'selected' : ''; ?>>40</option>
<option value="50" <?php echo ($_SESSION['results']== 50) ? 'selected' : ''; ?>>50</option>
<option value="60" <?php echo ($_SESSION['results']== 60) ? 'selected' : ''; ?>>60</option>
<option value="70" <?php echo ($_SESSION['results']== 70) ? 'selected' : ''; ?>>70</option>
<option value="80" <?php echo ($_SESSION['results']== 80) ? 'selected' : ''; ?>>80</option>
<option value="90" <?php echo ($_SESSION['results']== 90) ? 'selected' : ''; ?>>90</option>
The default of course is 40 results but I want the default to be 80. How do I make it so 80 is automatically selected by default
A cleaner way to do this would be:
<?php
session_start(); //Start session
$_SESSION['results'] = $_SESSION['results'] ? $_SESSION['results'] : 80; //Check and set the session variable else default it to 80
$numbers = [30, 40, 50, 60, 70, 80, 90]; //Initialize your array
?>
<select name="test">
<?php
foreach ($numbers as $num) { //Loop through the array and populate your select box.
?>
<option value="<?php echo $num; ?>" <?php echo ($_SESSION['results'] == $num) ? 'selected' : ''; ?>><?php echo $num; ?></option>
<?php
}
?>
</select>

Auto select the data in <select> <options> tag

I have a form which a user fills up and hits "Save & Next" which will take them to another page where the user can upload images and hit "Final Submit". They can also go back to the previous page to edit the data.
At that time, all the data he had previously filled up should be displayed on the text box. I have used session variable to store the data and display it.
I am stuck in the drop down box.
<select name="District">
<option value="East">East</option>
<option value="West">West</option>
<option value="North">North</option>
<option value="South">South</option>
</select>
When the user submits, I am storing the selected value in Session $_SESSION['District'] = $_POST['District']; and when the user clicks back to go the previous page, I need to auto select that option value in the drop down.
How can I achieve this?
Using array would make it easier.
<?php
$options = array(
'East', 'West', 'North', 'South',
);
?>
<select name="District">
<?php foreach($options as $option) { ?>
<option value="<?php echo $option; ?>" <?php echo (isset($_SESSION['District']) && $_SESSION['District'] == $option) ? "selected" : "" ?>><?php echo $option; ?></option>
<?php } ?>
</select>
Check value in session and if matches set the attibute selected
<select name="District">
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'East'){ echo 'selected'; }?> value="East">East</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'West'){ echo 'selected'; }?> value="West">West</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'North'){ echo 'selected'; }?> value="North">North</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'South'){ echo 'selected'; }?> value="South">South</option>
</select>
Just check the selected value against the one stored in session.
Your option will look like this:
<option value="East" <?php echo ($_SESSION['District']=="East" ? "selected" : ""; ?>>East</option>
And the right one will be selected
<select name="District">
<option value="East" <?php if($_SESSION['District'] == "East"):?>selected="selected"<?php endif; ?>>East</option>
... Repeat with all options ...
Use below code:
<select name="District">
<option value="East" <?php echo ($_SESSION['District'] == "East") ? "selected" : "" ?>>East</option>
<option value="West" <?php echo ($_SESSION['District'] == "West") ? "selected" : "" ?>>West</option>
<option value="North" <?php echo ($_SESSION['District'] == "North") ? "selected" : "" ?>>North</option>
<option value="South" <?php echo ($_SESSION['District'] == "South") ? "selected" : "" ?>>South</option>
</select>
<option value="East" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'East' ? 'selected="selected"' :'' ;?> >East</option>
<option value="West" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'West' ? 'selected="selected"' :'' ;?>>West</option>
<option value="North" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'North' ? 'selected="selected"' :'' ;?>>North</option>
<option value="South" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'South' ? 'selected="selected"' :'' ;?>>South</option>

Dropdown menu does not stay selected via php after refreshing the page

The following script would let the user to chose between on and off. The problem is that the selected value does not stay selected after refreshing the page. Any idea how to fix this?
<form action="Conf.php" name="Calculation" method="post">
<?php
if (isset($_POST['Submit1']))
{
$Wave = $_POST['WaveHMap'];
}
?>
<?php
$attr = 'selected="selected"';
?>
Wave Height Maps:
<select selected="selected" name="WaveHMap" VALUE="<?PHP print $Wave ; ?>">
<?php $attr= 'selected="selected"'; ?>
<option VALUE="On"<?php echo $Wave == 'On' ? $attr : ''; ?>>On</option>
<option VALUE="Off"<?php echo $Wave== 'Off' ? $attr : ''; ?>>off</option>
</select>
<Input Type="Submit" Name="Submit1" Value="Save Parameters">
</form>
Try this
<select name="WaveHMap" >
<option VALUE="On" <?php echo $Wave == 'On' ? 'selected' : ''; ?>>On</option>
<option VALUE="Off" <?php echo $Wave== 'Off' ? 'selected': ''; ?>>off</option>
</select>

PHP, HTML filter

i would like this select filter to show up only if Language count is more than 4
Any ideas?
Here is html select filter code:
<p>
Select date:
</p>
<form method="get" action="">
<select id="training_session" name="date"onchange=this.form.submit()>
<option value=""<?php if($_GET['date'] === '') echo 'selected' ?>>[All dates]</option>
<option value="April"<?php if($_GET['date'] === 'April') echo 'selected' ?>>April</option>
<option value="May"<?php if($_GET['date'] === 'May') echo 'selected' ?>>May</option>
<option value="June"<?php if($_GET['date'] === 'June') echo 'selected' ?>>June</option>
<option value="July"<?php if($_GET['date'] === 'July') echo 'selected' ?>>July</option>
<option value="August"<?php if($_GET['date'] === 'August') echo 'selected' ?>>August</option>
<option value="September"<?php if($_GET['date'] === 'September') echo 'selected' ?>>September</option>
<option value="October"<?php if($_GET['date'] === 'October') echo 'selected' ?>>October</option>
<option value="November"<?php if($_GET['date'] === 'November') echo 'selected' ?>>November</option>
</form>
<noscript><input type="hidden" value="filter"></noscript>
Here is PHP code:
if (isset($_GET['date']) && $_GET['date']) {
foreach ($training_sessions as $key => $session) {
if (date('F', strtotime($session['ZCS_b_date'])) !== $_GET['date']) {
unset($training_sessions[$key]);
So how do i make it to show up only if language count is more than 4. I have language select filter which has to stay there all the time, but i want date filter to show up only if language count is more than 4.
This is the language select filter code:
<p>
Select language:
</p>
<form method="get" action="">
<select id="training_session" name="lang" onchange=this.form.submit()>
<option value=""<?php if($_GET['lang'] === '') echo 'selected' ?>>[All languages]</option>
<option value="English" <?php if($_GET['lang'] === 'English') echo 'selected' ?>>English</option>
<option value="Portuguese"<?php if($_GET['lang'] === 'Portuguese') echo 'selected' ?>>Portuguese</option>
<option value="French"<?php if($_GET['lang'] === 'French') echo 'selected' ?>>French</option>
<option value="Italian"<?php if($_GET['lang'] === 'Italian') echo 'selected' ?>>Italian</option>
<option value="Japanese"<?php if($_GET['lang'] === 'Japanese') echo 'selected' ?>>Japanese</option>
</form>
<noscript><input type="hidden" value="filter"></noscript>
This is the php code for language select filter:
if (isset($_GET['lang']) && $_GET['lang']) {
foreach ($training_sessions as $key => $session) {
if ($session['training_language'] !== $_GET['lang']) {
unset($training_sessions[$key]);
So does anyone knows what im trying to achieve here? What i want to do.
Ok here we go..
This should give you the results you want
<html>
<head></head>
<body>
<form id="submitquery" method="get" action="">
<?php
$month=array('[All Dates]','January','February','March','April',
'May','June','July','August',
'September','October','November','December');
$language=array('[All Languages]','English','Portuguese','French','Italian','Japanese');
$changeLanguagejs='changeForm();';
$changeDatejs='this.form.submit();';
writeSelect($language,'Language',$changeLanguagejs);
if (isset($_GET['Language'])){
if($_GET['Language']!='[All Languages]'){
$training_sessions=filterLanguage($training_sessions);}
if (count($training_sessions)>=4){
writeSelect($month,'Date',$changeDatejs);
if (isset($_GET['Date'])){
if($_GET['Date']!='[All Dates]'){
$training_sessions=filterDate($training_sessions);}
showResults($training_sessions);}}
else{showResults($training_sessions);}}
?>
</form>
</body>
</html>
<?php
function writeSelect($values,$name,$changejs){
echo "\n".'<p>Select '.$name.':</p>';
echo "\n".' <select id="training_sessions_'.$name.'" name="'.$name.'" onchange='.$changejs.'>'."\n";
foreach($values as $value){
echo ' <option value="'.$value.'" ';
if (isset($_GET[$name]) && $_GET[$name]==$value){echo 'selected ';}
echo '>'.$value.'</option>'."\n";}
echo ' </select><br/><br/>'."\n";}
function filterLanguage($training_sessions){
foreach($training_sessions as $key => $session){
if($session['training_language']!=$_GET['Language']){
unset($training_sessions[$key]);}}
return $training_sessions;}
function filterDate($training_sessions){
foreach($training_sessions as $key => $session){
if(date('F', strtotime($session['ZCS_b_date']))!=$_GET['Date']){
unset($training_sessions[$key]);}}
return $training_sessions;}
function showResults($training_sessions){
echo "\n".'Showing '.count($training_sessions).' ';
if(count($training_sessions)==1){echo 'Result';}
else{echo 'Results';}
echo'<br/>'."\n\n";
print_r($training_sessions);}
?>
<script>
function changeForm(){
var selectLang = document.getElementById("training_sessions_Language").value;
document.getElementById("submitquery").method="post";
document.getElementById("submitquery").action="?Language="+selectLang;
document.getElementById("submitquery").submit();}
</script>
I'm going to edit out the comments to make it more readable...
if you want to see the comments go back through the edits of this answer

Mark an option as selected from dynamic list of options

I've used Selected="Selected" on SELECT that is fed by values I have written into the code, my query is how can I use it with a dynamic query from the MySQL db?
This is my written code
<label>Fuel</label>
<select tabindex="1" id="proptenure" name="proptenure">
<option value=""></option>
<option value="1" <?php echo ($searchfuel == '1' ? 'selected' : '')?>>Mains gas</option>
<option value="2" <?php echo ($searchfuel == '2' ? 'selected' : '')?>>Wood or coal fire</option>
<option value="3" <?php echo ($searchfuel == '3' ? 'selected' : '')?>>Oil</option>
<option value="4" <?php echo ($searchfuel == '4' ? 'selected' : '')?>>Electric storage heaters</option>
<option value="5" <?php echo ($searchfuel == '5' ? 'selected' : '')?>>LPG or bottled gas</option>
<option value="6" <?php echo ($searchfuel == '6' ? 'selected' : '')?>>No central heating system</option>
</select>
How can use the above type of line on my Query based SELECT
<?php echo ($searchtenure == '2' ? 'selected' : '')?>
This is an example of how I'm using SELECT from a query
<label>Fuel Type</label>
<?php $fueltype = db::getInstance()->query('SELECT * FROM lkup_fueltype');
if(!$fueltype->count()) {
echo 'Problem';
} else { ?>
<select tabindex="1" id="propertyfueltype" name="propertyfueltype">
<?php foreach ($fueltype->results() as $fueltype) { ?>
<option value="<?php echo $fueltype->PropertyFuelType; ?>"><?php echo $fueltype->PropertyFuelType; ?></option> <?php } } ?>
</select>
So how can I use the Selected="selected"?
This should do the job:
<label>Fuel Type</label>
<?php $fueltype = db::getInstance()->query('SELECT * FROM lkup_fueltype');
if(!$fueltype->count()) {
echo 'Problem';
} else { ?>
<select tabindex="1" id="propertyfueltype" name="propertyfueltype">
<?php foreach ($fueltype->results() as $fueltype) { ?>
<?php if($fueltype->PropertyFuelType == 2){ $selected = ' selected="selected"'; }else{ $selected = NULL; }?>
<option value="<?php echo $fueltype->PropertyFuelType; ?>"<?php echo $selected; ?>><?php echo $fueltype->PropertyFuelType; ?></option> <?php } } ?>
</select>

Categories