Keeping select item selected in php after submit in form - php

How do i keep my select item selected via PHP? i did it the same way with radiobuttons and it worked. the only thing i changed was changed "checked" to "selected" as was recommended to me.
Here is my PHP:
if (empty($_POST["favFruit"])) {
$favFruitErr = "You must select 1 or more";
}
else {
$favFruit = $_POST["favFruit"];
}
And here is my form:
<select class=favfruitwindow name="favFruit[]" size="4" multiple>
<option <?php if (isset($favFruit) && $favFruit == "apple") echo "selected"; ?> value="apple">Apple</option>
<option <?php if (isset($favFruit) && $favFruit == "banana") echo "selected"; ?> value="banana">Banana</option>
<option <?php if (isset($favFruit) && $favFruit == "plum") echo "selected"; ?> value="plum">Plum</option>
<option <?php if (isset($favFruit) && $favFruit == "pomegranate") echo "selected"; ?> value="pomegranate">Pomegranate</option>
<option <?php if (isset($favFruit) && $favFruit == "strawberry") echo "selected"; ?> value="strawberry">Strawberry</option>
<option <?php if (isset($favFruit) && $favFruit == "watermelon") echo "selected"; ?> value="watermelon">Watermelon</option>
</select>

$favFruit will be an array even when selecting a single option, and null when nothing is selected.
print_r($_POST["favFruit"]); will return Array ( [0] => banana [1] => plum )
You need for see if the item exists in the array and select based on that check.
<select class=favfruitwindow name="favFruit[]" size="4" multiple>
<option <?php if (isset($favFruit) && in_array('apple', $favFruit)) echo "selected"; ?> value="apple">Apple</option>
<option <?php if (isset($favFruit) && in_array('banana', $favFruit)) echo "selected"; ?> value="banana">Banana</option>
<option <?php if (isset($favFruit) && in_array('plum', $favFruit)) echo "selected"; ?> value="plum">Plum</option>
<option <?php if (isset($favFruit) && in_array('pomegranate', $favFruit)) echo "selected"; ?> value="pomegranate">Pomegranate</option>
<option <?php if (isset($favFruit) && in_array('strawberry', $favFruit)) echo "selected"; ?> value="strawberry">Strawberry</option>
<option <?php if (isset($favFruit) && in_array('watermelon', $favFruit)) echo "selected"; ?> value="watermelon">Watermelon</option>
</select>

Related

Highlight selected option from dropdown

Iam using PHP codeigniter framework. Value is set to select dropdown as below
<select class="form-control";>
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected'; }?> value='migrated' >migrated</option>
</select>
for each row in table there is select option like above. I want to highlight option with color if option is set to migrated on page load.
I tried like below
<select class="form-control";>
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected'; echo 'style="<color:red>"' }?> value='migrated' >migrated</option>
</select>
Your are not using proper syntax for style; you are missing a semicolon; and selected and style will be together making it useless. Try:
<select class="form-control">
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected style="background-color:red"' }?> value='migrated' >migrated</option>
</select>
Although to me it is easier to read inline conditions as the following:
<select class="form-control">
<option <?php echo $d1 == 'native' ? 'selected' : ''; }?> value='native' >native</option>
<option <?php echo $d1 == 'migrated' ? 'selected style="background-color:red"' : ''; }?> value='migrated' >migrated</option>
</select>
your styling is wrong
<select class="form-control";>
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected'; echo 'style="color:red"'; }?> value='migrated' >migrated</option>
</select>
you don't need the brackets <> inside the inline style and don't forget semicolon ;

How do I keep the drop down value selected when I update the record/form data in PHP?

Here is the code that i am using for drop down to select.
<select name="formreg" value="">
<option value="Registered" <?php if($f_reg == 'Registered') { ?> selected <?php echo $f_reg; } ?> >Registered</option>
<option value="NonReg" <?php if($f_reg == 'NonReg') { ?> selected <?php echo $f_reg; } ?> >NonReg</option>
</select>
<select name="regform">
<option value="Registered" <?php if (isset($register) && $register=='Registered') {?> selected='selected' <?php echo $register; }?>>Registered</option>
<option value="NonReg" <?php if (isset($nonreg) && $nonreg == 'NonReg') {?> selected='selected' <?php echo $nonreg;} ?>>NonReg</option>
</select>
At the top of script:
$f_reg = $_POST["f_reg"]; // or $f_reg = $_GET["f_reg"] if you pass parameters as the url part
then your < select > tag:
<select name="formreg">
<option value="Registered" <?php if ($f_reg == "Registered") print "selected"?>>Registered</option>
<option value="NonReg" <?php if ($f_reg == "NonReg") print "selected"?>>NonReg</option>
</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>

select box's value after submit

I have simple code
File name:newEmptyPHP.php
<form method="post" action="newEmptyPHP.php">Title:
<select name="title">
<option value="select">Select</option>;
<option value="Dr" selected=<?php if(isset($_POST[ 'title'])=="Dr" ){ echo "selected"; } ?>>Dr</option>';
<option value="Prof">Prof</option>';
<option value="Mr">Mr</option>';
<option value="Ms">Ms</option>';
<option value="Miss">Miss</option>';
<option value="Mrs">Mrs</option>';</select>
<input type="submit" value="submit">
</form>
I want to keep selected particular value which was selected before the form submission.
But i did not get the desirable output.
any help appreciated.
change this
<option value="Dr" selected=<?php if(isset($_POST['title']) == "Dr")
{ echo "selected"; } ?> >Dr</option>';
to
<option value="Dr" <?php if(isset($_POST['title']) && $_POST['title'] == "Dr")
{ echo "selected='selected'"; } ?> >Dr</option>';
<?php if(isset($_POST['title']) == "Dr"){ echo "selected"; } ?>
Change to
<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo "selected"; } ?>
But better to use JS / jQuery. Example for jQuery:
<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"):?>
<script type="text/javascript">
$(function(){
$('select[name=title]').val('<?php echo html_special_chars($_POST[''title'])?>');
});
</script>
<?php endif?>
<?php
if($_POST['title'] == "Dr")
{ ?>
<option value="Dr" selected>Dr</option>
<?php
}
else
{
?>
<?php
<option value="Dr">Dr</option>
<?php
}
?>
Change
<option value="Dr" selected=<?php if(isset($_POST['title']) == "Dr"){ echo "selected"; } ?> >Dr</option>';
To :
<option value="Dr" <?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo 'selected="selected"'; } ?> >Dr</option>';
1.Remove isset where you are checking it for value, isset will return either true or false
2.Make selected="selected inside if condition
There is a mistake in your code. isset(Something) will return either true or false and will not return Dr you are looking for.
Retry with this
<form method="post" action="newEmptyPHP.php">
Title:<select name="title" >
<option value="" >Select</option>
<option value="Dr" selected=<?php if(isset($_POST['title']) && $_POST['title'] == "Dr"){ echo "selected"; } ?> >Dr</option>';
<option value="Prof" <?php if(isset($_POST['title']) && $_POST['title'] == "Prof"){ echo "selected"; } ?>>Prof</option>';
<option value="Mr" <?php if(isset($_POST['title']) && $_POST['title'] == "Mr"){ echo "selected"; } ?>>Mr</option>';
<option value="Ms" <?php if(isset($_POST['title']) && $_POST['title'] == "Ms"){ echo "selected"; } ?>>Ms</option>';
<option value="Miss" <?php if(isset($_POST['title']) && $_POST['title'] == "Miss"){ echo "selected"; } ?>>Miss</option>';
<option value="Mrs" <?php if(isset($_POST['title']) && $_POST['title'] == "Mrs"){ echo "selected"; } ?>>Mrs</option>';
</select>
<input type="submit" value="submit">
I thin you have static option values.If so the you have to check for every option like as below for one.
<form method="post" action="newEmptyPHP.php">
Title:<select name="title" >
<option value="select" >Select</option>;
<?php
$selected = "";
if(isset($_POST['title']) && $_POST['title'] == "Dr")
{
$selected = 'selected="selected"';
}
?>
<option value="Dr" <?php echo $selected; ?>>Dr</option>
<option value="Prof">Prof</option>
<option value="Mr">Mr</option>
<option value="Ms">Ms</option>
<option value="Miss">Miss</option>
<option value="Mrs">Mrs</option>
</select>
<input type="submit" value="submit">
</form>
I would rather prefer this code for the purpose
$option_array = array('select'=>'select','mr'=>'mr'... other values);
<?php
$option_string = '';
foreach($option_array as $key=>$value)
{
if($key == $_POST['title'])
{
$selected = 'selected';
}
else
{
$selected = '';
}
$option_string .= "<option value='$key' $selected>$value</option>";
}
?>
<form method="post" action="newEmptyPHP.php">Title:
<select name="title">
<?php echo $option_string; ?>
</select>
<input type="submit" value="submit">

how to put value in select option

I have select option in my page or drop down list,my problem is how can i set value
in my select option and this value is from the database,
here is my code.
<select name="status" value="<?php echo $status; ?>" >
<option value=""></option>
<option value="public">public</option>
<option value="private">private</option>
</select>
on top of my html tag
if(isset($_GET['status']))
{
$status = $_GET['status'];
$sstatus="select .......";
foreach($db->query($sstatus) as $rows)
{
$status= $rows['status'];
......
......
......
}
}
I tried modify my code and it's seems working but my problem is that it has 2 same
value in the drop down list.
<select name="status" >
<option selected="selected"><?php echo $status; ?></option>
<option value="public">public</option>
<option value="private">private</option>
</select>
it will show like this in my drop down list,my question for this is this is the right way
in displaying the values that comes from the database.
public
public
private
<select name="status" >
<option value=""></option>
<option value="public"<?php if (isset($status) && $status === 'public') echo 'selected'; ?>>public</option>
<option value="private"<?php if (isset($status) && $status === 'private') echo 'selected'; ?>>private</option>
</select>
<?php $options = array('', 'public', 'private') ?>
<select name="status">
<?php foreach ($options as $option): ?>
<option value="<?php echo $option ?>" <?php echo isset($status) && $status == $option ? 'selected="selected"' : '' ?>><?php echo $option ?></option>
<?php endforeach ?>
</select>

Categories