<select name="radio_typ" id="radio_typ" >
<option value="P5100">P5100</option>
<option value="P5400">P5400</option>
<option value="P7100">P7100</option>
<option value="P7200">P7200</option>
<option value="700P">Jaguar 700p</option>
<option value="LPE200">LPE200</option>
<option value="XL200">XL200</option>
</select>
I'd like to be able to pre-select an option in a drop down menu based on a variable from a database which will already be set to the current setting. This form is for the purpose of editing the setting if needbe.
I tried inserting value="<?php echo $radio_typ;?> but that didn't seem to work.
Any hints as to how I could do this?
Thank you twofed.
That code didn't quite work, but I was able to figure out a solution that did based on that thought process.
<select name="radio_typ" id="radio_typ" >
<?php foreach ($options as $value => $name) {
if ($value == $radio_typ) {
$sel = "selected";
} else {
$sel = "";
}?>
<option value="<?= $value; ?>" <?= $sel; ?>><?= $name; ?></option>
<?php } ?>
</select>
I think, the best solution would be:
<?php
$radio_typ = "700P";
// value => name
$options = [
"P5100" => "P5100",
"P5400" => "P5400",
"P7100" => "P7100",
"P7200" => "P7200",
"700P" => "Jaguar 700p",
"LPE200" => "LPE200",
"XL200" => "XL200"
];
?>
<select name="radio_typ" id="radio_typ" >
<?php foreach ($options as $value => $name) { ?>
<option value="<?= $value; ?>"<?= ($value == $radio_typ) ? ' selected' : ''; ?>><?= $name; ?></option>
<?php } ?>
</select>
Related
This always selected the last record from my database.
This is my select tag:
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" />
<option value=""></option>
<?php
$t_cost=$this->db->query("select * from t_proyek ");
foreach($t_cost->result() as $value){
$selected= '';
if($t_proyek_kd_proyek == $value->nm_proyek){
$selected = 'selected';
}
?>
<option value="<?php echo $value->kd_proyek; ?>" echo ' selected="selected"'; >
<?php echo $value->nm_proyek; ?>
</option>
<?php }?>
</select>
And this is my controller:
$row = $this->T_cost_model->get_by_id($id);
if ($row) {
$data = array(
'button' => 'Update',
'action' => site_url('konsultan/cost/update_action'),
'kd_cost' => set_value('kd_cost', $row->kd_cost),
'investasi_awal' =>rupiah(set_value ('investasi_awal', $row->investasi_awal)),
'b_operasional' => rupiah(set_value('b_operasional', $row->b_operasional)),
'g_cost' => rupiah(set_value('g_cost', $row->g_cost)),
'disbenfit' => rupiah(set_value('disbenfit', $row->disbenfit)),
'pwc' => set_value('pwc', $row->pwc),
't_proyek_kd_proyek' => set_value('t_proyek_kd_proyek', $row->t_proyek_kd_proyek),
);
$this->load->view('konsultan/t_cost_form', $data);
In your HTML, your last option tag has selected="selected" explicitly set:
option value="<?php echo $value->kd_proyek; ?>" <?php echo 'selected="selected"'; ?>
Wrap that in a condition and the selected option from your loop should work.
option value="<?php echo $value->kd_proyek; ?>" <?php if ($selected) echo 'selected="selected"'; ?>
change your <select> html code value like this
<select name="t_proyek_kd_proyek" id="t_proyek_kd_proyek" class="select-search" />
<option value=""></option>
foreach($t_cost->result() as $value){
$selected= '';
if($t_proyek_kd_proyek == $value->nm_proyek){
$selected = ' selected="selected" ';
}
?>
<option value="<?php echo $value->kd_proyek; ?>" <?= $selected?> >
<?php echo $value->nm_proyek; ?>
</option>
<?php }?>
</select>
hope it will help
I have a simple HTML form with a simple select like this
<select name="myselect" selected="<?php echo $_GET['brand'];?>">
<option value="" <?php if($brand== "") echo "selected"; ?>>all brands</option>
<option value="samsung" <?php if($brand== "samsung") echo "selected"; ?>>Samsung</option>
<option value="motorola" <?php if($brand== "motorola") echo "selected"; ?>>Motorola</option>
</select>
What I am trying to do is because its a search filtering results form if the user choose Samsung as default brand to search next page with filteres search results will the select option default selected Samsung and no other. If user select to search Motorola then the selected option default will be Motorola.
How do I fix this?
selected is attribute of option not select, so remove it from select tag, change to:
<?php $brand = trim( strtolower($_GET['brand']) ); ?>
<select name="myselect">
<option value="" <?php if($brand== "") echo "selected"; ?>>all brands</option>
<option value="samsung" <?php if($brand== "samsung") echo "selected"; ?>>Samsung</option>
<option value="motorola" <?php if($brand== "motorola") echo "selected"; ?>>Motorola</option>
</select>
This is Simple Example of selected=selected by using foreach loop and ternary operators in php... use can easily understand and customize that code...
<?php $plan = array('1' => 'Green','2'=>'Red' ); ?>
<select class="form-control" title="Choose Plan">
<?php foreach ($plan as $key => $value) { ?>
<option value="<?php echo $key;?>" <?php echo ($key == '2') ? ' selected="selected"' : '';?>><?php echo $value;?></option>
<?php } ?>
</select>
<?php $selected_brand = $_GET['brand'];
$temp_str = '';
$temp_str .= '<select name="myselect">';
$brands_array = array("all" => "all brands", "samsung" => "Samsung", "motorola" => "Motorola");
foreach ($brands_array as $key => $value) {
if($key == $selected_brand){
// For selected option.
$temp_str .= '<option value="'.$key.'" selected>'.$value.'</option>';
} else {
$temp_str .= '<option value="'.$key.'">'.$value.'</option>';
}
}
$temp_str .= '</select>';
echo $temp_str; // Select box.
?>
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>
I'm dealing with a registration form at the moment, specifically, I'm dealing with redisplaying any information the user enters into the inputs if there is an error in the registration verification.
Normally, when a user hits 'submit' - if there is an error, the page refreshes and they are echoed out and the form is redisplayed. The problem is there is sometimes genuinely valid information in some of the fields, and by regrabbing that data, it can be redisplayed using the value attribute and the isset() function like so (if of course that data has been POSTed, which in this case it has):
<input type="email" name="anEmail"
value=" <?php echo (isset($email)) ? $email : false; ?>" />
That works fine for the <input> element, but is the same achievable with a dropdown list through the <select> element?
I initially tried:
<select name="region"
value="<?php echo (isset($region)) ? $region : 'Auckland'; ?>" >
<!-- with 'Auckland' being the default value of the list -->
However, it doesn't seem like that value="" is a valid attribute for the <select> tag.
Any ideas on how I could accomplish this? Cheers.
function build_select($name, array $options = NULL, $selected = NULL)
{
foreach($options as $value => $option)
$return .= ($value != $selected)? '<option value="'.$value.'">'.$option.'</option>': '<option selected="selected" value="'.$value.'">'.$option.'</option>';
return '<select name="'.$name.'">'.$return.'</select>';
}
echo build_select('region', array('Lorem' => 'Ipsum', 'Auckland' => 'Auckland', 'Ipsum' => 'Lorem'), 'Auckland');
HTML
<select name="region">
<option value="Lorem">Ipsum</option>
<option selected="selected" value="Auckland">Auckland</option>
<option value="Ipsum">Lorem</option>
</select>
You have to add selected="selected" to specific option. Like so:
<select name="region">
<option value="opt1">Option 1</option>
<option value="opt2" selected="selected">Option 2</option>
<option value="opt3">Option 3</option>
<option value="opt4">Option 4</option>
</select>
With this code Option 2 is selected
And in pehe you have to check if specific option equals the input.
<option value="opt4"<?php if ($region == "opt2"){ echo ' selected="selected"'; } ?>>Option 4</option>
it's selected="selected", i think. or just "selected".
a quick example of how you might implement that:
case "value0":
$selected0 = "selected";
break;
case "value1":
$selected1 = "selected";
break;
<option value="value0" <?php echo $selected0;?>></option>
<option value="value1" <?php echo $selected1;?>></option>
function __selected($ctrlName,$value)
{
if(isset($_REQUEST[$ctrlName]) && trim($_REQUEST[$ctrlName]) == trim($value))
return "selected='selected'";
else
return false;
}
use like
<select name="cmbCondition">
<option value="Excellent" <?php echo __selected('cmbCondition',"Excellent")?>>Excellent</option>
<option value="Good" <?php echo __selected('cmbCondition',"Good")?>>Good</option>
<option value="Poor" <?php echo __selected('cmbCondition',"Poor")?>>Poor</option>
</select>
One way you can do this if number of options are not much:
In every option put the code:
<select>
<option selected="<?php echo (isset($region) && $region=='Volvo') ? $region : ''; ?>">Volvo</option>
<option selected="<?php echo (isset($region) && $region=='Saab') ? $region : ''; ?>">Saab</option>
<option selected="<?php echo (isset($region) && $region=='Mercedes') ? $region : ''; ?>">Mercedes</option>
<option selected="<?php echo (isset($region) && $region=='Audi') ? $region : ''; ?>">Audi</option>
</select>
But if options are much in number javascript code is needed to be written..
if(isset($region))
{
echo '
<script>$("#id option").each(function()
{
if($(this).val()=='.$region.')
$(this).attr("selected", "selected");
});
</script>';
}
Dynamiclly creating options basod on array.
Usage should be pretty straightforward.
<?php
function rennder_options($options = array(), $selection = null) {
if (!is_array($options)) {
return false;
}
$result = "";
foreach($option as $option) {
$option_str = '<option value="'.$option['value'].'"';
if ($selection !== null && $option['value'] == $selection){
$option_str .= ' selected="selected"';
}
$option_str .= '>'.$option['name'].'</option>';
$result .= $option_str;
}
return result;
}
$options = array(
array( "value" => "opt1", "name" => "Option 1" )
array( "value" => "opt2", "name" => "Option 2" ),
array( "value" => "opt3", "name" => "Option 3" ),
);
?>
<select name="region">
<?php echo render_options($options, $_POST['region']); ?>
</select>