Remember dropdown item OR select if GET - php

I currently have this code to remember the last selected item on post.
<select tabindex="1" name="system_customer">
<option value="Choose">- Choose a Customer -</option>
<?PHP while ($row = mysql_fetch_array($result_get_users))
{
echo "<option value='".$row['id']."'";
echo $system_customer == $row['id'] ? 'selected="selected"' : '';
echo ">".$row['firstname']." ".$row['lastname']."</option>";
} ?>>
</select>
On load (before submitting) the list defaults to "Choose". What I would like to do, is if the URL has "userid=1" to select user 1, or if it is "userid=2" then select user 2, but keep the code to remember the last selected.
Thank you.
PS - cannot use mysqli or PDO due to the software I am writing a module for. It is hard coded and encrypted.

Add this above your code -
$system_customer = "";
if (!empty($_GET['userid'])) {
$system_customer = $_GET['userid'];
}
Then -
<select tabindex="1" name="system_customer">
<option value="Choose">- Choose a Customer -</option>
<?PHP
while ($row = mysql_fetch_array($result_get_users)) {
echo "<option value='".$row['id']."'";
echo $system_customer == $row['id'] ? 'selected' : '';
echo ">".$row['firstname']." ".$row['lastname']."</option>";
}
?>
</select>

You can use session or cookie
for exp :
<?PHP
Session_start();
if ( isset ( $_POST['system_customer'] ) ) {
$_SESSION['SELECTED_USER_ID'] = $_POST['system_customer'];
?>
and you haave to make some changes in you dropdown list
<select tabindex="1" name="system_customer">
<option value="Choose">- Choose a Customer -</option>
<?PHP while ($row = mysql_fetch_array($result_get_users)) {
echo "<option value='".$row['id']."'";
echo $_SESSION['SELECTED_USER_ID'] == $row['id'] ? 'selected="selected"' : '';
echo ">".$row['firstname']." ".$row['lastname']."</option>";
}
?>>
</select>
in you select form you have to check if session id is equal to row if then put selected attribiut for that option .
but if you want to save details for long time it's better to use cookie or databases you can't do it in another cases .

Related

isset($_POST['tag-name']) is set but $_POST['tag-name'] return empty when selecting dynamic list

I has created dynamic list in php.
<select name="student" >
<option selected disable class="hideoption">Student Name</option>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo<option value='".$row['path']."'>'".$row['studentName']."'</option>";
}
?>
</select>
When I'm selected the list it give empty string. But when I'm not selected anything it return the first value which is Student Name. I tried to get the value from this line.
if(isset($_POST['student'])){
$selectedName = $_POST['student'];
}
I also try to checked whether the isset is set or empty. The isset is set but its empty.
This is the only solution Google provided.Ideas?
Edit
I have solved the problem. The explanation on the answer below.
At first I thought $_POST['student'] returning the text inside the <option> tag and I was wrong. I learnt that it return the value which is should be set in <option> attribute, So I just set the value to $row['studentName'], because $row['path'] returned empty string. Correct me if what I said is wrong. So basically, the code looks like below.
<select name="student" >
<option selected disable class="hideoption">Student Name</option>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo<option value='".$row['studentName']."'>'".$row['studentName']."'</option>";
}
?>
</select>
Thanks for all your responses. Your responses made me think what should I do.
Use below code:
<select name="student" id="student" >
<option selected disable class="hideoption">Student Name</option>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
echo "<option name='".$row['studentName']."' value='".$row['path']."'>'".$row['studentName']."'</option>";
}?>
</select>
You should be use this line isset($_POST['studentName']) && $selected = $_POST['studentName'] == $row['studentName'] ? 'selected = "selected"' : null; for selected option
<select name="student" >
<option selected disable class="hideoption">Student Name</option>
<?php
while ($row = mysqli_fetch_array($result, MYSQLI_BOTH)){
$selected = isset($_POST['studentName']) && $_POST['studentName'] == $row['studentName'] ? 'selected = "selected"' : null;
echo "<option ".$selected." value='".$row['path']."'>".$row['studentName']."</option>";
}
?>
</select>
#dongcheng This is the same code but with better alignments. So you can see your code clearly.
<select name="student">
<option selected disable class="hideoption">Student Name</option>
<?php
while($row = mysqli_fetch_array($result, MYSQLI_BOTH)) {
echo '<option value="' . $row['path'] . '">' . $row['studentName'] . '</option>';
}
?>
</select>
use
$selectedStudent = !empty($_POST['student']) ? $_POST['student'] : '';
if ($selectedStudent) {
// your code
}

Show specific information from mysql to dropdownbox in php html.

These is my code to get the information from the database. So status is the one where in my database would be either Interested or Not Interested.
while($row= mysqli_fetch_array($result))
{
$ID =$row['Particulars_ID'];
$name = $row['Name'];
$number =$row['Number'];
$status =$row['Status'];
$remarks =$row['Remarks'];
}
I would like to echo out the value Not Interested if my database shows that this person is not interested. However from my below code, it always show Interested no matter which person i click on.
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested'>Not Interested</option>
</select><br>";
first - you do not need the comma in the select, second - ensure that this is the only element with the id of status, third - simply check the $status value in each option and echo selected if it is.
echo "<select name = 'status' id = 'status'>
<option value='Interested'";
if($status == "Interested"){echo " selected";}
echo">Interested</option>
<option value='Not Interested' ";
if($status == "Not Interested"){echo " selected";}
echo">Not Interested</option>
</select><br>";
since the default is the first option Interested, then if ($status === "Not Interested") set the option attribute selected
<?php
if ($status === "Not Interested") $selected = "selected";
else $selected = "";
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested' $selected>Not Interested</option>
</select><br>";
Change this
echo "<select name = 'status', id = status>
<option value='Interested'>Interested</option>
<option value='Not Interested'>Not Interested</option>
</select><br>";
to this
// Set the selected attributes based on the value of status
$interested = ($status === 'Interested') ? ' selected' : '';
$notInterested = ($interested === '') ? ' selected' : '';
echo <<< SELECT
<select name="status" id="status">
<option$interested>Interested</option>
<option$notInterested>Not Interested</option>
</select>
SELECT;
You don't need the value attribute if the value is the same as the text.
If there were more than two options, I recommend a loop like this:
<?php foreach ($options as $o) : ?>
<option<?php if ($o === $optionValue) ?> selected<?php endif ?>><?= optionValue ?></option>
<?php endforeach ?>

Select form - use last posted value if blank

Sorry in advance for the novice question here...
I currently have my first value as a disabled and defaulted "Select" option which then changes to the selected option when a selection is made.
However if the user submits again without reselecting, the value defaults back because the post is blank. Therefore is there a way to use the previous value if so?
<select name="test_select" style="width: 110px">
<option disabled="disabled" selected="selected">
<?php
if(!empty($_POST['test_select'])){
echo $_POST[test_select'];}
else
echo "Select Option"; ?>
</option>
<?php $sql = mysql_query("SELECT test FROM test_settings");
while ($row = mysql_fetch_array($sql)){
?><option><?php echo $row['test']; ?></option><?php }?>
</select>
Thanks in advance,
Dan
I suppose that problem is that forms are not sending disabled values.
I would edit code as following:
<select name="test_select" style="width: 110px">
<?php
if (empty($_POST['test_select']))
echo '<option selected="selected">Select Option</option>';
$sql = mysql_query("SELECT test FROM test_settings");
while ($row = mysql_fetch_array($sql)){
$selected = isset($_POST['test_select']) && $row['test'] == $_POST['test_select']
? ' selected="selected"'
: '';
echo '<option'.$selected.'>'.$row['test'].'</option>';
?>
</select>

How to show the selected item name in the drop down list?

I have created a drop down list which is working perfectly fine, its fetching data from database and showing it in the drop-down list. The problem is that I am unable to identify that where to use 'selected'attribute in the select tag. Right now whatever the field I select it opens it, but in the drop down list it shows the first given name. I also tried to use 'selected' attribute, but it was showing the last item name in the drop-down list.
Kindly check it and guide me how to use 'selected' attribute in the loop.
<?php
//Drop Down List
$sub_query = "select * from sub_categories where category_id=$category_id ";
if (!$sub_query_run = mysql_query($sub_query))
{
echo mysql_error();
}
else
{
echo "<select name='menu1' id='menu1' >
<option value='#'> All</option> ";
while ($sub_query_fetch = mysql_fetch_array($sub_query_run))
{
//$sub_query_fetch = mysql_fetch_array($sub_query_run);
$sub_category_id2 = $sub_query_fetch['sub_category_id'];
$sub_category_name = $sub_query_fetch['sub_category_name'];
echo "<option value='earings2.php?sub_category_id=$sub_category_id2' >"
.htmlspecialchars($sub_category_name= $sub_query_fetch['sub_category_name']).
"</option>";
}
}
follow this example..
<select name="cate" id="cate" class="reginput" >
<option value="">Select Category</option>
<?php $s2="select * from tbl_category order by cate_name";
$q2=mysql_query($s2);
while($rw2=mysql_fetch_array($q2)) {
?>
<option value="<?php echo $rw2['id']; ?>"<?php if($rw2['id']==$row['cate_id']) echo 'selected="selected"'; ?>><?php echo $rw2['cate_name']; ?></option><?php } ?>
</select>
You have to add a condition for the selected item.
echo "<option value='earings2.php?sub_category_id=$sub_category_id2'";
if ($sub_category_id2 == $MATCHING_CATEGORY_ID) echo " selected";
echo ">".htmlspecialchars($sub_category_name= $sub_query_fetch['sub_category_name'])."</option>";
Where $MATCHING_CATEGORY_ID is the category id that will be selected.
<option value="#" selected>ALL</option>
"selected" should be included in option tag
Try it like,
// get the category id from request parameter
$sc_id=isset($_REQUEST['sub_category_id']) ? $_REQUEST['sub_category_id'] : "";
while ($sub_query_fetch= mysql_fetch_array($sub_query_run))
{
//$sub_query_fetch= mysql_fetch_array($sub_query_run);
$sub_category_id2= $sub_query_fetch['sub_category_id'];
$sub_category_name= $sub_query_fetch['sub_category_name'];
$sel='';
if($sc_id==$sub_category_id2)// get the selected item
$sel='selected="selected"';
echo "<option value='earings2.php?sub_category_id=$sub_category_id2' ".$sel." >"
.htmlspecialchars($sub_category_name= $sub_query_fetch['sub_category_name']).
"</option>";
}
you need to use select in <option> tag
try this
while ($sub_query_fetch= mysql_fetch_array($sub_query_run))
{
//$sub_query_fetch= mysql_fetch_array($sub_query_run);
$sub_category_id2= $sub_query_fetch['sub_category_id'];
$sub_category_name= $sub_query_fetch['sub_category_name'];
$selected = ($isSelected == $sub_category_id2) ? 'selected' : ''; should be your selected condition fetch from db
echo "<option ".$selected."
value='earings2.php?sub_category_id=$sub_category_id2' >"
.htmlspecialchars($sub_category_name= $sub_query_fetch['sub_category_name']).
"</option>";
}

display from database to dropdown list

I have stored value from drop down list to database. I want to echo the same value to be displayed in that drop down list in my edit form. how can I achieve that in php?
Region
<select name="stf_region">
<option>Select</option>
<option value="1">MDU</option>
<option value="2">TMM</option>
</select>
i have stored in database using value of selection
but i dont know to display that value in same drop down
Use something like this:
<?
$sql = "SELECT id, description FROM dropDownTable";
$rs = mysql_query($sql);
?>
<select name="dropDown">
<option value="-1">Please select...</option>
<? while ($obj = mysql_fetch_object($rs)) { ?>
<option value="<?= $obj->id; ?>" <? if ($data['downDown'] == $obj->id) echo "SELECTED"; ?>>
<?= $obj->description; ?>
</option>
<? } ?>
</select>
Please note $data needs to be set as an associative array containing attributes of the entity that is being edited. This code is flexible because in the case of a form where a user may have submitted an incomplete form $data could be set to the $_POST variable and so all entered fields can be included without the user needing to re-specify fields they previously filled in. This basically means your form template, inserting an entry and editing an entry can be the same!
Well you could do like this
$query = "select id, label from lookup_table";
$result = mysql_query($query);
$html = "<select name='yourname'><option value="">Please select...</option>";
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$html .= "<option value='$row[id]'>$row[label]</option>";
}
$html = "</select>";
echo ($html);//Display the select in the page
If you're able to get db data into array, you can work with them like this:
<?php
$options = array('label 1' => 'value 1', 'label 2' => 'value 2');
echo "<select name=somename>";
foreach($options as $key => $value){
echo "<option value=" . $value . ">" . $key . "</option>";
}
echo "</select>";
?>
i used the COOKIE to match the value that should be selected when it comes to edit the form.
<?php
while($result_row=mysql_fetch_array($result)){
if ( $_COOKIE['MY_COOKIE'] == $result_row[importance_level_id )
{
echo "<option SELECTED=\"SELECTED\" value=$result_row[importance_level_id]>$result_row[importance_level]</option>";
}
else
{
echo "<option value=$result_row[importance_level_id]>$result_row[importance_level]</option>";
}
?>
Assuming you are ok with selecting the value out of the database into a PHP variable (let's say $region) I think you are just after
Region
<select name="stf_region">
<option>Select</option>
<option value="1"<?php echo ($region == '1') ? ' selected="selected"' : ''; ?>>MDU</option>
<option value="2"<?php echo ($region == '2') ? ' selected="selected"' : ''; ?>>TMM</option>
</select>
This is the most concise answer to the question that I think you are asking. However this is a very specific answer and assumes that your dropdown values are hard-coded and won't really be changing.
If you want a more flexible setup that retrieves the values of the dropdown from the database you are looking for something more along the lines of what has been suggested by Gordon Murray Dent above
<div class="form-group has-success col-md-6">
<label class="control-label" for="state-success">Select Buyer</label>
<select id="state-success" class="form-control ">
<?php
$sql="SELECT full_name FROM `new_customer`";
$data=mysqli_query($dbcon,$sql);
?>
<option>Select byer...</option>
<?php while($row1=mysqli_fetch_array($data)){?>
<option value="<?php echo $row1['full_name'];?>"><?php echo $row1['full_name'];?></option>
<?php } ?>
</select>

Categories