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>
Related
I'm looking for a simple way to dynamically change the default item in a select-element without adding a ton of code into every option-element.
This is an example of a select-element that allows you to select a month and it always sets the current month as the default item:
<select name="month">
<option <?php echo ((date("n")==1)?'selected="selected"':'')?> value="1">Januar</option>
<option <?php echo ((date("n")==2)?'selected="selected"':'')?> value="2">Februar</option>
<option <?php echo ((date("n")==3)?'selected="selected"':'')?> value="3">März</option>
<option <?php echo ((date("n")==4)?'selected="selected"':'')?> value="4">April</option>
<option <?php echo ((date("n")==5)?'selected="selected"':'')?> value="5">Mai</option>
<option <?php echo ((date("n")==6)?'selected="selected"':'')?> value="6">Juni</option>
<option <?php echo ((date("n")==7)?'selected="selected"':'')?> value="7">Juli</option>
<option <?php echo ((date("n")==8)?'selected="selected"':'')?> value="8">August</option>
<option <?php echo ((date("n")==9)?'selected="selected"':'')?> value="9">September</option>
<option <?php echo ((date("n")==10)?'selected="selected"':'')?> value="10">Oktober</option>
<option <?php echo ((date("n")==11)?'selected="selected"':'')?> value="11">November</option>
<option <?php echo ((date("n")==12)?'selected="selected"':'')?> value="12">Dezember</option>
</select>
As you can see, there is just way too much code in it.
I'm wondering if anyone of you has a better idea on how to do this.
Use a loop:
<?php
$months = [
1=>'January',
2=>'february',
3=>'march'
....//etc
];
?>
<select name="month">
<?php foreach($months as $key=>$month):?>
<option <?= ((date("n")==$key)?'selected="selected"':'')?> value="<?= $key;?>"><?= $month;?></option>
<?php endforeach;?>
</select>
You could create an array of months and then check it like that.
<?php
$months = array(
array(
"monthNumber" => 1,
"name" => "January"
),
array(
"monthNumber" => 2,
"name" => "Feb"
),
array(
"monthNumber" => 3,
"name" => "March"
)
);
?>
<select name="month">
<?php
foreach ($months as $month) {
echo "<option " . ((date("n") == $month['monthNumber']) ? 'selected="selected"' : ''). ">".$month['name']."</option>";
}
?>
</select>
Use foreach loop
<?php
$months=array('1'=>'January','2'=>'Febuary','3'=>'March',4=>'April','5'=>'May','6'=>'June','7'=>'July','8'=>'August','9'=>'September',10=>'October','11'=>'November','12'=>'December');
?>
<select name='month'>
<?php foreach($months as $num=>$month) {?>
<option <?php echo ((date("n")==$num)?'selected="selected"':'')?>value="<?php echo $num;?>"><?php echo $month; ?>
</option>
</select>
It may be helpful..
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>
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>
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.