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>
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>
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>