This is the part of an edit page in PHP
Here I am displaying the country which is already selected by the user and this code works fine.
And in this case there are3 countries only.
If I have a lot of countries, then I have to validate with each country and the no: of lines of code will increase.
Is there any other option to acheive this?
Select Country:
<select name="country">
<?php if($country == "india") { ?>
<option value="">-Select Country-</option>
<option value="india" selected>india</option>
<option value="us">us</option>
<option value="uk">uk</option>
<?php } else if($country == "us"){ ?>
<option value="">-Select Country-</option>
<option value="india">india</option>
<option value="us" selected>us</option>
<option value="uk">uk</option>
<?php } else{ ?>
<option value="">-Select Country-</option>
<option value="india">india</option>
<option value="us">us</option>
<option value="uk" selected>uk</option>
<?php } ?>
</select>
load the countries into array
$countries = array("india", "us", "uk");
then,
<select name="country">
<?php
foreach($countries as $c)
{
echo "<option ";
if ($c == $country) echo "selected";
echo ">$c</option>";
}
?>
</select>
is this your update county page then do below
first get the selected country from database let
$row['country']='india';
and your country Array is
$countryArray = array ('india','us','uk');
then do below
<select name="country">
<option value="">-Select Country-</option>
<?php foreach( $countryArray as $country) { ?>
<option value="india" <?php if($row['country']===$country ) { ?> selected ="selected" <? } ?><?php echo $country?></option>
<?php } ?>
</select>
Hm, I believe you would be looking for something like this:
<?php
$selected = "india";
$countries = Array(
["india"] => "India",
["us"] => "United States",
["uk"] => "United Kindom",
);
foreach( $countries as $id => $country ){
echo '<option value="'.$id.'"', $selected == $id ? ' selected' : '' ,'>' . $country . '</option>';
}
?>
Excuse me, I have not coded in PHP in a while, but I believe this is correct.
Related
I have an issue for which I am not able to think of a solution.
I have an array of data which I get from the server. I have to pre populate a form which already have some values.
<select id="select1" name="Salutation" class="field-size-top-large" >
<option value="">-- please select -- </option>
<option value="Mr.">Mr.</option>
<option value="Ms.">Ms.</option>
</select>
I am getting an array
array(11) {
[0]=> string(4) "Ms."
[1]=> string(8) "Y"
}
I have to check if [0]=>'Ms.' is there in select list, if there it should be selected else default is selected. Any ideas?
use this
$options = array('Mr.', 'Ms.');
$data = array(0 => 'Ms.', 1 => 'Y');
<select id="select1" name="Salutation" class="field-size-top-large">
<option value="">-- please select --</option>
<?php foreach ($options as $option): ?>
<option value="<?php echo $option?>" <?php echo $option == $data[0] ? 'selected' : ''; ?>><?php echo $option; ?></option>
<?php endforeach; ?>
</select>
Try this:
$options = ['Mr.', 'Ms.']; // the default options
$data = [0 => 'Ms.', 1 => 'Y']; // data from server
<select id="select1" name="Salutation" class="field-size-top-large">
<option value="">-- please select --</option>
<?php foreach ($options as $option): ?>
<option value="<?php echo $option; ?>" <?php echo in_array($option, $data) ? 'selected' : ''; ?>><?php echo $option; ?></option>
<?php endforeach; ?>
</select>
My soultion:
the data array I am getting from the server i.e 0=>'Ms.' is the only value I can check for the rest are different fields
<select id="select1" name="Salutation" class="field-size-top-large" >
<option value="">-- please select --</option>
<option value="Mr." <?php echo 'Mr.'== $data[0]? 'selected':''?>> Mr. </option>
<option value="Ms."<?php echo 'Ms.'== $data[0]? 'selected':''?>>Ms.</option>
</select>
Hi i am storing countries and states in database i need to edit and update it in database but when i click on edit option iam not able to fetch the record from database.Here is my code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://lab.iamrohit.in/js/location.js"></script>
Country:<select name="country" class="countries" id="country" value="<?php echo $row['country'];?>">
<option >Select Country</option>
</select><br/>
State:<select name="state" class="states" id="state" value="<?php echo $row['state'];?>">
<option >Select State</option>
</select><br/>
If i click on console the value is printing but iam not able to display in frontend.Can anyone help me regarding this.
please use this code.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://lab.iamrohit.in/js/location.js"></script>
Country:<select name="country" class="countries" id="country">
<option value="<?php echo $row['country'];?>"><?php echo $row['country'];?></option>
<option >Select Country</option>
</select><br/>
State:<select name="state" class="states" id="state">
<option value="<?php echo $row['state'];?>"><?php echo $row['state'];?></option>
<option >Select State</option>
</select><br/>
There were few things which were wrong.
Option should be inside select box
select value should be based on database condition
Thanks
Amit
Select box don't contain value attribute. You are going by wrong way.
You can use select box by this way:-
<select name="country">
<option value="country1" <?php if ($row['country'] == 'country1') { echo ' selected="selected"'; } ?>>country1</option>
<option value="country2" <?php if ($row['country'] == 'country2') { echo ' selected="selected"'; } ?>>country2</option>
<option value="country3" <?php if ($row['country'] == 'country3') { echo ' selected="selected"'; } ?>>country3</option>
</select>
<select name="state">
<option value="state1" <?php if ($row['state'] == 'state1') { echo ' selected="selected"'; } ?>>state1</option>
<option value="state2" <?php if ($row['state'] == 'state2') { echo ' selected="selected"'; } ?>>state2</option>
<option value="state3" <?php if ($row['state'] == 'state3') { echo ' selected="selected"'; } ?>>state3</option>
</select>
Hope it will help you :)
You can't assign value attribute to Select Box.So you can use Like this:
<select name="country">
<option value="country" <?php if ($row['country'] == 'country') { echo ' selected="selected"'; } ?>>country</option>
<select name="state">
<option value="state12" <?php if ($row['state'] == 'state12') { echo ' selected="selected"'; } ?>>state12</option>
I have an HTML form that a user can use to edit a row in a mysql database, the PHP script queries the database and then displays the current data in form. I have this working fine for text fields with something like:
Correct Answer:<input type="text" name="answer" value="<?php echo $row['CorrectAnswer']; ?>"><br>
and simple radio set up like:
<?php
if ($row['Hazardous'] == "no"){
?>
Hazardous?:<input type="radio" name="hazardous" value="yes">Yes
<input type="radio" name="hazardous" value="no" checked="checked">No<br>
<?php
}else{
?>
Hazardous?:<input type="radio" name="hazardous" value="yes" checked="checked">Yes
<input type="radio" name="hazardous" value="no" >No<br>
<?php } ?>
I also have a select option element like below:
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
Which im trying to set up, I could use:
<?php if ($row['Category'] == "hazardawareness"){ ?>
Category: <select name="category">
<option value="hazardawareness" selected="selected">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "observation"){ ?>
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation" selected="selected">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "insurance"){ ?>
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance"selected="selected">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "attitude"){ ?>
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude" selected="selected">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "knowledge"){ ?>
Category: <select name="category">
<option value="hazardawareness" >Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge" selected="selected">Gen. Knowledge</option>
</select><br>
<?php } ?>
But perhaps there is a better method to do this without duplicating so much code?
You can add an if clause on your option creation that will at the selected attribute if needed.
<?php $options = array(
"Hazard Awareness" => hazardawareness,
"Observation" => observation,
"Insurance" => insurance,
"Attitude" => attitude
); ?>
<select name="category">
<?php foreach($options as $display => $value) { ?>
<option value='<?= $value ?>' <?php if($row['Category'] == trim($value)) { ?>selected='selected'<?php } ?>>
<?= $display ?>
</option>
<?php } ?>
</select>
I think this will help you to do the work using less code:
<?php
$options = array(
"Hazard Awareness" => hazardawareness,
"Observation" => observation,
"Insurance" => insurance,
"Attitude" => attitude
);
echo 'Category: <select name="category">';
foreach($options as $display => $value) {
if ($row['Category'] == trim($value)) {
echo '<option value="' . $value . '" selected>' . $display .'</option>';
}
else {
echo '<option value="' . $value . '">' . $display . '</option>';
}
}
echo '</select>';
Put your option data in array().
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>
How to select a multiple choices of array in a dropdown using php.
Input :
<?php
$val = "22,33,55";
?>
<select name="choice" multiple="true">
<option value="11">11</option>
<option value="22">22</option>
<option value="33">33</option>
<option value="44">44</option>
<option value="55">55</option>
</select>
I want the Output like:
<select name="choice" multiple="true">
<option value="11">11</option>
**<option value="22">22</option>**
**<option value="33">33</option>**
<option value="44">44</option>
**<option value="55">55</option>**
</select>
<?php
$val = "22,33,55";
$valarray = explode(',',$val);
?>
<select name="choice" multiple="true">
<option value="11" >11</option>
**<option value="22" <?php if(in_array(22,$valarray)){ ?>selected="selected"<?php } ?>>22</option>**
**<option value="33" <?php if(in_array(33,$valarray)){ ?>selected="selected"<?php } ?>>33</option>**
<option value="44">44</option>
**<option value="55" <?php if(in_array(55,$valarray)){ ?>selected="selected"<?php } ?>>55</option>**
</select>
http://www.w3schools.com/tags/att_select_multiple.asp
First of all, change $val into array. Then is select options use in_array() ( http://www.php.net/in_array ) to validate if you need to print selected="selected" in give option.
I guess thats the easiest option
Here is a solution
<?php
$selected_value = "22,33,55";
$selected_value = explode(',', $selected_value);
$all_values = array(11,22,33,44,55); ?>
<select name="choice[]" multiple="multiple"> <?php
foreach($all_values as $option_value ) {
$selected = '';
if(in_array($option_value, $selected_value)){
$selected = "selected";
}?>
<option <?php echo $selected; ?> ><?php echo $option_value; ?></option><?php
} ?>
</selected