Keep Values Selected After Form Submission

Keep values selected after form submission

To avoid many if-else structures, let JavaScript do the trick automatically:

<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>

<script type="text/javascript">
document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>

<select name="location" id="location">
<option value="x">x</option>
<option value="y">y</option>
</select>

<script type="text/javascript">
document.getElementById('location').value = "<?php echo $_GET['location'];?>";
</script>

Keep the selected value after the form submission

You can use this code. Although I haven't tested the code but let me share my logic with you to make you a better understanding of it. I have added a condition that if the form is submitted then it should display the form with $_POST['toolchain'] selected otherwise it should display it in normal ways.

<?php
$filename = 'select.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
if(isset($_POST['toolchain'])
{
?>
<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<option selected value="<?php echo $_POST['toolchain']; ?>"><?php echo $_POST['toolchain']; ?></option>
<?php foreach($eachlines as $lines){
if($lines!=$_POST['toolchain'])
echo "<option value='".$lines."'>$lines</option>";
}?>
</select>

</form>
<?php }
else{
?>

<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<option selected value="base">Please Select</option>
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'>$lines</option>";
}?>
</select>

</form>
<?php }
?>

It's a normal but a lengthy way. You can also add a condition inside form. If the form is submitted mean if(isset($_POST['toolchain'])) then you can make the option selected for the $_POST['toolchain'] inside foreach loop



Update



I have put the condition inside the form. Kindly update if you face any error as I haven't tested the code

    <?php
$filename = 'select.txt';
$eachlines = file($filename, FILE_IGNORE_NEW_LINES);
?>
<form action="#" method="post">
<select id="toolchain" name="toolchain" onchange='this.form.submit()'>
<?php if(isset($_POST['toolchain']))
{
?>
<option value="base">Please Select</option>
<?php foreach($eachlines as $lines){
if($_POST['toolchain']==$lines))
{
echo "<option selected value='".$lines."'>$lines</option>";
}
else {
echo "<option value='".$lines."'>$lines</option>";
}
}
}
else {

?>
<option selected value="base">Please Select</option>
<?php foreach($eachlines as $lines){
echo "<option value='".$lines."'>$lines</option>";
}
}
?>
</select>

</form>

PHP keep dropdown value after submit

You have to set the selected attribute for the option that was submitted. So you have to check the submitted value for each option. In my solution I am using the ternary operator to echo the selected attribute only for the correct operator.

<select name="operator">
<option value="add" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'add') ? 'selected' : ''; ?>>+</option>
<option value="minus" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'minus') ? 'selected' : ''; ?>>-</option>
<option value="divide" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'divide') ? 'selected' : ''; ?>>/</option>
<option value="multiply" <?php echo (isset($_POST['operator']) && $_POST['operator'] == 'multiply') ? 'selected' : ''; ?>>x</option>
</select>

The code above is somewhat repetitive. It keeps repeating a lot of code and html. It would be great if we could factor out the repetitive stuff. Luckily we can do that by creating an array that stores the options and loop through them using a foreach, like this:

<?php
$options = [
'add' => '+',
'minus' => '-',
'divide' => '/',
'multiply' => 'x'
];
?>

<select name="operator">
<?php foreach ($options as $key => $label) { ?>
<option value="<?= $key ?>" <?= (isset($_POST['operator']) && $_POST['operator'] == $key) ? 'selected' : '' ?>><?= $label ?></option>
<?php } ?>
</select>

how to retain selected values in Select field after form submission?

Use jQuery like this.

<script type="text/javascript">
jQuery(document).ready(function(){

jQuery('select#XiD').val('<?php echo $_POST['XiD'];?>');

});
</script>

Keeping values selected after form submission with php

There's a whole lot missing to actually adding a right answer for this, but i'll try anyway.

PHP locations array:

$locations = array( 'Loc1', 'Loc2', 'Loc3' );

On the form:

<form action="" method="get">
<select name="location">
<?php
foreach ( $locations as $location ) {
echo '<option value="' . $location . '" ' . ( isset( $_GET['location'] ) && $_GET['location'] == $location ? 'selected="selected"' : '' ) . '>' . $location . '</option>';
}
?>
</select>
</form>

First of all there's no way of telling that your posted value is the value name. As i recall there's no name attribute for option, it has to be a select around it which holds the attribute name, and you are putting the selected="true" in the name attribute which you are not properly closing in the first place (and which is non existing).

How to Keep the selected value of the select box after Form POST or GET

I assume you get categories from database.

you should try:

<?php

$categories = $rows; //array from database
foreach($rows as $row){
if($row['name'] == $_POST['category']){
$isSelected = ' selected="selected"'; // if the option submited in form is as same as this row we add the selected tag
} else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$row['id']."'".$isSelected.">".$row['name']."</option>";
}
?>

How to keep selected value after submit in php

The reason why 2016 is selected always is because you are setting it to 2016.

<?php
foreach ($yearArray as $year)
{

$selected = ( $year == 2016) ? 'selected' : ''; // Here, you are setting it
echo '<option name="year" '.$selected.' value="'.$year.'">'.$year.'</option>';
}
?>

Instead do this,

<?php
foreach ($yearArray as $year)
{

$selected = ((isset($_POST['year']) && $_POST['year'] == $year) || ($year == '2016')) ? 'selected' : '';

echo '<option name="year" '.$selected.' value="'.$year.'">'.$year.'</option>';
}
?>

By default 2016 is selected, if you have submitted any other value, that will be selected.



Related Topics



Leave a reply



Submit