dropdown list with 2 values - php

I need to create a dropdownlist which has two values "OK" and "NOK" one should be selected by default based of an value of $Status. The user should than be able to change the selection or let the default one. How can i manage this? What i have tried:
<select name="Status">
<option value="OK">OK selected="selected"</option>
<option value="NOK">NOK</option>
</select>

<select>
<option value="OK" <?php if($Status == "OK") echo 'selected="selected"';?>>OK</option>
<option value="NOK" <?php if($Status == "NOK") echo 'selected="selected"';?>>NOK</option>
</select>
You can add an id and/or name attribute to the select as you like.

Related

Read value from MySQL and compare which option is selected from drop-down list

<select name="title">
<selected value="<?php echo $title; ?>"><?php echo $title; ?></selected>
<option value="Mrs">Mrs.</option>
<option value="Ms">Ms.</option>
<option value="Mr">Mr.</option>
<option value="Dr">Dr.</option>
</select>
I am trying to read a value from a column Title in a MySQL database, which is suppose to read the value, whether it be Mr., Ms., Mrs., then compare it with the values in the drop-down list. It then lets the user select another title to then update the one stored in MySQL database.
I am creating a user profile. So when the user logs in and navigates to the view to edit a profile, he/she should be presented with a drop-down list containing the title he/she selected when registered. Then if he/she wants to they can change the title in the drop-down list and press the update button and it should now update to the new title in the database.
Change your <select> dropdown list in the following way,
<select name="title">
<option value="Mrs"<?php if($title == "Mrs"){ echo " selected='selected'"; } ?>>Mrs.</option>
<option value="Ms"<?php if($title == "Ms"){ echo " selected='selected'"; } ?>>Ms.</option>
<option value="Mr"<?php if($title == "Mr"){ echo " selected='selected'"; } ?>>Mr.</option>
<option value="Dr"<?php if($title == "Dr"){ echo " selected='selected'"; } ?>>Dr.</option>
</select>
There is no selected HTML tag. Use the selected attribute of the option tag:
selected
If present, this Boolean attribute indicates that the option is initially selected. If the <option> element is the descendant of a <select> element whose multiple attribute is not set, only one single <option> of this <select> element may have the selected attribute.1
So consider this example from the Examples section of the Mozilla Developer Network page for <select>:
<!-- The second value will be selected initially -->
<select name="select">
<option value="value1">Value 1</option>
<option value="value2" selected>Value 2</option>
<option value="value3">Value 3</option>
</select>
Your example code can be updated similarly:
<select name="title">
<option value="Mrs" <?php if($title=="Mrs"){ echo "selected"; } ?>>Mrs.</option>
<option value="Ms" <?php if($title=="Ms"){ echo "selected"; } ?>>Ms.</option>
<option value="Mr" <?php if($title=="Mr"){ echo "selected"; } ?>>Mr.</option>
<option value="Dr" <?php if($title =="Dr"){ echo "selected"; } ?>>Dr.</option>
</select>
A simpler way to do this would be to process the names first, using array_reduce():
<?php
$title = 'Dr';
$names = array('Mrs','Ms','Mr','Dr');
$options = array_reduce($names,function($carry,$name) use ($title) {
return $carry .= '<option value="'.$name.'"'.($title == $name?' selected':'').'>'.$name.'.</option>';
});
?>
<select name="title">
<?php echo $options;?>
</select>
See it in action in this playground example.
1https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option

trying to display previously selected value in dropdown list php

I have made some code to create a dropdown in a webpage and you can select among 2 currency values namely USD and SGD. I have been able to get the value for the currency field while entering the data in the database. But while trying to edit the entry in the database, I am able to use $_POST to get all entries to display except the value of currency. I would ideally want to display the previously selected value on the dropdown. As of now the drop down on the edit page just displays the default "Please Choose" and doesn't show the previously selected value. any help would be greatly appreciated.
Code :
<select id="currency" name="currency" placehoder="Currency">
<option value='' disabled selected style='display:none;'>Please Choose</option>
<option value="SGD">SGD</option>
<option value="USD">USD</option>
</select>
And I am trying to somehow display the previously read value that exists in the database and display that instead of the "Please Choose" so that while editing I don't have to re-select the currency value.
Supposing you stored in $currency the value from DB:
<select id="currency" name="currency" placehoder="Currency">
<option value='' disabled style='display:none;'>Please Choose</option>
<option value="SGD"<?php echo $currency == "SGD" ? " selected" : ""; ?>>SGD</option>
<option value="USD"<?php echo $currency == "USD" ? " selected" : ""; ?>>USD</option>
</select>
Use PHP to Solve it..
<select name="select_limitby" onChange="frm_sub()">
<?php if($_SESSION[select_limitby]!='') { ?>
<option value="<?php echo $_SESSION[select_limitby]; ?>" <?php if($_POST[select_limitby]=='$_SESSION[select_limitby]') {?> selected="selected" <?php }?>><?php echo $_SESSION[select_limitby]; ?></option>
<?php } ?>
<option value="">Default</option>
<option value="9" <?php if($_POST[select_limitby]=='9') {?> selected="selected" <?php }?>>9</option>
<option value="12" <?php if($_POST[select_limitby]=='12') {?> selected="selected" <?php }?>>12</option>
<option value="15" <?php if($_POST[select_limitby]=='15') {?> selected="selected" <?php }?> >15</option>
</select>
USE session if it does not work...
otherwise you can leave session...

How to keep showing selected option from drop down list?

I have a drop down list where I select options
<form action="" method="POST" class="styled-select">
<select name="seasons" onchange='this.form.submit()'>
<option value="">Select a Season</option>
<option value="1">2002/2003</option>
<option value="2">2003/2004</option>
<option value="3">2004/2005</option>
<option value="4">2005/2006</option>
<option value="5">2006/2007</option>
<option value="6">2007/2008</option>
<option value="7">2008/2009</option>
<option value="8">2009/2010</option>
<option value="9">2010/2011</option>
<option value="10">2011/2012</option>
<option value="11">2012/2013</option>
<option value="12">2013/2014</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
You can see the list here footystat
I am using the following PHP
if(isset($_POST['seasons'])){ $seasonette = $_POST['seasons']; }
if(isset($_POST['year'])){ $yearette = $_POST['year']; }
if(isset($_POST['comp'])){ $competitionette = $_POST['comp']; }
if(isset($_POST['which'])){ $whichette = $_POST['which']; }
When I select something from the list, I want selected item in the list to continue showing. At the moment when I select (for example) 2013/2014, it will show the results but the drop down menu goes back to its original state instead of showing 2013/2014.
Get Option value selected when it gets posted value, like this,
<option value="1" <?php if(isset($_POST['seasons']) && $_POST['seasons'] == '1'){ ?> selected="selected" <?php } ?>>2002/2003</option>
Set value like this for each option
You can set the "selected" property to the option , just like you set a value !
<option value="8" selected>2009/2010</option>
Use a if statement in PHP to determine which one should be selected.
Thats because the page refreshes.
On page load check if there is post variable than match the value with each option's HTML and write selected attribute.
The shorter way is
<option value="1" <?php echo $_POST['seasons']==1?"selected":""; ?>2002/2003</option>

set select <option> as selected in form

I have a form wich inserts some data in a mysql database.
This form contain a select and some options with their respective values like
<select name="car_type">
<option value="sport">Sports car</option>
<option value="van">Van</option>
<option value="large">Large family sedan</option>
<option value="small">Small city car</option>
</select>
The form can also be used to update a car's details in the database, it does so by loading the values from the database and fills the form automatically but I am stuck at making the <option> in the select, selected by default based on the value already set in the DB.
So if the user chooses to edit a car, lets say a car that already has Sports type filled in the DB, I want the form to automatically set the <option value="sport">Sports car</option> as selected, <option selected="selected" value="sport">Sports car</option>. By not doing this, the user has to choose again the type every time he submits the form, otherwise the first <option> and its value (sport) is sent by POST.
I am able to retrieve the value from the database by using $data['type'] but I did not find the exact php code to set the selected <option> to that in the database, can you guys help ?
Although the code looks messy, you can do something like this:
<select name="car_type">
<option value="sport" <?php if($data['type']=='sport') echo "selected='selected'"; ?> >Sports car</option>
<option value="van" <?php if($data['type']=='van') echo "selected='selected'"; ?>>Van</option>
<option value="large" <?php if($data['type']=='large') echo "selected='selected'"; ?>>Large family sedan</option>
<option value="small" <?php if($data['type']=='small') echo "selected='selected'"; ?>>Small city car</option>
</select>
<option value="large" <?php echo ($dbvalue=="large") ? "selected=\"selected\"" : "" ;?>>Large family sedan</option>
Work out the sql query bit yourself and replace my $dbvalue with the column data from your db
If you're trying to mark the active car type I would do it like this:
foreach ($car_types as $car_type){
echo '<option value="'.$car_type.'" '.($data['type']==$car_type?'selected':'').'>'.$car_type.'</option>';
}

how to select a value from a listbox?

I am having a list box like this ,the list box is populated from the database
<td bgcolor="#FFFFCC">
<select name="listbox" id="FriendmailId" size="3" >
<option value="0">Select User From List</option>
<? foreach($searchfriend as $row)
{?>
<option value=""><?=$row['dEmailID'];?></option>
<? } ?>
</select>
</td>
The values are listed in the list box ....but the problem is when i select a item it is highted but not really selected why it is so
You need to add selected="selected" for the option value you want selected:
<option value="" selected="selected"><?=$row['dEmailID'];?></option>
In a loop, this is usually done when a certain condition is met for an option to be selected (of course only one option can be selected at a time)
<? foreach($searchfriend as $row)
if (condition to select a specific option value) // when true
{
{?>
<option value="" selected="selected"><?=$row['dEmailID'];?></option>
<? } else { ?>
<option value=""><?=$row['dEmailID'];?></option>
<? }} ?>
Note: If you don't specify selected="selected" for an option, by default, first option value is selected.

Categories