Iam using PHP codeigniter framework. Value is set to select dropdown as below
<select class="form-control";>
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected'; }?> value='migrated' >migrated</option>
</select>
for each row in table there is select option like above. I want to highlight option with color if option is set to migrated on page load.
I tried like below
<select class="form-control";>
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected'; echo 'style="<color:red>"' }?> value='migrated' >migrated</option>
</select>
Your are not using proper syntax for style; you are missing a semicolon; and selected and style will be together making it useless. Try:
<select class="form-control">
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected style="background-color:red"' }?> value='migrated' >migrated</option>
</select>
Although to me it is easier to read inline conditions as the following:
<select class="form-control">
<option <?php echo $d1 == 'native' ? 'selected' : ''; }?> value='native' >native</option>
<option <?php echo $d1 == 'migrated' ? 'selected style="background-color:red"' : ''; }?> value='migrated' >migrated</option>
</select>
your styling is wrong
<select class="form-control";>
<option <?php if($d1 == 'native') { echo 'selected'; }?> value='native' >native</option>
<option <?php if($d1 == 'migrated') { echo 'selected'; echo 'style="color:red"'; }?> value='migrated' >migrated</option>
</select>
you don't need the brackets <> inside the inline style and don't forget semicolon ;
Related
Is there any way to set the selected item in a drop down box using the following 'type' code?
<select selected="<?php print($row[month]); ?>"><option value="Janurary">January</option><option value="February">February</option><option value="March">March</option><option value="April">April</option></select>
The database holds a month.. and I want to allow on the edit page, them to choose this month.. but it to be pre-filled with their current setting?
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.
You mark the selected item on the <option> tag, not the <select> tag.
So your code should read something like this:
<select>
<option value="January"<?php if ($row[month] == 'January') echo ' selected="selected"'; ?>>January</option>
<option value="February"<?php if ($row[month] == 'February') echo ' selected="selected"'; ?>>February</option>
...
...
<option value="December"<?php if ($row[month] == 'December') echo ' selected="selected"'; ?>>December</option>
</select>
You can make this less repetitive by putting all the month names in an array and using a basic foreach over them.
You can use this method if you use a MySQL database:
include('sql_connect.php');
$result = mysql_query("SELECT * FROM users WHERE `id`!='".$user_id."'");
while ($row = mysql_fetch_array($result))
{
if ($_GET['to'] == $row['id'])
{
$selected = 'selected="selected"';
}
else
{
$selected = '';
}
echo('<option value="'.$row['id'].' '.$selected.'">'.$row['username'].' ('.$row['fname'].' '.substr($row['lname'],0,1).'.)</option>');
}
mysql_close($con);
It will compare if the user in $_GET['to'] is the same as $row['id'] in table, if yes, the $selected will be created. This was for a private messaging system...
Simple and easy to understand example by using ternary operators to set selected value in php
<?php $plan = array('1' => 'Green','2'=>'Red' ); ?>
<select class="form-control" title="Choose Plan">
<?php foreach ($plan as $id=> $value) { ?>
<option value="<?php echo $id;?>" <?php echo ($id== '2') ? ' selected="selected"' : '';?>><?php echo $value;?></option>
<?php } ?>
</select>
Its too old but I have to add my way as well :) because it is generic and useful especially when you are using static dropdown values.
function selectdCheck($value1,$value2)
{
if ($value1 == $value2)
{
echo 'selected="selected"';
} else
{
echo '';
}
return;
}
and in you dropdown options you can use this function like this and you can use this as many as you can because it fits with all of your select boxes/dropdowns
<option <?php selectdCheck($row[month],january); ?> value="january">january</option>
:) I hope this function help others
Simple way
<select class ="dropdownstyle" name="category" selected="<?php print($messageeditdetails[0]['category_id']); ?>">
<option value=""><?php echo "Select"; ?></option>
<?php foreach ($dropdowndetails as $dropdowndetails) { ?>
<option <?php if($messageeditdetails[0]['category_id'] == $dropdowndetails['id']) { ?> selected="<?php echo $dropdowndetails['id']; ?>" <?php } ?> value="<?php echo $dropdowndetails['id']; ?>"><?php echo $dropdowndetails['category_name']; ?></option>
<?php } ?>
</select>
This is the solution that I came up with...
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="select_month">
<?php
if (isset($_POST['select_month'])) {
if($_POST["select_month"] == "January"){
echo '<option value="January" selected="selected">January</option><option value="February">February</option>';
}
elseif($_POST["select_month"] == "February"){
echo '<option value="January">January</option><option value="February" selected="selected">February</option>';
}
}
else{
echo '<option value="January">January</option><option value="February">February</option>';
}
?>
</select>
<input name="submit_button" type="submit" value="Search Month">
</form>
A Simple Solution:
It work's for me
<div class="form-group">
<label for="mcategory">Select Category</label>
<select class="form-control" id="mcategory" name="mcategory" required>
<option value="">Please select category</option>
<?php foreach ($result_cat as $result): ?>
<option value="<?php echo $result['name'];?>"<?php
if($result['name']==$mcategory){
echo 'selected';
} ?>><?php echo $result['name']; ?></option>
}
<?php endforeach; ?>
</select>
</div>
Check this:
<select class="form-control" id="department" name="department" type="text">
<option value="medical-furniture" #if($list->department == "medical-furniture") selected #endif>Medical furniture</option>
<option value="medical-table" #if($list->department == "medical-table") selected #endif>Medical table</option>
<option value="service" #if($list->department == "service") selected #endif>Service</option>
</select>
My suggestion is to leverage the hidden/collapse attribute. Try with this example:
<select>
<option value="echo $row[month]" selected disabled hidden><? echo $row[month] ?></option>
<option value="1">Jan</option>
<option value="2">Feb</option>
<option value="3">Mar</option>
</select>
in case of null for $row[month] the selected item is blank and with data, it would contain less codes for many options and always working for HTML5 and bootstrap etc...
If you have a big drop down. it's much easier to use jQuery with PHP.
This is how to do it:
<script>
$(document).ready(function () {
$('select[name="country"]').val('<?=$data[0]['Country']?>');
});
</script>
The easiest solution for the selected option in dropdown using PHP is following
<select class="form-control" name="currency_selling" required >
<option value="">Select Currency</option>
<option value="pkr" <?=$selected_currency == 'pkr' ? ' selected="selected"' : '';?> >PKR</option>
<option value="dollar" <?=$selected_currency == 'dollar' ? ' selected="selected"' : '';?> >USD</option>
<option value="pounds" <?=$selected_currency == 'pounds' ? ' selected="selected"' : '';?> >POUNDS</option>
<option value="dirham" <?=$selected_currency == 'dirham' ? ' selected="selected"' : '';?> >DRHM</option>
</select>
You can try this after select tag:
<option value="yes" selected>yes</option>
<option value="no">no</option>
How do i keep my select item selected via PHP? i did it the same way with radiobuttons and it worked. the only thing i changed was changed "checked" to "selected" as was recommended to me.
Here is my PHP:
if (empty($_POST["favFruit"])) {
$favFruitErr = "You must select 1 or more";
}
else {
$favFruit = $_POST["favFruit"];
}
And here is my form:
<select class=favfruitwindow name="favFruit[]" size="4" multiple>
<option <?php if (isset($favFruit) && $favFruit == "apple") echo "selected"; ?> value="apple">Apple</option>
<option <?php if (isset($favFruit) && $favFruit == "banana") echo "selected"; ?> value="banana">Banana</option>
<option <?php if (isset($favFruit) && $favFruit == "plum") echo "selected"; ?> value="plum">Plum</option>
<option <?php if (isset($favFruit) && $favFruit == "pomegranate") echo "selected"; ?> value="pomegranate">Pomegranate</option>
<option <?php if (isset($favFruit) && $favFruit == "strawberry") echo "selected"; ?> value="strawberry">Strawberry</option>
<option <?php if (isset($favFruit) && $favFruit == "watermelon") echo "selected"; ?> value="watermelon">Watermelon</option>
</select>
$favFruit will be an array even when selecting a single option, and null when nothing is selected.
print_r($_POST["favFruit"]); will return Array ( [0] => banana [1] => plum )
You need for see if the item exists in the array and select based on that check.
<select class=favfruitwindow name="favFruit[]" size="4" multiple>
<option <?php if (isset($favFruit) && in_array('apple', $favFruit)) echo "selected"; ?> value="apple">Apple</option>
<option <?php if (isset($favFruit) && in_array('banana', $favFruit)) echo "selected"; ?> value="banana">Banana</option>
<option <?php if (isset($favFruit) && in_array('plum', $favFruit)) echo "selected"; ?> value="plum">Plum</option>
<option <?php if (isset($favFruit) && in_array('pomegranate', $favFruit)) echo "selected"; ?> value="pomegranate">Pomegranate</option>
<option <?php if (isset($favFruit) && in_array('strawberry', $favFruit)) echo "selected"; ?> value="strawberry">Strawberry</option>
<option <?php if (isset($favFruit) && in_array('watermelon', $favFruit)) echo "selected"; ?> value="watermelon">Watermelon</option>
</select>
Here is the code that i am using for drop down to select.
<select name="formreg" value="">
<option value="Registered" <?php if($f_reg == 'Registered') { ?> selected <?php echo $f_reg; } ?> >Registered</option>
<option value="NonReg" <?php if($f_reg == 'NonReg') { ?> selected <?php echo $f_reg; } ?> >NonReg</option>
</select>
<select name="regform">
<option value="Registered" <?php if (isset($register) && $register=='Registered') {?> selected='selected' <?php echo $register; }?>>Registered</option>
<option value="NonReg" <?php if (isset($nonreg) && $nonreg == 'NonReg') {?> selected='selected' <?php echo $nonreg;} ?>>NonReg</option>
</select>
At the top of script:
$f_reg = $_POST["f_reg"]; // or $f_reg = $_GET["f_reg"] if you pass parameters as the url part
then your < select > tag:
<select name="formreg">
<option value="Registered" <?php if ($f_reg == "Registered") print "selected"?>>Registered</option>
<option value="NonReg" <?php if ($f_reg == "NonReg") print "selected"?>>NonReg</option>
</select>
I have a form which a user fills up and hits "Save & Next" which will take them to another page where the user can upload images and hit "Final Submit". They can also go back to the previous page to edit the data.
At that time, all the data he had previously filled up should be displayed on the text box. I have used session variable to store the data and display it.
I am stuck in the drop down box.
<select name="District">
<option value="East">East</option>
<option value="West">West</option>
<option value="North">North</option>
<option value="South">South</option>
</select>
When the user submits, I am storing the selected value in Session $_SESSION['District'] = $_POST['District']; and when the user clicks back to go the previous page, I need to auto select that option value in the drop down.
How can I achieve this?
Using array would make it easier.
<?php
$options = array(
'East', 'West', 'North', 'South',
);
?>
<select name="District">
<?php foreach($options as $option) { ?>
<option value="<?php echo $option; ?>" <?php echo (isset($_SESSION['District']) && $_SESSION['District'] == $option) ? "selected" : "" ?>><?php echo $option; ?></option>
<?php } ?>
</select>
Check value in session and if matches set the attibute selected
<select name="District">
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'East'){ echo 'selected'; }?> value="East">East</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'West'){ echo 'selected'; }?> value="West">West</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'North'){ echo 'selected'; }?> value="North">North</option>
<option <?php if (!empty($_POST['District']) && $_POST['District'] == 'South'){ echo 'selected'; }?> value="South">South</option>
</select>
Just check the selected value against the one stored in session.
Your option will look like this:
<option value="East" <?php echo ($_SESSION['District']=="East" ? "selected" : ""; ?>>East</option>
And the right one will be selected
<select name="District">
<option value="East" <?php if($_SESSION['District'] == "East"):?>selected="selected"<?php endif; ?>>East</option>
... Repeat with all options ...
Use below code:
<select name="District">
<option value="East" <?php echo ($_SESSION['District'] == "East") ? "selected" : "" ?>>East</option>
<option value="West" <?php echo ($_SESSION['District'] == "West") ? "selected" : "" ?>>West</option>
<option value="North" <?php echo ($_SESSION['District'] == "North") ? "selected" : "" ?>>North</option>
<option value="South" <?php echo ($_SESSION['District'] == "South") ? "selected" : "" ?>>South</option>
</select>
<option value="East" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'East' ? 'selected="selected"' :'' ;?> >East</option>
<option value="West" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'West' ? 'selected="selected"' :'' ;?>>West</option>
<option value="North" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'North' ? 'selected="selected"' :'' ;?>>North</option>
<option value="South" <?php echo isset($_SESSION['District']) && $_SESSION['District'] == 'South' ? 'selected="selected"' :'' ;?>>South</option>
I am using chosen select drop down to show auto complete drop down. I want to set selected value for edit. I tried following code which works for normal select option but not working for chosen select
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list))
{
foreach($list as $d)
{
?>
<option value="<?php echo $d->id; ?><?php if($d->id == 2) { echo "selected"; } ?>"><?php echo $d->name; ?></option>
<?php } } ?>
</select>
You are putting your selected inside your value attribute, you need to write it after :
<select class="chosen-select" >
<option value=""></option>
<?php if(!empty($list)) {
foreach($list as $d) {
?>
<option value="<?php echo $d->id; ?>"<?php if($d->id == 2) { echo " selected"; } ?>><?php echo $d->name; ?></option>
<?php } } ?>
</select>
Building on #roberto06's answer, the following should be a bit cleaner to look at.
BTW, you really should consider using a template engine.
<select class="chosen-select">
<option value=""></option>
<?php if (!empty($list)): ?>
<?php foreach ($list as $d): ?>
<option value="<?php echo $d->id; ?>" <?php echo ($d->id == 2) ? "selected" : "">
<?php echo $d->name; ?>
</option>
<?php endforeach; ?>
<?php endif; ?>
</select>