How to Set Selected Value of HTML Select Box with PHP

How to set selected value of HTML select box with PHP

The manual way.....

<select name="interest">
<option value="seo"<?php if($result['interest'] == 'seo'): ?> selected="selected"<?php endif; ?>>SEO и Блоговодство</option>
.....

The better way would be to loop through the interests

$interests = array(
'seo' => 'SEO и Блоговодство',
'auto' => 'Авто',
....
);

<select name="interest">
<?php foreach( $interests as $var => $interest ): ?>
<option value="<?php echo $var ?>"<?php if( $var == $result['interest'] ): ?> selected="selected"<?php endif; ?>><?php echo $interest ?></option>
<?php endforeach; ?>
</select>

how to set selected value in html dropdown using php

Let me show you very neat method of doing this.

1) Take an array of options

2) Loop over it to get select <option>s.

3) Check if current is selected in loop.

Corrected code:

<?php
$form_field1 .= '<select name="typeofleave">';
$options = array();
$options['LWP'] = 'LWP';
$options['SL'] = 'SL';
$options['PL'] = 'PL';
if (! empty($options)) {
foreach ($options as $option) {
$selected = ($typeofleave == $option) ? 'selected="selected"' : '';
$form_field1 .= '<option value="'.$option.'" ' . $selected . '>'.$option.'</option>';
}
}
$form_field1 .= '</select>';
?>

Set option as selected with php

Just add the selected="selected" attribute to the option tag that you received under $_POST['s'].

<?php
$selectedOption = '';
if(isset($_POST['btSubmit'])) {
$selectedOption = $_POST['s']; // Value of selected option … but how to use it below?
}

function injectSelectedAttribute($selectedOption, $option_value){
return strtolower($selectedOption) === strtolower($option_value) ? 'selected="selected"' : '';
}
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
<select name="s">
<option value="" disabled>-- choose one --</option>
<option value="a" <?php echo injectSelectedAttribute($selectedOption, 'a'); ?>>choose a</option>
<option value="b" <?php echo injectSelectedAttribute($selectedOption, 'b'); ?>>choose b</option>
</select>
<input type="submit" name="btSubmit">
</form>

You can further improve this to echo HTML of the options via looping on a PHP array of select options.

How do I set the selected item in a drop down box

You need to set the selected attribute of the correct option tag:

<option value="January" selected="selected">January</option>

Your PHP would look something like this:

<option value="January"<?=$row['month'] == 'January' ? ' selected="selected"' : '';?>>January</option>

I usually find it neater to create an array of values and loop through that to create a dropdown.

HTML select option set selected default echo get value php

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>

Using $_POST to get select option value from HTML

Use this way:

$selectOption = $_POST['taskOption'];

But it is always better to give values to your <option> tags.

<select name="taskOption">
<option value="1">First</option>
<option value="2">Second</option>
<option value="3">Third</option>
</select>

How to dynamically set an option selected in PHP

Try this

foreach($optionArr as $key => $value){
$isSelected =""; //added this line
if($_REQUEST['opt_side_'.$cside] == $value){
$isSelected = "selected";
}
echo '<option value="'.$value.'"'.$isSelected.'>'.$key.'</option>';
}

PHP set selected value of dropdown box

With the way your string is built, it's a fairly simple str_replace(), which is nice as it saves the hassle of needing regular expressions:

$dropdown = str_replace("value='".$rid."'","value='".$rid."' selected=\"selected\"",$dropdown);

Set selected option value using PHP

Try this:

<?php 
$postval = $_POST['dbfld'];

$opvar = "<select name='".$name."' id='".$Id."'><option value=''> Select ART_ID</option>";

while($row = mysqli_fetch_array($result_sql,MYSQLI_ASSOC)) {
$selected = $postval == $row[$dbfld] ? ' selected="selected"' : '';
$opvar .= "<option value='".$row[$dbfld]."'".$selected.">".$row[$dbfld]."</option>";
}

$opvar .= "</select>";
?>

How to Keep the selected value of the select box after Form POST or GET

I assume you get categories from database.

you should try:

<?php

$categories = $rows; //array from database
foreach($rows as $row){
if($row['name'] == $_POST['category']){
$isSelected = ' selected="selected"'; // if the option submited in form is as same as this row we add the selected tag
} else {
$isSelected = ''; // else we remove any tag
}
echo "<option value='".$row['id']."'".$isSelected.">".$row['name']."</option>";
}
?>


Related Topics



Leave a reply



Submit