Is there any way to set the selected item in a drop down box using the following “type” code?

<select selected=”<?php print($row); ?>”><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.

Đang xem: Show selected option value from array & mysql db using php

Erk sorry, didn't see your mostly identical comment before posting mine. I'll upvote yours but leave it to the questioner to pick a winner. Btw, does your opening PHP tag have a typo? or is <?= valid? – James Wheare Aug 26 “09 at 17:55
Yours gets the tick 🙂 Makes more sense to me haha! – tarnfeld Aug 26 “09 at 17:57

<?= is valid – it does an echo for you – Greg Aug 26 “09 at 18:26
I had to modify this, had to put an if statement in.. didn't work otherwise 🙂 – tarnfeld Aug 26 “09 at 21:59

In MS Expression Web I get a warning that this (php script) is not permitted inside the HTML5 <option> tag. Is that true? – samuelschaefer Feb 7 “15 at 20:57

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 == “January”) echo ” selected=”selected””; ?>>January</option> <option value=”February”<?php if ($row == “February”) echo ” selected=”selected””; ?>>February</option> … … <option value=”December”<?php if ($row == “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)) else 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…

Xem thêm:

from where $_GET<'to'> data will get? – KUMAR Jul 2 “20 at 7:52
KUMAR It would get this information from the URL: page.php?to=34. – Frederick Marcoux Jul 2 “20 at 15:35

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) ?> </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) else 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,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) ?> 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">)) elseif($_POST<"select_month"> == “February”) } else ?> </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) ?>><?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”

My suggestion is to leverage the hidden/collapse attribute. Try with this example:

<select> <option value=”echo $row” selected disabled hidden><? echo $row ?></option> <option value=”1″>Jan</option> <option value=”2″>Feb</option> <option value=”3″>Mar</option> </select>

in case of null for $row the selected item is blank and with data, it would contain less codes for many options and always working for HTML5 and bootstrap etc…

Xem thêm:

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 () ); </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>

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *