I have select option in my page or drop down list,my problem is how can i set value
in my select option and this value is from the database,
here is my code.
<select name="status" value="<?php echo $status; ?>" >
<option value=""></option>
<option value="public">public</option>
<option value="private">private</option>
</select>
on top of my html tag
if(isset($_GET['status']))
{
$status = $_GET['status'];
$sstatus="select .......";
foreach($db->query($sstatus) as $rows)
{
$status= $rows['status'];
......
......
......
}
}
I tried modify my code and it's seems working but my problem is that it has 2 same
value in the drop down list.
<select name="status" >
<option selected="selected"><?php echo $status; ?></option>
<option value="public">public</option>
<option value="private">private</option>
</select>
it will show like this in my drop down list,my question for this is this is the right way
in displaying the values that comes from the database.
public
public
private
<select name="status" >
<option value=""></option>
<option value="public"<?php if (isset($status) && $status === 'public') echo 'selected'; ?>>public</option>
<option value="private"<?php if (isset($status) && $status === 'private') echo 'selected'; ?>>private</option>
</select>
<?php $options = array('', 'public', 'private') ?>
<select name="status">
<?php foreach ($options as $option): ?>
<option value="<?php echo $option ?>" <?php echo isset($status) && $status == $option ? 'selected="selected"' : '' ?>><?php echo $option ?></option>
<?php endforeach ?>
</select>
Related
Is there any way to set the selected item in a drop down box using the following 'type' code?
<select selected="<?php print($row[month]); ?>"><option value="Janurary">January</option><option value="February">February</option><option value="March">March</option><option value="April">April</option></select>
The database holds a month.. and I want to allow on the edit page, them to choose this month.. but it to be pre-filled with their current setting?
You need to set the selected attribute of the correct option tag:
<option value="January" selected="selected">January</option>
Your PHP would look something like this:
<option value="January"<?=$row['month'] == 'January' ? ' selected="selected"' : '';?>>January</option>
I usually find it neater to create an array of values and loop through that to create a dropdown.
You mark the selected item on the <option> tag, not the <select> tag.
So your code should read something like this:
<select>
<option value="January"<?php if ($row[month] == 'January') echo ' selected="selected"'; ?>>January</option>
<option value="February"<?php if ($row[month] == 'February') echo ' selected="selected"'; ?>>February</option>
...
...
<option value="December"<?php if ($row[month] == 'December') echo ' selected="selected"'; ?>>December</option>
</select>
You can make this less repetitive by putting all the month names in an array and using a basic foreach over them.
You can use this method if you use a MySQL database:
include('sql_connect.php');
$result = mysql_query("SELECT * FROM users WHERE `id`!='".$user_id."'");
while ($row = mysql_fetch_array($result))
{
if ($_GET['to'] == $row['id'])
{
$selected = 'selected="selected"';
}
else
{
$selected = '';
}
echo('<option value="'.$row['id'].' '.$selected.'">'.$row['username'].' ('.$row['fname'].' '.substr($row['lname'],0,1).'.)</option>');
}
mysql_close($con);
It will compare if the user in $_GET['to'] is the same as $row['id'] in table, if yes, the $selected will be created. This was for a private messaging system...
Simple and easy to understand example by using ternary operators to set selected value in php
<?php $plan = array('1' => 'Green','2'=>'Red' ); ?>
<select class="form-control" title="Choose Plan">
<?php foreach ($plan as $id=> $value) { ?>
<option value="<?php echo $id;?>" <?php echo ($id== '2') ? ' selected="selected"' : '';?>><?php echo $value;?></option>
<?php } ?>
</select>
Its too old but I have to add my way as well :) because it is generic and useful especially when you are using static dropdown values.
function selectdCheck($value1,$value2)
{
if ($value1 == $value2)
{
echo 'selected="selected"';
} else
{
echo '';
}
return;
}
and in you dropdown options you can use this function like this and you can use this as many as you can because it fits with all of your select boxes/dropdowns
<option <?php selectdCheck($row[month],january); ?> value="january">january</option>
:) I hope this function help others
Simple way
<select class ="dropdownstyle" name="category" selected="<?php print($messageeditdetails[0]['category_id']); ?>">
<option value=""><?php echo "Select"; ?></option>
<?php foreach ($dropdowndetails as $dropdowndetails) { ?>
<option <?php if($messageeditdetails[0]['category_id'] == $dropdowndetails['id']) { ?> selected="<?php echo $dropdowndetails['id']; ?>" <?php } ?> value="<?php echo $dropdowndetails['id']; ?>"><?php echo $dropdowndetails['category_name']; ?></option>
<?php } ?>
</select>
This is the solution that I came up with...
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="select_month">
<?php
if (isset($_POST['select_month'])) {
if($_POST["select_month"] == "January"){
echo '<option value="January" selected="selected">January</option><option value="February">February</option>';
}
elseif($_POST["select_month"] == "February"){
echo '<option value="January">January</option><option value="February" selected="selected">February</option>';
}
}
else{
echo '<option value="January">January</option><option value="February">February</option>';
}
?>
</select>
<input name="submit_button" type="submit" value="Search Month">
</form>
A Simple Solution:
It work's for me
<div class="form-group">
<label for="mcategory">Select Category</label>
<select class="form-control" id="mcategory" name="mcategory" required>
<option value="">Please select category</option>
<?php foreach ($result_cat as $result): ?>
<option value="<?php echo $result['name'];?>"<?php
if($result['name']==$mcategory){
echo 'selected';
} ?>><?php echo $result['name']; ?></option>
}
<?php endforeach; ?>
</select>
</div>
Check this:
<select class="form-control" id="department" name="department" type="text">
<option value="medical-furniture" #if($list->department == "medical-furniture") selected #endif>Medical furniture</option>
<option value="medical-table" #if($list->department == "medical-table") selected #endif>Medical table</option>
<option value="service" #if($list->department == "service") selected #endif>Service</option>
</select>
My suggestion is to leverage the hidden/collapse attribute. Try with this example:
<select>
<option value="echo $row[month]" selected disabled hidden><? echo $row[month] ?></option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
</select>
in case of null for $row[month] the selected item is blank and with data, it would contain less codes for many options and always working for HTML5 and bootstrap etc...
If you have a big drop down. it's much easier to use jQuery with PHP.
This is how to do it:
<script>
$(document).ready(function () {
$('select[name="country"]').val('<?=$data[0]['Country']?>');
});
</script>
The easiest solution for the selected option in dropdown using PHP is following
<select class="form-control" name="currency_selling" required >
<option value="">Select Currency</option>
<option value="pkr" <?=$selected_currency == 'pkr' ? ' selected="selected"' : '';?> >PKR</option>
<option value="dollar" <?=$selected_currency == 'dollar' ? ' selected="selected"' : '';?> >USD</option>
<option value="pounds" <?=$selected_currency == 'pounds' ? ' selected="selected"' : '';?> >POUNDS</option>
<option value="dirham" <?=$selected_currency == 'dirham' ? ' selected="selected"' : '';?> >DRHM</option>
</select>
You can try this after select tag:
<option value="yes" selected>yes</option>
<option value="no">no</option>
Here I am listing all cars.customers want to compare car so they will select from this drop down. A person can select multiple cars. At the first time he is selecting 'Audi' and Saab' I will store it into data base next if he came I need to populate Saab and audi as select how I can do this using php
<select name="cars" multiple>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
Here is my code
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
?> <option value="<?php echo($entry['ID']); ?>" ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
Following code I tried $resources contain select cars
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<?PHP
if($carslist->num_rows() >0)
{
foreach($carslist->result_array() as $entry):
if($resources->num_rows() >0)
{
foreach($resources->result_array() as $car):
if($entry['ID'] == $employee['car_id'])
{
$select = 'selected="selected"';
}
else
{
$select = '';
}
endforeach;
}
?> <option value="<?php echo($entry['ID']); ?>" <?php echo $select;?> ><?php echo($entry['car_name']); ?></option>
<?php
endforeach;
}
?>
</select>
but it showing error
Here, try something like this, and see if it works:
Here is the controller:
<?php
function something(){
$data = array();
$data['cars'] = $this->some_model->some_function_to_return_cars_array();
$data['selected'] = $some_array_of_selected_cars();
$this->load->view('some_view', $data);
}
?>
And this is the view:
<select id="cars" class="multiselect" multiple="multiple" name="cars[]">
<option value="">Select:</option>
<?php
foreach( $cars as $key => $val ){
?>
<option value="<?php echo $val['some_id'] ?>"
<?php
if( in_array( $val['some_id'], $selected ) ) echo ' selected';
?>
><?php echo $val['some_name'] ?></option>
<?php
}
?>
</select>
I have a dropdown like the following.
<select>
<option value="Mr">Mr</option>
<option value="Dr">Dr</option>
<option value="Prof">Prof</option>
</select>
I am getting a value from data base in $selected_value variable. Based on this value I want to make one option from the above select to be selected.
Eg: If $selected_value = Mr, <option value="Mr" selected>Mr</option>
if $selected_value = Dr, <option value="Dr" selected>Dr</option>
update:
now when i am inspecting element i am getting like below.but not selecting Dr.but it is orking in w3schools try editor.
<select>
<option value="Mr">Mr</option>
<option value="Dr" selected="selected">Dr</option>
<option value="Prof">Prof</option>
</select>
update 2
see screen shot:
update3
now it works! added name for <select>
This will work for you problem
try this code
<select>
<option value="Mr" <?=($selected_value=="Mr") ? "selected" : ""?>>Mr</option>
<option value="Dr" <?=($selected_value=="Dr") ? "selected" : ""?>>Dr</option>
</select>
try this..
<option value="Mr" <?php if($selected_value == 'Mr') echo 'selected' ?>>Mr</option>
<option value="Dr" <?php if($selected_value == 'Dr') echo 'selected' ?>>Dr</option>
<option value="Prof" <?php if($selected_value == 'Prof') echo 'selected' ?>>Prof</option>
Or you can use by using jquery
<script>
$('select option[value="<?php echo $selected_value ?>"]').attr('selected','true')
</script>
As per your current HTML use the code below:
<select name="your_select_name">
<option <?php echo (($selected_value=="Mr")?"selected":"") ?> value="Mr">Mr</option>
<option <?php echo (($selected_value=="Dr")?"selected":"") ?> value="Mr">Dr</option>
<option <?php echo (($selected_value=="Prof")?"selected":"") ?> value="Mr">Prof</option>
</select>
If the value of this variable($selected_value) returns "Dr" then 2nd option will be selected. And also give a name of your select tag.
I changed my solution so it fits yours I hope.
UPDATE:
<select name="dropdownlist">
<?
$options = array("Mr", "Dr", "Prof");
foreach($options as $option){
if($_POST['dropdownlist'] == $option){
echo '<option selected="selected">' .$option. '</option>';
}else{
echo '<option>' .$option. '</option>';
}
}
?>
</select>
If you also have an array of your options you could create the option tags by looping through each one of them. In that loop you then can check if the $selected_value matches $option_value like so:
<select>
<?php foreach($options as $option_value => $option_displayName) : ?>
<option
value="<?php echo $option_value; ?>"
<?php echo $option_value == $selected_value ? 'selected' : ''; ?>>
<?php echo $option_displayName; ?>
</option>
<?php endforeach; ?>
</select>
To do what you want you'd have to build the select dynamically like the following example:
<?php
$selects = array(
array('name' => 'Mr', 'value' => 'Mr'),
array('name' => 'Dr', 'value' => 'Dr'),
array('name' => 'Prof', 'value' => 'Prof'),
);
$selected_option = 'Dr';
echo '<select>';
foreach($selects as $select) {
if($select['value'] == $selected_option) {
echo '<option value="'.$select['value'].'" selected>' .$select['name']. '</option>';
} else {
echo '<option value="'.$select['value'].'">' .$select['name']. '</option>';
}
}
echo '</select>';
?>
Which outputs:
<select>
<option value="Mr">Mr</option>
<option value="Dr" selected>Dr</option>
<option value="Prof">Prof</option>
</select>
Example
Try as follows .
<select name="select_options">
<option value="Mr">Mr</option>
<option value="Dr">Dr</option>
<option value="Prof">Prof</option>
</select>
After hit submit button. Then you will get value in $_POST['select_options']
There are many ways to achieve what you're doing. I can think of two straight ways to do this.
The first is ugly:
Insert in each option a php tag and check if value is selected:
<select>
<option <?php if ($selected_value == 'Mr') echo 'selected'; ?> value="Mr">Mr</option>
<option <?php if ($selected_value == 'Dr') echo 'selected'; ?> value="Dr">Dr</option>
<option <?php if ($selected_value == 'Prof') echo 'selected'; ?> value="Prof">Prof</option>
</select>
Otherwise, I would personally write a little helper
function generateSelect(array $entries, $selected)
{
$ret = '<select>'
foreach ($entries as $entry) {
$ret .= '<option';
if ($entry == $selected) {
$ret .= ' selected';
}
$ret .= ' value="'.$entry.'"';
$ret .= '>'.$entry.'</option'>;
}
return $ret;
}
It is just an example and its functionality could be expanded. It SHOULD work, but I haven't tried it myself (wrote it quickly)
You have mistaken syntax
<select>
<option value="Mr">Mr</option>
<option value="Dr" selected="selected">Dr</option>
<option value="Prof">Prof</option>
</select>
this will work
<select>
<option value="Mr">Mr</option>
<option value="Dr" selected>Dr</option>
<option value="Prof">Prof</option>
</select>
The below code show the retrived data from database and it also repeat the same option
For example: if you select employed from dropdown, it repeat the same employed twice in dropdown
<select class="txtbox" name="currentstatus">
<option value="<?php echo "$currentstatus"; ?>"><?php echo "$currentstatus"; ?></option>
<option value="Employed"<?php if(isset($_POST["currentstatus"]) && $_POST["currentstatus"] == "Employed") echo "selected"; ?>>Employed</option>
<option value="Unemployed"<?php if(isset($_POST["currentstatus"]) && $_POST["currentstatus"] == "Unemployed") echo "selected"; ?>>Unemployed</option>
</select>
What about this? This displays two options and selects one according to the database or by $_POST:
<select class="txtbox" name="currentstatus">
<?php foreach (array('Employed', 'Unemployed') as $status): ?>
<option value="<?php echo $status; ?>"<?php if ($currentstatus == $status || (isset($_POST["currentstatus"]) && $_POST["currentstatus"] == $status)) echo ' selected'?>><?php echo $status; ?></option>
<?php endforeach; ?>
</select>
please, i'm somewhat new with php bear with me.
I have a selectbox...
<select name="criteria">
<option value="1">Student ID</option>
<option value="2">Firstname </option>
<option value="3">Lastname</option>
<option value="6">All</option>
</select>
<input name="searchvalue" type="text">
<input name="search" type="submit" value="Search">
When i search with a particular option, after the post request, the selected option usually defaults to Student ID. But i want it to still remain the option i searched with. Any suggestion would be appreciated.
To be honest I'd adjust your select code just a little bit to allow for a bit more dynamic editing..
<?php
$options = array('1'=>'Student ID', '2'=>'Firstname', '3'=>'Lastname', '6'=>'All');
?>
<select name="criteria">
<?php
foreach($options as $key=>$value){
$addtItem = '';
if(isset($_POST['criteria']) && $_POST['criteria']==$key){
$addtItem = 'selected="selected"';
}
echo('<option '.addtItem.' value="'.key.'">'.$value.'</option>');
}
?>
</select>
This way you can quickly add more values without a ton of repeat code..
<select name="criteria">
<option value="1" <?php if ($_POST["criteria"] == 1) echo "SELECTED"; ?>>Student ID</option>
<option value="2" <?php if ($_POST["criteria"] == 2) echo "SELECTED"; ?>>Firstname </option>
<option value="3" <?php if ($_POST["criteria"] == 3) echo "SELECTED"; ?>>Lastname</option>
<option value="6" <?php if ($_POST["criteria"] == 6) echo "SELECTED"; ?>>All</option>
</select>
Really ugly, but that works.
You need to check the $_POST array and select the corresponding select item
<?php
$criteria = isset($_POST['criteria']) ? $_POST['criteria']: 0;
?>
<select name="criteria">
<option value="1" <?php echo ($criteria == '1') ? 'selected="selected"': ''; ?>>Student ID</option>
<option value="2" <?php echo ($criteria == '2') ? 'selected="selected"': ''; ?>>Firstname </option>
<option value="3" <?php echo ($criteria == '3') ? 'selected="selected"': ''; ?>>Lastname</option>
<option value="6" <?php echo ($criteria == '6') ? 'selected="selected"': ''; ?>>All</option>
</select>