How to make a select list option selected - php

I receive a variable from the mysql database named $option which in this case could be '1' or '2'. What is the correct PHP code to add the 'selected="selected"' code to $option 1 the corresponding option in the list?
<select name="select-list">
<option value='1'>option 1</option>
<option value='2'>option 1</option>
</select>

<select name="select-list">
<option value='1' <?php if ($option == '1') { echo "selected"; } ?>>option 1</option>
<option value='2' <?php if ($option == '2') { echo "selected"; } ?>">option 1</option>
</select>
like this?

This is how I would do it.
<?php
$select-list = (str) $_POST['select-list']; //or however you retrieve this value
?>
<select name="select-list">
<option value='1' <?php if($select-list === "1"){ echo "selected"; }?>>option 1</option>
<option value='2' <?php if($select-list === "2"){ echo "selected"; }?>>option 1</option>
</select>

Just add the selected property to the item you want selected.
<select name="select-list">
<option value='1'>option 1</option>
<option value='2' <?php if ($option === '2') { echo "selected" } ?>>option 2</option>
</select>

Related

How to use a select tag with the selected option in the controller in codeignator?

I am using Codeigniter. In the controller, I have a select tag something like
$action='<select name="pp_fileStatus[]" class="form-control multipleselect wip_fileStatus" data-id="'.$row->bank_id.'">
<option value="" disabled selected>File Status</option>
<option value="1">Disbursed </option>
<option value="2">Files sumitted </option>
<option value="3">Pendency </option>
<option value="4">Approved</option>
<option value="5">Rejected</option>
<option value="6">Dropped</option>
</select>';
Now what I am doing is, I have to use an if condition to show the selected option. So I tried:
<option value="1"'if($row->status == "1"){ echo "selected"; }'>Disbursed </option>
but it is showing the wrong results.
Should I create something like this:
$action='<select name="pp_fileStatus[]" class="form-control multipleselect wip_fileStatus" data-id="'.$row->bank_id.'">
<option value="" disabled selected>File Status</option>
<option value="1"';
if($row->b_filestatus == "1"){ echo "selected"; }
$action='>Disbursed </option>
<!--more here-->
or should I use an altogether different method?
You can try like this
$action='<select name="pp_fileStatus[]" class="form-control multipleselect wip_fileStatus" data-id="'.$row->bank_id.'">
<option value="" disabled selected>File Status</option>
<option value="1"'. ($row->status == "1"?'selected':'').' >Disbursed </option>
<option value="2"'. ($row->status == "2"?'selected':'').'>Files sumitted </option>
<option value="3"'. ($row->status == "3"?'selected':'').'>Pendency </option>
<option value="4"'. ($row->status == "4"?'selected':'').'>Approved</option>
<option value="5"'. ($row->status == "5"?'selected':'').'>Rejected</option>
<option value="6"'. ($row->status == "6"?'selected':'').'>Dropped</option>
</select>';

How to set Dropdown List default value based on user selection

I have a dropdown list. However i want to set a default based on user selection. Therefore the default value is not consistent. What can i do to achieve this? I have set $country = $_POST['country']; as user selection.
<td>Country:</td>
<td colspan="2"><select name="country">
<option value="93">93-Afghanistan</option>
<option value="355">355-Albania</option>
<option value="213">213-Algeria</option>
<option value="1-684">1-684-American Samoa</option>
<option value="376">376-Andorra</option>
<option value="244">244-Angola</option>
<option value="1-264">1-264-Anguilla</option>
<option value="672">672-Antarctica</option>
<option value="1-268">1-268-Antigua and Barbuda</option>
<option value="54">54-Argentina</option>
<option value="374">374-Armenia</option>
<option value="297">297-Aruba</option>
<option value="61">61-Australia</option>
</select>
To add on. There are 200 over options (I never list all down) so i hope to get a convenience way to achieve this
You can use 'selected' for relevant option
<option value="93" <?php if($country==93) echo "selected" ?> >93-Afghanistan</option>
<option value="355" <?php if($country==355) echo "selected" ?> >355-Albania</option>
like this add if conditon for each option.
Try using session for it.
The php code:
session_start();
$_SESSION['selectedCountry'] = $_POST['country'];
Then you can use JQuery to make dropdown selected:
$(document).ready(function(){
$(function() {
$("#country").val("<?php echo $_SESSION['selectedCountry'];?>");
});
})
Please try out this ..
HTML :-
<td>Country:</td>
<td colspan="2"><select id="country" name="country">
<option value="93">93-Afghanistan</option>
<option value="355">355-Albania</option>
<option value="213">213-Algeria</option>
<option value="1-684">1-684-American Samoa</option>
<option value="376">376-Andorra</option>
<option value="244">244-Angola</option>
<option value="1-264">1-264-Anguilla</option>
<option value="672">672-Antarctica</option>
<option value="1-268">1-268-Antigua and Barbuda</option>
<option value="54">54-Argentina</option>
<option value="374">374-Armenia</option>
<option value="297">297-Aruba</option>
<option value="61">61-Australia</option>
</select>
</td>
JQuery :-
$(document).ready(function(){
$(function() {
$("#country").val("<?php echo $_POST['country'];?>");
});
})
Try this...
<?php
(isset($_POST["country"])) ? $country = $_POST["country"] : $country=93;
?>
<form action='stackover.php' method='POST'>
<select id="country" name="country">
<option <?php if ($country == 93 ) echo 'selected' ; ?> value="93">93-Afghanistan</option>
<option <?php if ($country == 355 ) echo 'selected' ; ?> value="355">355-Albania</option>
<option <?php if ($country == 213 ) echo 'selected' ; ?> value="213">213-Algeria</option>
</select>
<input type="submit" value="Submit">
</form>

Error setting the selected value from a select list

I have a HTML select list where one can chose an option.
<select name='price' id='price'>
<option value='' >Select....</option>
<option value='0-50,000' selected>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
When this list is submitted via a HTML submit button, this list shows again in another page. Now, I want the option the user selected to be new selected value. I am doing this:
<select name='price' id='price'>
<option value='{$_POST['price']}'>{$_POST['price']}</option>
<option value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
But values are appearing twice. The option the user selected is shown as the selected and still appears in the list. For example, we now have something like this:
0-50,000 (this is the selected value)
0-50,000
50,000-100,000
100,000-150,000
150,000-200,000
200,000 and above
How do I solve this?
In other page, make sure the selected option has selected="selected"
<select name='price' id='price'>\
<option value='' >Select....</option>
<option selected="selected" value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
try this
/**
* Takes To values (First for option value Second for value to be compare)
* #param Srting $option stores Option Value String
* #param String $value Stores to be compare
* #return String
* #access public
*/
function selectBoxSelection($option, $value) {
if ($option == $value) {
return "selected";
}
}
<select name='price' id='price'>
<option value='0-50,000' <?php echo selectBoxSelection('0-50,000', $_POST['price']);?> >0-50,000</option>
<option value='50,000-100,000' <?php echo selectBoxSelection('50,000-100,000', $_POST['price']);?> >50,000-100,000</option>
<option value='100,000-150,000' <?php echo selectBoxSelection('100,000-150,000', $_POST['price']);?> >100,000-150,000</option>
<option value='150,000-200,000' <?php echo selectBoxSelection('150,000-200,000', $_POST['price']);?> >150,000-200,000</option>
<option value='200,000 and above' <?php echo selectBoxSelection('200,000 and above', $_POST['price']);?> >200,000 and above</option>
<option value='see all' <?php echo selectBoxSelection('see all', $_POST['price']);?> >See All</option>
</select>
instead of your code,
<select name='price' id='price'>
<option value='{$_POST['price']}'>{$_POST['price']}</option>
<option value='0-50,000'>0-50,000</option>
<option value='50,000-100,000'>50,000-100,000</option>
<option value='100,000-150,000'>100,000-150,000</option>
<option value='150,000-200,000'>150,000-200,000</option>
<option value='200,000 and above'>200,000 and above</option>
<option value='see all'>See All</option>
</select>
try like this,
<select name='price' id='price'>
<option value='' >Select....</option>
<?php foreach ($arr as $key=>$value)
{
if($key == $_POST['price'])
{
echo "<option selected value=$key>$value</option>";
unset($arr[$key]);
}
else
echo "<option value=$key>$value</option>";
}?>
</select>
This will do it :
<form name="myform" method="post" action="testsel.php">
<select name='price' id='price'>
<option value='' >Select....</option>
<option value='0-50,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="0-50,000") print "selected"; ?>>0-50,000</option>
<option value='50,000-100,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="50,000-100,000") print "selected"; ?>>50,000-100,000</option>
<option value='100,000-150,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="100,000-150,000") print "selected"; ?>>100,000-150,000</option>
<option value='150,000-200,000' <?php if(isset($_POST["price"]) && $_POST["price"]=="150,000-200,000") print "selected"; ?>>150,000-200,000</option>
<option value='200,000 and above' <?php if(isset($_POST["price"]) && $_POST["price"]=="200,000 and above") print "selected"; ?>>200,000 and above</option>
<option value='see all' <?php if(isset($_POST["price"]) && $_POST["price"]=="see all") print "selected"; ?>>See All</option>
</select>
<input type="submit" value="submit">
</form>
<?php
if(isset($_POST["price"]))
print $_POST["price"];
?>
Save this file as "testsel.php" and view. It will serve the purpose.
<select name='price' id='price'>
<option value='' >Selec</option>
<option value='0-50,000'<?php if($_POST['price']=='0-50,000'){echo "selected==selected";}?>>0-50,000</option>
<option value='50,000-100,000'<?php if($_POST['price']=='50,000-100,000'){echo "selected==selected";}?>>50,000-100,000</option>
<option value='100,000-150,000'<?php if($_POST['price']=='100,000-150,000'){echo "selected==selected";}?>>100,000-150,000</option>
<option value='150,000-200,000'<?php if($_POST['price']=='150,000-200,000'){echo "selected==selected";}?>>150,000-200,000</option>
<option value='200,000 and above'<?php if($_POST['price']=='200,000 and above'){echo "selected==selected";}?>>200,000 and above</option>
<option value='see all'<?php if($_POST['price']=='see all'){echo "selected==selected";}?>>See All</option>
</select>

how to set multiple option value in multiple select box after refresh the page

please tell me how to set multiple option value in multiple select box after refreshing the page.
I have dynamic select box with different id like following
<select id="select_1" name="select_1"/>
<option value="a" selected="selected">Data 1</option>
<option value="b">Data 2</option>
</select>
<select id="select_2" name="select_2"/>
<option value="a">Data 1</option>
<option value="b" selected="selected">Data 2</option>
</select>
<select id="select_3" name="select_3"/>
<option value="a" selected="selected">Data 1</option>
<option value="b">Data 2</option>
</select>
I want to selected all option values of all selectbox after refreshing the page with php and ajax.
I'm not sure how your data is populated and presented, but here's an example:
<?foreach($selects as $select):?>
<?$selected = ($select->Selected === true) ? 'selected="selected"' :'';?>
<select id="<?=$select->Id?>" name="<?=$select->Name?>"/>
<?foreach($select->Options as $option):?>
<option value="<?=$option->Value?>"<?=$selected;?>><?=$option->Text?></option>
<?endforeach;?>
<?endforeach;?>
Or
foreach($selects as $select)
{
$selected = ($select->Selected === true) ? 'selected="selected"' :'';
echo "<select id=\"{$select->Id}\" name=\"{$select->Name}\"/>\n";
foreach($select->Options as $option)
{
echo "<option value=\"{$option->Value}\"{$selected}>{$option->Text}</option>\n";
}
echo "</select>\n";
}

Populating the HTML drop-down list with a value that is in the MySQL databse and making it 'selected' while there are other options in the list

Let's say I have an HTML dropdown menu as,
<p>General Medical History:
<select name="otherproblem">
<option value="Nothing">Nothing</option>
<option value="Allergy: Penicillin">Allergy: Penicillin</option>
<option value="Aspirin">Aspirin</option>
<option value="Erythromycin">Erythromycin</option>
<option value="Latex or Rubber Products">Latex or Rubber Products</option>
<option value="Codeine">Codeine</option>
<option value="Tetracycline">Tetracycline</option>
<option value="Germicides/Pesticides, Foods">Germicides/Pesticides, Foods</option>
<option value="Other">Other</option>
<option value="Asthma">Asthma</option>
<option value="Bleeding Disorders">Bleeding Disorders</option>
<option value="Diabetes">Diabetes</option>
<option value="Epilepsy">Epilepsy</option>
<option value="GI disorders">GI disorders</option>
<option value="Heart disease">Heart disease</option>
<option value="Hepatitis">Hepatitis</option>
<option value="Jaundice">Jaundice</option>
<option value="Liver disease">Liver disease</option>
<option value="Neoplasm">Neoplasm</option>
<option value="Psychiatric Problems">Psychiatric Problems</option>
<option value="Respiratory diseases">Respiratory diseases</option>
<option value="Rheumatic fever">Rheumatic fever</option>
</select>
</p>
By selecting one of the options, I am adding the particular value to the database as a VARCHAR.
Now I am calling them back, let's say, for an edit, so I need the entered data back to the same form, where I could retrieve all the other things to text boxes. But I have no idea how to get the value from the db and make it selected in this dropdown list. All I could do is saving the retrieved value from db to a variable named, $otherproblem
You have two options, one is to load the list along the lines of:
<?
$otherproblem = "Erythromycin";
?>
<p>General Medical History:
<select name="otherproblem">
<?
$result = mysql_query("query to get the list of options");
while($row=mysql_fetch_array($result)){
if(strcmp($row['Problem'],$otherproblem)===0){ $selected = 'selected="selected"'; } else { $selected = ""; }
?>
<option <?=$other?> value="<?=$row['Problem'];?>"><?=$row['Problem'];?></option>
<? } ?>
Dirty, but it will work with the way you have it setup currently. You can also rig it by simply making the item select at the top of the list, and then the full list like this:
<?
$otherproblem = "Erythromycin";
?>
<p>General Medical History:
<select name="otherproblem">
<option selected="selected" value="<?=$otherproblem;?>"><?=$otherproblem;?></option>
<?
$result = mysql_query("query to get the list of options");
while($row=mysql_fetch_array($result)){
?>
<option value="<?=$row['Problem'];?>"><?=$row['Problem'];?></option>
<? } ?>
There is much neater ways to do this, but without seeing how you are pulling the data, these two options will work in any case.
You would modify each option tag to look like this:
<option value="Nothing" <?=$otherproblem === 'Nothing' ? 'selected="selected"' : ''?>>Nothing</option>
Each one should compare the value from the database to the value of the option tag and add the selected attribute if they are equal.
Often, you will be loading the list of options from the database as well. You'll loop through the list and construct the <option> tags on the fly. As you do that, you compare the option value being created with the value retrieved from the earlier submission, and, if they match, you add the "selected" attribute to the option tag.
Here is a way to do this:
<?php
$otherproblem = "Aspirin";
$dropdown = array(
"Nothing",
"Allergy: Penicillin",
"Aspirin",
"Erythromycin",
"Latex or Rubber Products",
"Codeine",
"Tetracycline",
"Germicides/Pesticides, Foods",
"Other",
"Asthma",
"Bleeding Disorders",
"Diabetes",
"Epilepsy",
"GI disorders",
"Heart disease",
"Hepatitis",
"Jaundice",
"Liver disease",
"Neoplasm",
"Psychiatric Problems",
"Respiratory diseases",
"Rheumatic fever"
);
?>
<p>General Medical History:
<select name="otherproblem">
<?php foreach ($dropdown as $name): ?>
<option <?php if ($otherproblem == $name) print('selected="selected"');?> value="<?php print($name);?>"><?php print($name);?></option>
<?php endforeach; ?>
</select>
</p>
Way 2:
<?php
$otherproblem = "Erythromycin";
?>
<p>General Medical History:
<select name="otherproblem">
<option <?php if($otherproblem == "Nothing"){print('selected="selected"');}?> value="Nothing">Nothing</option>
<option <?php if($otherproblem == "Allergy: Penicillin"){print('selected="selected"');}?> value="Allergy: Penicillin">Allergy: Penicillin</option>
<option <?php if($otherproblem == "Aspirin"){print('selected="selected"');}?> value="Aspirin">Aspirin</option>
<option <?php if($otherproblem == "Erythromycin"){print('selected="selected"');}?> value="Erythromycin">Erythromycin</option>
<option <?php if($otherproblem == "Latex or Rubber Products"){print('selected="selected"');}?> value="Latex or Rubber Products">Latex or Rubber Products</option>
<option <?php if($otherproblem == "Codeine"){print('selected="selected"');}?> value="Codeine">Codeine</option>
<option <?php if($otherproblem == "Tetracycline"){print('selected="selected"');}?> value="Tetracycline">Tetracycline</option>
<option <?php if($otherproblem == "Germicides/Pesticides, Foods"){print('selected="selected"');}?> value="Germicides/Pesticides, Foods">Germicides/Pesticides, Foods</option>
<option <?php if($otherproblem == "Other"){print('selected="selected"');}?> value="Other">Other</option>
<option <?php if($otherproblem == "Asthma"){print('selected="selected"');}?> value="Asthma">Asthma</option>
<option <?php if($otherproblem == "Bleeding Disorders"){print('selected="selected"');}?> value="Bleeding Disorders">Bleeding Disorders</option>
<option <?php if($otherproblem == "Diabetes"){print('selected="selected"');}?> value="Diabetes">Diabetes</option>
<option <?php if($otherproblem == "Epilepsy"){print('selected="selected"');}?> value="Epilepsy">Epilepsy</option>
<option <?php if($otherproblem == "GI disorders"){print('selected="selected"');}?> value="GI disorders">GI disorders</option>
<option <?php if($otherproblem == "Heart disease"){print('selected="selected"');}?> value="Heart disease">Heart disease</option>
<option <?php if($otherproblem == "Hepatitis"){print('selected="selected"');}?> value="Hepatitis">Hepatitis</option>
<option <?php if($otherproblem == "Jaundice"){print('selected="selected"');}?> value="Jaundice">Jaundice</option>
<option <?php if($otherproblem == "Liver disease"){print('selected="selected"');}?> value="Liver disease">Liver disease</option>
<option <?php if($otherproblem == "Neoplasm"){print('selected="selected"');}?> value="Neoplasm">Neoplasm</option>
<option <?php if($otherproblem == "Psychiatric Problems"){print('selected="selected"');}?> value="Psychiatric Problems">Psychiatric Problems</option>
<option <?php if($otherproblem == "Respiratory diseases"){print('selected="selected"');}?> value="Respiratory diseases">Respiratory diseases</option>
<option <?php if($otherproblem == "Rheumatic fever"){print('selected="selected"');}?> value="Rheumatic fever">Rheumatic fever</option>
</select>
</p>

Categories