Select-form, make the option stay selected after submit - php

I have this form:
<form method="post">
<fieldset>
<select name="taskOption" required>
<option value="" disabled selected>Choose group</option>
<?php
while($hej < count($hejsan)) {
echo '<option value=' . $hej . '>' . $hejsan[$hej] . '</option>';
$hej++;
}
?>
</select>
<input type="submit" value="Find bookings">
</fieldset>
</form>
When I press the submit button the select/option will reset to the default, how can I change this and make it stay as the one I selected?

Try this
if (isset($_POST['taskOption'])) {
$Option = $_POST['taskOption'];
}
while($hej < count($hejsan)) {
if($hej==$_POST['taskOption']){
$selected = "selected=selected";
}else{
$selected = "";
}
echo '<option value="' . $hej . '" '.$selected .'>' . $hejsan[$hej] . '</option>';
$hej++;
}

You will need to look at the option you have selected and make the corresponding option selected. Since your form leads to the same page, you can just look at the contents of $_POST.
P.S.: also, you will find it useful to echo a newline after each option to make the output more readable.

Try this:
<?php
$taskOption = '';
if (isset($_POST['taskOption']) {
$taskOption = $_POST['taskOption'];
}
?>
<form method="post">
<fieldset>
<select name="taskOption" required>
<option value="" disabled selected>Choose group</option>
<?php
while($hej < count($hejsan)) {
if ($hey == $taskOption) {
echo "<option value={$hey} selected>{$hejsan[$hej]}</option>";
} else {
echo "<option value={$hey}>{$hejsan[$hej]}</option>";
}
$hej++;
}
?>
</select>
<input type="submit" value="Find bookings">
</fieldset>
</form>

At a if else statement to it. If the option value is equal to the select, then add the selected="selected"
This is an example code. could be that the operator is wrong, im always confused with the greater then and smaller then operators.
<?php
$option_value = 5;
for($i=0; $i > 10; $i++){
if($option_value == 5){
echo '<option value="'.$i.'" selected="selected">Item number'.$i.' is selected</option>';
} else {
echo '<option value="'.$i.'">Item number'.$i.'</option>';
}
}
?>

Related

Option value selected for loop

I am trying to create a selected value of the previous output.
(the user fills in a form , submits, returns to the form) And I want the selected value to be the value the user previously used.
The vraagNummer and answer are given in the url parameter and the dropdown menu of items available in the list are created in a for loop.
Now I got stuck on that part.. How do I create an option selected value if it is created in a for loop?
Usually I would just put in option value = $vraagNummer selected> <?php echo $vraagNummer ?></option but in this case that wouldn't work. I suppose?
Anyone knows how to fix this?
Same with the A/B/C/D. How do I put in the selected value of answer into the value previously selected while still keeping the others as an option.
With kind regards,
if(!empty($_GET['vraagnummer'])){
$vraagNummer = $_GET['vraagnummer'];
if(!empty($_GET['answer'])){
$answer = $_GET['answer'];
}
}
echo $vraagNummer;
echo $answer;
?>
<form id="start" method="post" action="index.php?test=true">
<select name="question">
<?php
for($i= 1; $i < $nRows+1 ; $i++){
?><option value="<?php echo $i ?>">Question <?php echo $i?></option>
<?php
}
?>
</select>
<select name="result">
<option value="A">Answer A</option>
<option value="B">Answer B</option>
<option value="C">Answer C</option>
<option value="D">Answer D</option>
<input type="submit" id="submit" value="Submit" />
</form>
You simply test the $_GET['question'] against the current $i value in the loop using a ternary if
<form id="start" method="post" action="index.php?test=true">
<select name="question">
<?php
for($i= 1; $i < $nRows+1 ; $i++){
$sel = (isset($_GET['question']) && $_GET['question'] == $i) ? 'selected="selected"' : '';
echo '<option ' . $sel . ' value="' . $i .">Question ' . $i . '</option>';
}
?>
</select>
Warning:
Also be careful with the use of empty() when the variables can contain a zero as zero is considered as empty by the empty() function
Actually I would do your other dropdown like this
<select name="result">
foreach (range('A', 'D') as $char) {
$sel = (isset($_GET['result']) && $char == $_GET['result']) ? 'selected="selected"' : '';
echo "<option $sel value='$char'>Answer $char</option>";
}
</select>

Form showing no option is selected error message when options are selected

I have the following form:
<form action="#" method="POST" enctype="multipart/form-data">
from:
<select name="age_from" id="age_a" onchange="checkages_a()">
<option value=""></option>
<?php
for($i = 17; $i <= 50; ++$i) {
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
}
?>
</select>
to:
<select name="age_to" id="age_b" onchange="checkages_b()">
<option value=""></option>
<?php
for($i = 18; $i <= 50; ++$i) {
echo "\t", '<option value="', $i, '">', $i, '</option>', "\n";
}
?>
</select>
<input type="radio" name="gender" value="male">Male</input> <br />
<input type="radio" name="gender" value="female">Female</input><br />
<input type="submit" class="btn btn-info"
name="submit_form" value="Click to start chat! " />
The way it works is that a user can select one of the two options from gender and age and search for a user. It is not required for both options to be completed to search.
However, if none of the options are selected, I want it to stay on the current page (`random_chat.php), and echo a message.
Here is my approach:
$refined_gender = isset($_POST['gender']) ? escape($_POST['gender']) : '';
$age_from = isset($_POST['age_from']) ? escape($_POST['age_from']) : '';
$age_to = isset($_POST['age_to']) ? escape($_POST['age_to']) : '';
if (isset($_POST['submit_form'])){
if (empty ($refined_gender) || empty($age_from) || empty ($age_to)) {
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
} else {
// search user
}
Currently, when I have both search options empty, the message echos, which is good. But the message also echos when I am searching for a male user, or have age not empty, which shouldn't happen.
you need to change || to &&, (Some other changes are also stated as comment):-
<form method="POST" enctype="multipart/form-data"> <!-- remove action ='#' -->
from:
<select name="age_from" id="age_a" onchange="checkages_a()">
<option value=""></option>
<?php
for($i = 17; $i <= 50; ++$i) {
if(isset($_POST['age_from']) && $_POST['age_from'] == $i){ // to make selected value to be selected even after form submit
echo "\t", '<option value="', $i. '" selected ="selected">', $i, '</option>', "\n";
}else{
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
}
}
?>
</select>
to:
<select name="age_to" id="age_b" onchange="checkages_b()">
<option value=""></option>
<?php
for($i = 18; $i <= 50; ++$i) {
if(isset($_POST['age_to']) && $_POST['age_to'] == $i){// to make selected value to be selected even after form submit
echo "\t", '<option value="', $i. '" selected ="selected">', $i, '</option>', "\n";
}else{
echo "\t", '<option value="', $i. '">', $i, '</option>', "\n";
}
}
?>
</select>
<input type="radio" name="gender" value="male">Male</input> <br />
<input type="radio" name="gender" value="female">Female</input><br />
<input type="submit" class="btn btn-info" name="submit_form" value="Click to start chat! " />
<form>
<?php
$refined_gender = isset($_POST['gender']) ? $_POST['gender'] : ''; // escape gives me undefined function error at my end so i removed it, but if it is working at your end thhen keep it up as it is.
$age_from = isset($_POST['age_from']) ? $_POST['age_from'] : '';
$age_to = isset($_POST['age_to']) ? $_POST['age_to'] : '';
if (empty ($refined_gender) && empty($age_from) && empty ($age_to)) { // check always with form posted value not submit button value and also cange || to &&
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
} else {
// search user
}
?>
The if condition does not reflect what you want fix it like this
if (empty ($refined_gender) && empty($age_from) && empty ($age_to)) {
echo "<div class='no_user'><p> Please apply at least one of the filters above. </p> </div>";
} else { // search user }

While filling in a form only update the form do not reload the whole page

I have got the following form:
<form action="" method="get" target="_self">
<select name="Category" class="dropmenu" id="Category"onchange="this.form.submit()">
<option value="">Any</option>
<option value="Keyboard"<?php if ($_GET['Category']=="Keyboard") {echo "selected='selected'"; } ?>>Keyboard</option>
<option value="Piano"<?php if ($_GET['Category']=="Piano") {echo "selected='selected'"; } ?>>Piano</option>
</select>
<select name='Manufacturer' class="dropmenu" onChange="this.form.submit()" >
<?php
echo '<option value="">Any</option>';
while ($row = mysql_fetch_array($RS_Search1)) {
$selected = $_GET['Manufacturer'] == $row['Manufacturer'] ? 'selected' : '';
echo '<option '.$selected.'>' . $row['Manufacturer'] . '</option>';
} ?> </select>
<select name="Color" class="dropmenu" onChange="this.form.submit()">
<?php
echo '<option value="">Any</option>';
while ($Color = mysql_fetch_array($RS_Search2)) {
$selected2 = $_GET['Color'] == $Color['Color'] ? 'selected' : '';
echo '<option '.$selected2.'>' . $Color['Color'] . '</option>';
} ?> </form>
Second form to submit
<form action="search2.php" method="get"> </select><input name="Category" type="hidden" value="<?php echo $_GET['Category']; ?>">
<input name="Manufacturer" type="hidden" value="<?php echo $_GET['Manufacturer']; ?>">
<input name="Color" type="hidden" value="<?php echo $_GET['Color']; ?>">
<select name="price" class="dropmenu">
<?php
echo '<option value="">Any</option>';
while ($Price = mysql_fetch_array($RS_Price)) {
$selected3 = $_GET['price_range'] == $Price['price_range'] ? 'selected' : '';
echo '<option '.$selected3.'>' . $Price['price_range']. '</option>';
} ?>
</select><input name="Search2" type="submit" id="Search2" value="Submit">
</form>
As you can see the option values are pulled from a db, after the first value is chosen. What the form does at the minute is reload the whole page. Is there a way to just reload the form in stead of reloading the whole page. I need to keep the values that are submitted on change of a option value to be able to fill in the next drop down menu.
Any help welcome.

Retain dynamically selected value from drop down list, but still be able to see list after submit

I have got the following form:
<div id="Quick_find_container">
<form action="" method="post" target="_self">
<div id="qcategory_1">Product</div>
<div id="qcategory">
<select name="Category" class="dropmenu" id="Category"onchange="this.form.submit()">
<option value="">Any</option>
<option value="Keyboard"<?php if ($_POST['Category']=="Keyboard") {echo "selected='selected'"; } ?>>Keyboard</option>
<option value="Piano"<?php if ($_POST['Category']=="Piano") {echo "selected='selected'"; } ?>>Piano</option>
</select>
</div>
<div id="qbrand_1">Brand</div>
<div id="qbrand"><select name='Manufacturer' onchange="this.form.submit()">
<?php
echo '<option value="">Any</option>';
$value = $_POST['Manufacturer'];
while ($row = mysql_fetch_array($RS_Search)) {
echo "<option value=" . $row['Manufacturer'] . " ' . (selected == $value ? ' selected' : '') . '>" . $row['Manufacturer'] . "</option>";
}
?>
</select>
</div>
<div id="qsubmit">
<input name="Search2" type="submit" id="Search2" value="Submit">
</div>
</form>
</div>
<?php echo $_POST['Category']; ?>
<?php echo $_POST['Manufacturer']; ?>
Echo post Category and Manufacturer are purely to see what it is submitting.
My problem is the second drop down menu. I would like to display after something is selected what the selected value was. At the moment it just jumps back to the default value Any even though the out put of POST_[Manufacturer'] is correct.
Is there a way to display the selected value like in the first drop down menu? I still would like to keep the values to choose from the database as well. But just display the selected value.
Any help welcome
Your line that echos the option has a lot of problems with quotes. This will fix it:
while ($row = mysql_fetch_array($RS_Search)) {
$selected = $_POST['Manufacturer'] == $row['Manufacturer'] ? 'selected' : '';
echo '<option ' . $selected . ' value="' . htmlspecialchars($row['Manufacturer']) . '">' . htmlspecialchars($row['Manufacturer']) . '</option>';
}
Side note: remember to use htmlspecialchars() or similar when outputting variables into HTML to prevent XSS.
In theory the code you have there might work altough i'd had made it like this
while ($row = mysql_fetch_array($RS_Search)) {
$selected = ($_POST['Manufacturer'] == $row['Manufacturer']) ? 'selected="selected":'';
echo '<option '.$selected.'>' . $row['Manufacturer'] . '</option>";
}?>
You do not need value if you have the same thing within the tag. Also you were missing some quote marks in your tag. Learn to simplify your code so it's easier to understand.

Save selected option from a dropdown box

<form method="post">
<select name="select_employee" id="select_employee">
<?php $allemp=$this->AllEmployees; ?>
<option selected value="">Select an employee..</option>
<?php foreach($allemp as $row){ echo "<option value=".$row[ 'Id']. ">".$row[ 'Etunimi']. " - ". $row[ 'Sukunimi']. "</option>"; } ?>
</select>
<input type="hidden" name="send" value="namesent">
<input type="submit" value="submit" id="button">
</form>
<br/>
<br/>
When I submit this form, I stay on the same page, but I want a the same time to keep the option selected previously option visible.. How can I do that?
I tried:
document.getElementById('select_employee').value = "<?php echo $_GET['select_employee'];?>";
But it did not work..
here:
foreach($allemp as $row){
echo "<option " . (isset($_POST['select_employee']) && $_POST['select_employee'] == $row['Id'] ? ' selected ' : '') . " value=".$row['Id'].">".$row['Etunimi']." - ".$row['Sukunimi']."</option>";
}
Try this :
$selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';
echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
added a line above echo options ($selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';)
Added '.$selected.' in side options.
Your code becomes : Copy paste below code instead of yours
<form method="post">
<select name="select_employee" id="select_employee">
<?php $allemp=$this->AllEmployees; ?>
<option selected value="">Select an employee..</option>
<?php foreach($allemp as $row){
$selected = ($row['Id'] == $_REQUEST['select_employee'])?'selected="selected"':'';
echo '<option '.$selected.' value="'.$row['Id'].'">'.$row['Etunimi'].' - '. $row['Sukunimi'].'</option>';
} ?>
</select>
<input type="hidden" name="send" value="namesent">
<input type="submit" value="submit" id="button">
</form>
<br/>
<br/>

Categories