Dropdown option getting selected when it should not - html

Current code for selecting birthday dates
<label>Birthday</label>
<select name="dob-year" id="dob-year" class="form-control">
<option value="" disabled>Year</option>
<option value="" disabled>----</option>
...
<option value="1971" {{date("Y", strtotime($friend->birthday)) == '1971' ? 'selected' : ''}}>1971</option>
<option value="1970" {{date("Y", strtotime($friend->birthday)) == '1970' ? 'selected' : ''}}>1970</option>
<option value="1969" {{date("Y", strtotime($friend->birthday)) == '1969' ? 'selected' : ''}}>1969</option>
...
</select>
<select name="dob-month" id="dob-month" class="form-control">
<option value="" disabled>Month</option>
<option value="" disabled>-----</option>
<option value="01" {{date("F", strtotime($friend->birthday)) == 'January' ? 'selected' : ''}}>01</option>
<option value="02" {{date("F", strtotime($friend->birthday)) == 'February' ? 'selected' : ''}}>02</option>
...
</select>
<select name="dob-day" id="dob-day" class="form-control">
<option value="" disabled>日</option>
<option value="" disabled>---</option>
<option value="01" {{date("d", strtotime($friend->birthday)) == '01' ? 'selected' : ''}}>01</option>
<option value="02" {{date("d", strtotime($friend->birthday)) == '02' ? 'selected' : ''}}>02</option>
...
</select>
What I want to do
is to add {{$friend->birthday == null ? 'selected' : ''}} like below in each Year/Month/Day tags
<select name="dob-year" id="dob-year" class="form-control">
<option value="" disabled>Year</option>
<option value="" {{$friend->birthday == null ? 'selected' : ''}} disabled>----</option>
...
</select>
The problem
This a weird one I cant seem to find the issue that is causing this.
Ive disabled all javascript and double checked what is being returned from controller too but cannot find the source of the issue.
The default selected values of Year/Month/Day is always 1970/01/01 even if I hard code selected on other options. For example <option value="" selected>Year</option> would have it only select 1970.
If I delete the whole 1970 option tag only then everything works fine. So for this to work I had to delete 1970 option tag, 01 January option tag and 01 day option tag. Which is no use...
Does anyone have an idea what could be causing this?

This is happening due to the format you are giving to strtotime() function, when giving any unsupported format to
echo date('Y/d/m',strtotime("20/03/03"));
echo date('Y:d:m',strtotime(null))
it will return you default value which is `1970/01/01`
you are passing either wrong format or null as parameter to strtotime()
Suggestion
You can do something like this, where you want to show the dropdown get dbirthday into a variable and check if it is null or not, and then update your $year, $day, $month value in that case if value is null it will not select any value and on right value it will select the option is correct.
#php
$friendBD = '';
$day = '';
$month = '';
$year = '';
#endphp
#foreach ($friends as $friend)
#php
$friendBD = $friend->birthday
#endphp
#if(!is_null($friendBD))
#php
$date = \Carbon\Carbon::create($friendBD);
$month = $date->format('M');
$day = $date->format('d');
$year = $date->format('Y');
#endphp
#endif
<label>Birthday</label>
<select name="dob-year" id="dob-year" class="form-control">
<option value="" disabled>Year</option>
<option value="" disabled>----</option>
<option value="1971" >1971</option>
<option value="1970" {{$year == '1970' ? 'selected' : ''}}>1970</option>
<option value="1969" {{$year == '1969' ? 'selected' : ''}}>1969</option>
<option value="2020" {{$year == '2020' ? 'selected' : ''}}>2020</option>
</select>
#endforeach

Your PHP might be invalid
http://sandbox.onlinephpfunctions.com/code/bb07478fd5cc5da9535750524631d0c78763fe11
If you have
$friend['birthday'] = "1980/02/01";
or
$friend = array("birthday"=>"1980/02/01");
then you need to access the birthday like this
echo strtotime($friend["birthday"]);
So your statements need to look like this
echo date("Y", strtotime($friend["birthday"]));
echo date("F", strtotime($friend["birthday"]));
echo date("d", strtotime($friend["birthday"]));
If you do NOT access the string correctly, your will get 1970, January, 01 for any date. In my case since I am in GMT+1, I get 1969, December, 31 for the "0" date resulting from an invalid date passed to strtotime
Alternatively use JavaScript:
const dob = "1971/02/01" // "<?= $friend["birthday"] ?>";
const [yyyy,mm,dd] = dob.split("/")
document.getElementById("dob-year").value=yyyy;
document.getElementById("dob-month").value=mm;
document.getElementById("dob-day").value=dd;
<label>Birthday</label>
<select name="dob-year" id="dob-year" class="form-control">
<option value="" disabled>Year</option>
<option value="" disabled>----</option>
<option value="1971">1971</option>
<option value="1970">1970</option>
<option value="1969">1969</option>
</select>
<select name="dob-month" id="dob-month" class="form-control">
<option value="" disabled>Month</option>
<option value="" disabled>-----</option>
<option value="01" 01</option>
<option value="02">02</option>
</select>
<select name="dob-day" id="dob-day" class="form-control">
<option value="" disabled>日</option>
<option value="" disabled>---</option>
<option value="01">01</option>
<option value="02">02</option>
</select>

Related

Multi forms with selects but all $_POST are overwritten with last $_POST

In below test code I try to save and use 3 $_POST variables. But only the last $_POST variable is stored. The rest is overwritten with the last $_POST variable.
Purpose of the question is to select first the wanted country. Select from a table the country as selected. Then select the wanted a car_brand from the selected country. Then select the wanted company and filter this from the selected country+car_brand.
I have tried to store the 3 $_POST into a $_SESSION also tried it with $_REQUEST via type="hidden" without getting the 3 $_POST variables.
Question: what do I do wrong and how can I solve this problem?
session_start();
$content .= ' <form id="sel_country" method="POST">
<select name="country" onchange="this.form.submit()" >
<option value="NL">Netherlands</option>
<option value="DE">German</option>
<option value="GB">England</option>
</select>
</form>';
$content .= '<form id="sel_car_brand" method="POST">
<select name="brand" onchange="this.form.submit()" >
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="audi">Audi</option>
</select>
</form>';
$content .= ' <form id="sel_company" method="POST">
<select name="company" onchange="this.form.submit()">
<option value="dealer">Dealer</option>
<option value="service">Service</option>
<option value="import">Importer</option>
</select>
</form>';
$_SESSION['country'] = $_POST['country'];
$_SESSION['brand'] = $_POST['brand'];
$_SESSION['company'] = $_POST['company'];
var_dump($_SESSION) ;
Had a session_start() at the beginning and then stock your variable in it. Like $_SESSION['country'] = $_POST['country'] when $_POST['country'] is defined (do a var_dump() to verify it) and you'll be able to access your datas stocked in your $_SESSION.
session_start();
$content .= ' <form id="sel_country" method="POST">
<select name="country" onchange="this.form.submit()" >
<option value="NL">Netherlands</option>
<option value="DE">German</option>
<option value="GB">England</option>
</select>
</form>';
$_SESSION['country'] = $_POST['country'];
$content .= '<form id="sel_car_brand" method="POST">
<select name="brand" onchange="this.form.submit()" >
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="audi">Audi</option>
</select>
</form>';
$_SESSION['brand'] = $_POST['brand'];
$content .= ' <form id="sel_company" method="POST">
<select name="company" onchange="this.form.submit()">
<option value="dealer">Dealer</option>
<option value="service">Service</option>
<option value="import">Importer</option>
</select>
</form>';
$_SESSION['company'] = $_POST['company'];
var_dump($_SESSION) ;
// Edited by moliets: var_dump response: array(3) { ["country"]=> NULL ["brand"]=> NULL ["company"]=> string(6) "import" }

default select box by a passed parameter to django template?

I'm passing a dictionary to a django template and trying to set a default value for a select box. This template is an 'edit product' template for a previous 'add product' template, thus the user selected one of the following options from a select box when he added a new product and what I am trying to do now is in the 'edit product' template so he can change it while the default will be as the one he selected at first.
Is there a way to do something like this:
<td><select name="gender_limit" deafault="{{django_context.gender}}">
<option value="Male">Male</option>
<option value="Female">Female</option>
<option value="Both">Both</option>
</select>
</td>
Rather than:
<td><select name="gender_limit" >
<option value="Male" selected >Male</option>
<option value="Female">Female</option>
<option value="Both">Both</option>
</select>
</td>
I have tried to look a solution for this but haven't found. Any help is appreciated. Thanks
I found a solution to my problem, here is an example (a different one from the question example):
<select name="units">
<option ng-selected="'{{ med.units }}' == 'mg' " value="mg">mg</option>
<option ng-selected="'{{ med.units }}' == 'gr' " value="gr">gr</option>
<option ng-selected="'{{ med.units }}' == 'mcg' " value="mcg">mcg</option>
<option ng-selected="'{{ med.units }}' == 'ng' " value="ng">ng</option>
</select>
Or even as so:
<select name="units" ng-model="'{{ med.units }}'">
<option value="mg">mg</option>
<option value="gr">gr</option>
<option value="mcg">mcg</option>
<option value="ng">ng</option>
</select>
med.units is django context passed from a view.

select input value from request

I am currently working on my first web application. Users need to fill a form, and one of the information is 'city', as a select type input. If the user chooses a city but one of the information is not valid, I would like the chosen city to stay there while the user fixes the invalid input.
For example, I did it for a date that the user needs to enter:
<input type=date name="endDate" value="<c:out value="${requete.endDate}"/>">
And it's working, but for a select type input I don't know how to do it..
<select name="city">
<option value="City" selected>City</option>
<option value="Ottawa">Ottawa</option>
<option value="Toronto">Toronto</option>
<option value="Montreal">Montreal</option>
</select>
The same sort of thing - let's say that the selected city is stored in request.city.
You would need to make sure that the selected option has the "selected" property in your JSP like so:
<select name="city">
<option value="City"
<c:if test="${empty request.city}">selected</c:if>>
City
</option>
<option value="Ottawa"
<c:if test="${request.city == 'Ottawa'}">selected</c:if>>
Ottawa
</option>
<option value="Toronto"
<c:if test="${request.city == 'Toronto'}">selected</c:if>>
Toronto
</option>
<option value="Montreal"
<c:if test="${request.city == 'Montreal'}">selected</c:if>>
Montreal
</option>
</select>
So, to sum up what this is doing - whatever is between a "c:if" element will only be output as HTML if the test evaluates to true. So all this test does is determine whether the selected city equals the value that option represents, and adds the "selected" property to that element if so.
Try with simple EL and selected attribute:
<select name="city">
<option value="City" selected>City</option>
<option value="Ottawa" ${'Ottawa' eq param.city ? 'selected' : ''}>Ottawa</option>
<option value="Toronto" ${'Toronto' eq param.city ? 'selected' : ''}>Toronto</option>
<option value="Montreal" ${'Montreal' eq param.city ? 'selected' : ''}>Montreal</option>
</select>

Set selected option on existing select tag with jstl

So I have a select for the grade on each subject. It's predefined and hence I don't have to store grades as a table in the database. I have a list of qualifications and I using jstl for each like this: <c:forEach items="${qualificationdetails}" var="qd">.
For each item I am producing a select like this.
<select class="grade" title="Grade Obtained">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="A">A</option>
<option value="B">B</option>
<option value="C">C</option>
<option value="D">D</option>
<option value="E">E</option>
</select>
Is it possible to set the selected option using my variable qd ? something like
<option value="${qd.grade}" selected="selecetd">${qd.grade}</option>
This will add a duplicate option to the select. I think it would a bit "clunky" to make an array with the grades and send it accross for generating the options. Any ideas ?
You could just let JSP render the selected attribute conditionally.
<select class="grade" title="Grade Obtained">
<option value="1" ${qd.grade == '1' ? 'selected' : ''}>1</option>
<option value="2" ${qd.grade == '2' ? 'selected' : ''}>2</option>
<option value="3" ${qd.grade == '3' ? 'selected' : ''}>3</option>
<option value="A" ${qd.grade == 'A' ? 'selected' : ''}>A</option>
<option value="B" ${qd.grade == 'B' ? 'selected' : ''}>B</option>
<option value="C" ${qd.grade == 'C' ? 'selected' : ''}>C</option>
<option value="D" ${qd.grade == 'D' ? 'selected' : ''}>D</option>
<option value="E" ${qd.grade == 'E' ? 'selected' : ''}>E</option>
</select>
Alternatively, you could just create a collection/array of grades and store it in the application scope so that it's available in EL so that you can loop over it using <c:forEach>. I'm not sure how that would be "clunky". You could use <c:set> to store them commaseparated and use fn:split() to split them for <c:forEach>.
<c:set var="grades" value="1,2,3,A,B,C,D,E" scope="application" />
<select class="grade" title="Grade Obtained">
<c:forEach items="${fn:split(grades, ',')}" var="grade">
<option value="${grade}" ${qd.grade == grade ? 'selected' : ''}>${grade}</option>
</c:forEach>
</select>
This way you end up with more DRY code.

How can I set the default value for an HTML <select> element?

I thought that adding a "value" attribute set on the <select> element below would cause the <option> containing my provided "value" to be selected by default:
<select name="hall" id="hall" value="3">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
However, this did not work as I had expected. How can I set which <option> element is selected by default?
Set selected="selected" for the option you want to be the default.
<option selected="selected">
3
</option>
In case you want to have a default text as a sort of placeholder/hint but not considered a valid value (something like "complete here", "select your nation" ecc.) you can do something like this:
<select>
<option value="" selected disabled hidden>Choose here</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
Complete example:
<select name="hall" id="hall">
<option>1</option>
<option>2</option>
<option selected>3</option>
<option>4</option>
<option>5</option>
</select>
I came across this question, but the accepted and highly upvoted answer didn't work for me. It turns out that if you are using React, then setting selected doesn't work.
Instead you have to set a value in the <select> tag directly as shown below:
<select value="B">
<option value="A">Apple</option>
<option value="B">Banana</option>
<option value="C">Cranberry</option>
</select>
Read more about why here on the React page.
You can do it like this:
<select name="hall" id="hall">
<option> 1 </option>
<option> 2 </option>
<option selected> 3 </option>
<option> 4 </option>
<option> 5 </option>
</select>
Provide "selected" keyword inside the option tag, which you want to appear by default in your drop down list.
Or you can also provide attribute to the option tag i.e.
<option selected="selected">3</option>
if you want to use the values from a Form and keep it dynamic try this with php
<form action="../<SamePage>/" method="post">
<?php
$selected = $_POST['select'];
?>
<select name="select" size="1">
<option <?php if($selected == '1'){echo("selected");}?>>1</option>
<option <?php if($selected == '2'){echo("selected");}?>>2</option>
</select>
</form>
Best way in my opinion:
<select>
<option value="" selected="selected" hidden="hidden">Choose here</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
Why not disabled?
When you use disabled attribute together with <button type="reset">Reset</button> value is not reset to original placeholder. Instead browser choose first not disabled option which may cause user mistakes.
Default empty value
Every production form has validation, then empty value should not be a problem. This way we may have empty not required select.
XHTML syntax attributes
selected="selected" syntax is the only way to be compatible with both XHTML and HTML 5. It is correct XML syntax and some editors may be happy about this. It is more backward compatible. If XML compliance is important you should follow the full syntax.
I prefer this:
<select>
<option selected hidden>Choose here</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
'Choose here' disappears after an option has been selected.
An improvement for nobita's answer. Also you can improve the visual view of the drop down list, by hiding the element 'Choose here'.
<select>
<option selected disabled hidden>Choose here</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
<option value="5">Five</option>
</select>
Another example; using JavaScript to set a selected option.
(You could use this example to for loop an array of values into a drop down component)
<select id="yourDropDownElementId"><select/>
// Get the select element
var select = document.getElementById("yourDropDownElementId");
// Create a new option element
var el = document.createElement("option");
// Add our value to the option
el.textContent = "Example Value";
el.value = "Example Value";
// Set the option to selected
el.selected = true;
// Add the new option element to the select element
select.appendChild(el);
The selected attribute is a boolean attribute.
When present, it specifies that an option should be pre-selected when the page loads.
The pre-selected option will be displayed first in the drop-down list.
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="vw">VW</option>
<option value="audi" selected>Audi</option>
</select>
If you are in react you can use defaultValue as attribute instead of value in the select tag.
If you are using select with angular 1, then you need to use ng-init, otherwise, second option will not be selected since, ng-model overrides the defaul selected value
<select ng-model="sortVar" ng-init='sortVar="stargazers_count"'>
<option value="name">Name</option>
<option selected="selected" value="stargazers_count">Stars</option>
<option value="language">Language</option>
</select>
I would just simply make the first select option value the default and just hide that value in the dropdown with HTML5's new "hidden" feature. Like this:
<select name="" id="">
<option hidden value="default">Select An Option</option>
<option value="1">One</option>
<option value="2">Two</option>
<option value="3">Three</option>
<option value="4">Four</option>
</select>
value attribute of tag is missing, so it doesn't show as u desired selected. By default first option show on dropdown page load, if value attribute is set on tag.... I got solved my problem this way
This example has been tested to work with multiple <select> elements on the page, and can also work with normal text elements. It has not been tested for setting the value to more than one selection when <select multiple="true">, however you can probably modify this sample to support that.
Add an attribute data-selected to each <select> element and set the value(s) to the value of the option you wish to have selected.
Use javascript's querySelectorAll() to select all elements that have the custom attribute you just added.
In the following example, when run, the first <select> should show option with the value user as selected, and the second <select> should show the option with the value admin as selected.
document.querySelectorAll('[data-selected]').forEach(e => {
e.value = e.dataset.selected
});
<select data-selected="user" class="form-control" name="role">
<option value="public">
Pubblica
</option>
<option value="user">
Utenti
</option>
<option value="admin">
Admin
</option>
</select>
<select data-selected="admin" class="form-control" name="role2">
<option value="public">
Pubblica
</option>
<option value="user">
Utenti
</option>
<option value="admin">
Admin
</option>
</select>
I used this php function to generate the options, and insert it into my HTML
<?php
# code to output a set of options for a numeric drop down list
# parameters: (start, end, step, format, default)
function numericoptions($start, $end, $step, $formatstring, $default)
{
$retstring = "";
for($i = $start; $i <= $end; $i = $i + $step)
{
$retstring = $retstring . '<OPTION ';
$retstring = $retstring . 'value="' . sprintf($formatstring,$i) . '"';
if($default == $i)
{
$retstring = $retstring . ' selected="selected"';
}
$retstring = $retstring . '>' . sprintf($formatstring,$i) . '</OPTION> ';
}
return $retstring;
}
?>
And then in my webpage code I use it as below;
<select id="endmin" name="endmin">
<?php echo numericoptions(0,55,5,'%02d',$endmin); ?>
</select>
If $endmin is created from a _POST variable every time the page is loaded (and this code is inside a form which posts) then the previously selected value is selected by default.
This code sets the default value for the HTML select element with PHP.
<select name="hall" id="hall">
<?php
$default = 3;
$nr = 1;
while($nr < 10){
if($nr == $default){
echo "<option selected=\"selected\">". $nr ."</option>";
}
else{
echo "<option>". $nr ."</option>";
}
$nr++;
}
?>
</select>
You can use:
<option value="someValue" selected>Some Value</option>
instead of,
<option value="someValue" selected = "selected">Some Value</option>
both are equally correct.
Set selected="selected" where is option value is 3
please see below example
<option selected="selected" value="3" >3</option>
I myself use it
<select selected=''>
<option value=''></option>
<option value='1'>ccc</option>
<option value='2'>xxx</option>
<option value='3'>zzz</option>
<option value='4'>aaa</option>
<option value='5'>qqq</option>
<option value='6'>wwww</option>
</select>
You just need to put attribute "selected" on a particular option instead direct to select element.
Here is snippet for same and multiple working example with different values.
Select Option 3 :-
<select name="hall" id="hall">
<option>1</option>
<option>2</option>
<option selected="selected">3</option>
<option>4</option>
<option>5</option>
</select>
<br/>
<br/>
<br/>
Select Option 5 :-
<select name="hall" id="hall">
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option selected="selected">5</option>
</select>
<br/>
<br/>
<br/>
Select Option 2 :-
<select name="hall" id="hall">
<option>1</option>
<option selected="selected">2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
Default selected value is Option-4
<html:select property="status" value="OPTION_4" styleClass="form-control">
<html:option value="">Select</html:option>
<html:option value="OPTION_1" >Option-1</html:option>
<html:option value="OPTION_2" >Option-2</html:option>
<html:option value="OPTION_3" >Option-3</html:option>
<html:option value="OPTION_4" >Option-4</html:option>
<html:option value="OPTION_5" >Option-5</html:option>
</html:select>
You will need an "id" attribute in each option for this solution to work:
<script>
function select_option (id,value_selected) {
var select;
select = document.getElementById(id);
if (select == null) return 0;
var option;
option = select.options.namedItem(value_selected);
if (option == null) return 0;
option.selected = "selected";
return true;
}
</script>
<select name="hall" id="hall">
<option id="1">1</option>
<option id="2">2</option>
<option id="3">3</option>
<option id="4">4</option>
<option id="5">5</option>
</select>
<script>select_option ("hall","3");</script>
The function first tries to find the <select> with the id, then it will search for the value_selected in the <select> options and if it finds it, it will set the selected attribute returning true. False otherwise
The problem with <select> is, it's sometimes disconnected with the state of what's currently rendered and unless something has changed in the option list, no change value is returned. This can be a problem when trying to select the first option from a list. The following code can get the first-option the first-time selected, but onchange="changeFontSize(this)" by its self would not. There are methods described above using a dummy option to force a user to make a change value to pickup the actual first value, such as starting the list with an empty value. Note: onclick would call the function twice, the following code does not, but solves the first-time problem.
<label>Font Size</label>
<select name="fontSize" id="fontSize" onfocus="changeFontSize(this)" onchange="changeFontSize(this)">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
<option value="extraLarge">Extra large</option>
</select>
<script>
function changeFontSize(x){
body=document.getElementById('body');
if (x.value=="extraLarge") {
body.style.fontSize="25px";
} else {
body.style.fontSize=x.value;
}
}
</script>
I use Angular and i set the default option by
HTML Template
<select #selectConnection [(ngModel)]="selectedVal" class="form-control col-sm-6 " max-width="100px" title="Select"
data-size="10">
<option >test1</option>
<option >test2</option>
</select>
Script:
sselectedVal:any="test1";
You can try like this
<select name="hall" id="hall">
<option>1</option>
<option>2</option>
<option selected="selected">3</option>
<option>4</option>
<option>5</option>
</select>
To set the default using PHP and JavaScript:
State: <select id="State">
<option value="" selected disabled hidden></option>
<option value="Andhra Pradesh">Andhra Pradesh</option>
<option value="Andaman and Nicobar Islands">Andaman and Nicobar Islands</option>
.
.
<option value="West Bengal">West Bengal</option>
</select>
<?php
if(isset($_GET['State'])){
echo <<<heredoc
<script>
document.getElementById("State").querySelector('option[value="{$_GET['State']}"]').selected = true;
</script>
heredoc;
}
?>
This is simple method to make default option selected.
Can be used for multiple selects on an HTML page.
The method:
Find every select
Read the id and value of that select
Make the option selected
Note:
Every select must have ID to avoid conflict
$(document).ready(function() {
// Loop for every select in page
$('select').each(function(index, id) {
// Get the value
var theValue = $(this).attr('value');
// Get the ID
var theID = $(this).attr('id');
// Make option selected
$('select#' + theID + ' option[value=' + theValue + ']').attr('selected', true);
});
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<select id="sport" name="sport" class="autoselect" value="golf">
<option value="basket">Basket Ball</option>
<option value="tennis">Tennis</option>
<option value="golf">Golf</option>
<option value="bowling">Bowling</option>
</select>
<hr>
<select id="tools" name="tools" class="autoselect" value="saw">
<option value="hammer">Hammer</option>
<option value="drill">Drill</option>
<option value="screwdriver">Screwdriver</option>
<option value="saw">Saw</option>
<option value="wrench">Wrench</option>
</select>
I was having some troubles with it because I need some way to choose the option dynamically accordingly to the value that I have in the database. The script bellow works like a charm to me:
<?php
//pick the value of database
$selected_sexo = $query['s_sexo'];
?>
<select name="s_sexo" id="s_sexo" required>
<option <?php if($selected_sexo == 'M'){echo("selected");}?> value="M">M</option>
<option <?php if($selected_sexo == 'F'){echo("selected");}?> value="F">F</option>
</select>