How to retain dropdown selected option after submit / page refresh - php

I want to have dropdown selected option selected after submit/refresh page. I search for other examples but no one works on my code.
How to retain selected value after submit/refresh with this code?
<form name="test" action="test.php?id=<?php echo $row["id"]; ?>" method="post">
<select id="test_email" name="test_email">
<option value="">...select</option>
<?php
$sql2 = "SELECT test_id, test_email FROM test WHERE status='Act'";
$res = $db->query($sql2);
if ($res->num_rows > 0) {
while($row1 = mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?>"><?php echo $row1['test_id'];?></option>
<?php
}
}
?>
</select>
Solved this way:
Because I could not pass $_POST['test_email'] with <?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?>
Because I use explode on $_POST['test_email']
I make one more post (insert in db) $test_temp = trim(mysqli_real_escape_string($db, $_POST['test_email'])); to have my dropdown value (whole string) in db.
I added hidden input in my form <input id="test_temp" type="hidden" name="test_temp" value="<?php echo $row["test_temp"]; ?>">
And changed select option line to <option value="<?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>"<?php if($row1['test_email'] . '-' . $row1['test_id'] == $row['test_temp']) echo ' selected="selected"' ; ?>><?php echo $row1['test_id'];?></option>.
Perhaps this could be simpler but it works like a charm.
#roberto06: Thank you for your guidance.

You need to add the selected="selected" attribute on the option matching your $_POST value.
You might also want to concatenate <?php echo $row1['test_email'];?>-<?php echo $row1['test_id'];?> into <?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>, but that's up to you.
Here's a solution (I assumed that $POST['test_email'] has to be equal to $row1['test_email'] . '-' . $row1['test_id'] in order for the condition to be matched). I'm using a shorthand if...else here, but you could also use a classic if :
<form name="test" action="test.php?id=<?php echo $row["id"]; ?>" method="post">
<select id="test_email" name="test_email">
<option value="">...select</option>
<?php
$sql2 = "SELECT test_id, test_email FROM test WHERE status='Act'";
$res = $db->query($sql2);
if ($res->num_rows > 0) {
while($row1 = mysqli_fetch_array($res)) {
?>
<option value="<?php echo $row1['test_email'] . '-' . $row1['test_id']; ?>"<?php echo (isset($_POST['test_email']) && $row1['test_email'] . '-' . $row1['test_id'] == $_POST['test_email']) ? ' selected="selected"' : ''; ?>><?php echo $row1['test_id'];?></option>
<?php
}
}
?>
</select>
</form>

Related

How to select only id instead of print name in select options?

<select id="rightsid" name="rightsid">
<?php
while($row=mysql_fetch_array($list)) {
$admno=$row["adm_no"];
$name=$row["name"];
?>
<option id="<?php echo $admno;?>" name="<?php echo $admno;?>"><?php echo $admno;?> <?php echo $name;?></option>
<?php
}
?>
<input type="submit" value="Show Rights">
</select>
This is my code. When I take the value of select to next page, it is taking both name and admno but I want to take only admno but here in dropdown list. I want to display both but want to carry only one. How to do this?
Within option value, write $admo. Try this:
<?php
while ($row=mysql_fetch_array($list)) {
$admno = $row["adm_no"];
$name = $row["name"];
echo '<option id = "'.$admno.'" name = "'.$admno.'" value = "'.$admno.'" >' . $admno . $name . '</option>';
}
?>
Use value in option as below
<select id="rightsid" name="rightsid">
<?php
while($row=mysql_fetch_array($list)) {
$admno=$row["adm_no"];
$name=$row["name"];
?>
<option value="<?php echo $admno;?>" id="<?php echo $admno;?>" name="<?php echo $admno;?>"><?php echo $admno;?> <?php echo $name;?></option>
<?php
}
?>
</select>
<option id="<?php echo $admno;?>" value="<?php echo $admno;?>"><?php echo $name;?></option>

PHP getting value from a dynamic select list

I've got a dynamic select list, which is generated from MySQL. I've got no problem listing them, but I cant seem to get it's value when I submit the form. Here's my script for the Select:
<div class="form-group">
<select class="form-control" id="make" name="make">
<option value="">Make</option>
<?php
if ($result->num_rows > 0) {
$x = 1;
While($row = $result->fetch_assoc()) {
?>
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
<?php
$x = $x + 1;
}
}?>
</select>
</div>
And here's the script to get it's value:
if ($_POST["submit"]) {
$make = $_POST['make'];
when I do an echo for $make, I don't get anything at all. What went wrong? All help appreciated. Thanks
You forgot to echo $row['fmake'] and quotation marks mistake.
change this
<option value="<?php $row[fmake];?>" <?php if($_POST['make'] == $row[fmake]) echo 'selected="selected" '; ?>><?php echo $row[fmake];?></option>
by
<option value="<?php echo $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>
shouldn't it be $row['fmake'] instead of $row[fmake]
<option value="<?php $row['fmake'];?>" <?php if($_POST['make'] == $row['fmake']) echo 'selected="selected" '; ?>><?php echo $row['fmake'];?></option>
Believe you're missing an echo.
value="<?php $row[fmake];?>"
Should be:
value="<?php echo $row[fmake];?>"

PHP - Edit page , How to set default dropdownlist that have option from MySQL?

I know only this method. this method is assume that you know the values in all <option>
<select name="agama" id="agama">
<option value="Islam"<?php if ($rows['agama'] === 'Islam') echo ' selected="selected"'>Islam</option>
<option value="Khatolik"<?php if ($rows['agama'] === 'Khatolik') echo ' selected="selected"'>Khatolik</option>
<option value="Protestan"<?php if ($rows['agama'] === 'Protestan') echo ' selected="selected"'>Protestan</option>
<option value="Hindu"<?php if ($rows['agama'] === 'Hindu') echo ' selected="selected"'>Hindu</option>
<option value="Buddha"<?php if ($rows['agama'] === 'Buddha') echo ' selected="selected"'>Buddha</option>
<option value="Lain-Lain"<?php if ($rows['agama'] === 'Lain-Lain') echo ' selected="selected"'>Lain-Lain</option>
</select>
.... the above code is example from other people not mine.
but My case is the <option> is select from database too.
I have 2 table, oav_event and oav_album
the oav_album has foreign key (event_id) from oav_event table
I want to check if row['event_id'] from oav_album table is equal to option value (from oav_event table) if true, then set selected="selected"
while($row = mysqli_fetch_assoc($result)) { ?>
<option value="<?php echo $row['event_id']; ?>" >Event: <?php echo $row['event_date']; ?> </option>
<?php } ?>
the option will change depend on change in database table, so I don't know the value in option. How should I do?
<select name="event_id">
<?php
$sql = "SELECT * FROM oav_event";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)) {
$selected = "";
if($row['event_id'] == $Yourmatchvalue)
{
$selected = "selected";
}
?>
<option value="<?php echo $row['event_id']; ?>" selected="<?php echo $selected; ?>" >Event: <?php echo $row['event_date']; ?> </option>
<?php } ?>
</select>
may this helps your. you need to replace $Yourmatchvalue variable with your variable.
You can use $_GET as the method on your form and pass the id of the record using it:
while($row = mysqli_fetch_assoc($result)) {
if (!empty($_GET['event_id']) && $row['event_id'] == $_GET['event_id']) {
$selected = 'selected = "selected"';
} else {
$selected = '';
}
echo '<option '.$selected.' value="'.$row["event_id"].'">'.$row["event_date"].'</option>';
}
Here is a solution,
$selected_value = 'Hindu'; // This will come from database
Change option tag with this
<option value="<?php echo $row['event_id']; ?>" <?php echo ($row['event_id'] == $selected_value) ? 'selected="selected"' : ''; ?> >Event: <?php echo $row['event_date']; ?> </option>
Create one function which will create options list like this:
function setDropdownValue($selectQueue_list,$selectedVal)
{
$queueVal = '';
$selectQueue_list_res=$db->query($selectQueue_list);
while($selectQueue_list_res_row=$db->fetchByAssoc($selectQueue_list_res))
{
$val = $selectQueue_list_res_row['id'];
$name = $selectQueue_list_res_row['name'];
if($val == $selectedVal)
{
$queueVal .= "<option value='$val' selected='selected' label='$name'>$name</option>";
}
else
{
$queueVal .= "<option value='$val' label='$name'>$name</option>";
}
}
return $queueVal;
}
Then create a query:
$get_value_query="SELECT id, name FROM table";
$dropdown_selected_value = !empty($dropdown_value) ? $dropdown_value: ''; // Pass value which you want to be selected in dropdown
Then call this function:
$dropdown_options = setDropdownValue($get_value_query, $dropdown_selected_value);
Later when you get dropdown options in $dropdown_options, use jquery to populate the dropdown, like this:
$('#dropdown_select_id').html("$dropdown_options");
Give it a try, and let me know.

code for fetching value to select option

I have select option. this option has multiple value from database. I want to update something from database, this value i want to update is exist on the select option I have.
this is my option code
$id = $_GET['update'];
$query = mysql_query("SELECT * FROM transaction where id = '$id'") or die ("could not search");
$count = mysql_num_rows($query);
while ($rows = mysql_fetch_array($query)) {
$id = $rows['id'];
$tranid = $rows['tranid'];
$trandate = $rows['trandate'];
$patientid = $rows['patientid'];
$transactiontype = $rows['transactiontype'];
$trandescription = $rows['trandescription'];
$tranquantity = $rows['tranquantity'];
$tranunitprice = $rows['tranunitprice'];
$tranamount =$rows['tranamount'];
$gettrandescription = $rows['trandescription'];
}
}
if (isset($_POST['selectmedicine'])) {
$gettrandescription=$_POST['medicineid'];
}
if (isset($_POST['selectroomquantity'])) {
$tranquantity=$_POST['quantity'];
}
?>
<script type="text/javascript">
$('#collapseone').collapseone({
toggle: true
});
<option value="<?php echo $trandescription; ?>" <?php if($trandescription==$gettrandescription){ echo "selected";} ?> ><?php echo $gettrandescription; ?></option>
<option value="<?php echo $tranquantity; ?>" <?php if($tranquantity==$tranquantity){ echo "selected";} ?> ><?php echo $tranquantity; ?></option>
this has value results, but i cant fetch this value to my existing select option.
If you want to "make that variable an array" as aldrin27 said, append [] to the name attribute of the select tag. The selected value of the option with name selectroomquantity will be available in your script as $_POST["selectroomquantity"] (this is the varible).
<select multiple name="selectroomquantity[]">
<option value="...">...</option>
</select>
It should only be necessary if multiple options can be selected however.
Also, there seems to be a typo:
<?php if($tranquantity==$tranquantity)
That check will always return true. It should probably be:
<?php if($tranquantity==$gettranquantity)
hi all i just got the code on how to fecth the value to dropdown. actually i made a wrong word. pre-selected is the right one, sorry for that. here;s the working code.
<select name="selectmedicine" class="form-control col-sm-4" id="medicinename">
<option id="0" style="width:100px"></option>
<?php
$medicine = mysql_query("SELECT * FROM medicine");
while ($row = mysql_fetch_array($medicine)) {
echo '<option id="' . $row['medicinename'] . '"';
echo ' value="' . $row['medicineid'] . '"';
if($row['medicinename'] == $trandescription) {
echo ' selected="selected"';
}
echo '>';
echo $row['medicinename'];
echo '</option>';
}
?>
</select>
thanks everyone, whos trying to help me on this. actually this is my five revised question sorry for that. and finally i got the right one.

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.

Categories