Set selected option based on mysql field - php

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().

Related

how to edit state and country list and update it using php

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>

How to make dropdown option selected against a variable in php

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 more option in a dropdown with array of value using php?

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

how to put value in select option

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>

PHP Dropdown Validation

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.

Categories