Select option default based on database variable [duplicate] - html

This question already has answers here:
HTML <Select> <Option> default based on MySQL data
(4 answers)
Closed 7 months ago.
I have a form, with a simple select box, and I also have a text field that pulls a value. I want to use the value in $Defaultselection as the default value for the select options. So that if $Defaultselection = 4 then the default selected option would be D.
Using PHP/HTML
`$Defaultselection`
contains an integer between 1-4.
<select >
<option value="1">A</option>
<option value="2">B</option>
<option value="3">C</option>
<option value="4">D</option>
</select>

That should do it:
<select >
<option value="1" <?php echo ($Defaultselection == 1)?"selected":""; ?>>A</option>
<option value="2" <?php echo ($Defaultselection == 2)?"selected":""; ?>>B</option>
<option value="3" <?php echo ($Defaultselection == 3)?"selected":""; ?>>C</option>
<option value="4" <?php echo ($Defaultselection == 4)?"selected":""; ?>>D</option>
</select>
or this other one:
<select >
<option value="1" <?php if ($Defaultselection == 1) echo "selected"; ?>>A</option>
<option value="2" <?php if ($Defaultselection == 2) echo "selected"; ?>>B</option>
<option value="3" <?php if ($Defaultselection == 3) echo "selected"; ?>>C</option>
<option value="4" <?php if ($Defaultselection == 4) echo "selected"; ?>>D</option>
</select>

Related

Dropdown option getting selected when it should not

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>

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" }

How to indicate the default value in a dropdown menu

I have a form containing a number of dropdown menu's. Each dropdown has a default option. I know how to set this default value, but I'm not sure on how to indicate this value, so that people know that is the default.
I create the dropdown like this
<?php
for ($mismatch = 0; $mismatch <= 3; $mismatch ++) {
if($mismatch == 1){
$select_mismatch .= "<option value = $mismatch selected > ".$mismatch." (default) </option>";
} else {
$select_mismatch .= "<option value = $mismatch > ".$mismatch." </option>";
}
}
?>
So on the web page, the dropdown looks like:
option 1 (default)
option 2
option 3
So now I indicate the default value by just adding (default) to it. Does anyone know another way to indicate this which is a bit more pleasing to the eye?
Try this:
<select data-placeholder="Please Select" class="required select-full" name="status" >
<option value=""></option>
<option value="1" <?php if ($post['status'] == "1") { ?> selected="true" <?php } ?>>Enable</option>
<option value="0" <?php if ($post['status'] == "0") { ?> selected="true" <?php } ?>>Disable</option>
</select>
maybe like this?
<select name="test">
<option>
1
</option>
<option>
2
</option>
<option selected>
3
</option>
<option>
4
</option>
<option>
5
</option>
</select>
Unless you mean to a drop down hover menu?
if so then you could add a colour to the default option.
you can use a CSS class to indicate.
JSFiddle DEMO
<select name="" id="">
<option value="1">please choose</option>
<option value="2" class="default">default</option>
<option value="3">other item</option>
</select>
.default{
background: #000;
color: #fff;
}

html form many options

Hi I would like to hear if there are alternatives for these forms
so I dont have to write for every single option
1. For the age form
For the country form
<select name="age">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<!--And again and again and... again-->
<option value="99">99</option>
</select>
<select name="country">
<option value="England">England</option>
<option value="Sweeden">Sweeden</option>
<option value="Norway">Norway</option>
<option value="Denmark">Denmark</option>
<option value="Usa">Usa</option>
<option value="Spain">Spain</option>
<option value="Scotland">Scotland</option>
<option value="Ireland">Ireland</option>
<option value="French">French</option>
<option value="Italy">Italy</option>
<option value="Germany">Germany</option>
<option value="Poland">Poland</option>
<option value="Netherland">Netherland</option>
<option value="Australia">Australia</option>
<option value="China">China</option>
<option value="Japan">Japan</option>
<option value="Singapore">Singapore</option>
<option value="Russia">Russia</option>
<option value="Other">Other</option>
</select>
Hope you understand my question. Sos for bad english
There is a HTML "range" input, but it's not the most common input method out there and will require some javascript with it. Other than that, the only reasonable method is PHP. Though, if you do use PHP, you can copy and paste the HTML and then insert it into your HTML file if you don't want that page to have PHP. Below are a few HTML only examples. May not work in all browsers, so your server side code should always validate. http://jsfiddle.net/h96nU/1/
The below code shows a few inputs using various methods. The range input will update to show the range value that is currently selected. The last number input uses the HTML5 pattern attribute.
input:invalid {
border: 1px solid red;
}
input:valid {
border: 1px solid green;
}
<div>
<input id="range" type="range" min="1" max="120" step="1" />
<span id="range_value">0</span>
</div>
<div>
<input type="number" />
</div>
<div>
<input type="text" />
</div>
<div>
<input type="number" min="1" max="120" step="1" id="n1" name="age" pattern="\d+" />
</div>
var input = document.getElementById('range');
input.addEventListener('change',input_change,false);
function input_change(e) {
var target = e.target || e.srcElement;
var value = target.value;
var showdiv = document.getElementById('range_value');
showdiv.textContent = value;
}
More about Data Validation in HTML can be found at https://developer.mozilla.org/en-US/docs/Web/Guide/HTML/Forms/Data_form_validation
For PHP method of an age input loop, look below.
<?php
$min = 1;
$max = 100;
echo '<select name="age">';
for($i = $min; $i <= $max; $i++) {
echo '<option value="'.$i.'">'.$i.'</option>';
}
echo '</select>';
?>
If you are using PHP for example you could do it like this:
<?php
$myArray = ['USA','Australia','ETC ETC ETC'];
echo '<select name="country">';
foreach ($myArray as $value) {
echo '<option value="'.$value.'">';
echo $value;
echo "</option>";
}
echo "</select>";
?>
The output of this example would be :
<select name="country">
<option value="USA">USA</option>
<option value="Australia">Australia</option>
<option value="ETC ETC ETC">ETC ETC ETC</option>
</select>
For your age selectbox:
<?php
$maxAge = 99;
echo '<select name="age">';
for ($i=1; $i < $maxAge; $i++) {
echo '<option value="'.$i.'">';
echo $i;
echo "</option>";
}
echo "</select>";
?>

html select dropdown wont stay selected

I have a select dropdown list on my website(www.irishbonus.comule.com/en/). However when I select an option from the dropdown list and press submit I would like that the option stay selected.
Here is the code for the select:
<form action="" method="post">
<strong> Select Subject:</strong>
<select name="formSubject" class="dropdown">
<option value=">>>">">>>"</option>
<option value="Accounting">Accounting</option>
<option value="Agricultural Science">Agricultural Science</option>
<option value="Agricultural Economics">Agricultural Economics</option>
<option value="Applied Mathematics">Applied Mathematics</option>
<option value="Arabic">Arabic</option>
<option value="Art (jc only)">Art (jc only)</option>
<option value="Biology">Biology</option>
<option value="Business (jc only)">Business (jc only)</option>
<option value="Business Studies">Business Studies</option>
<option value="Chemistry">Chemistry</option>
<option value="Civic (jc only)">Civic (jc only)</option>
<option value="Classical Studies">Classical Studies</option>
<option value="Construction Studies">Construction Studies</option>
//more options
<option value="Typewriting (jc only)">Typewriting (jc only)</option>
</select>
<table width="300px">
<tr>
<td valign="top">
<strong>Insert Mark:</strong>
<input type="text" name="formMark" maxlength="2" size="4" value="<?=$mark;?>"/>
</td>
</tr>
</table>
<input type="submit" />
</form>
You need to use a server-side language for this. You already seem to be using PHP so just add some PHP code to do it. The best solution would be storing your options inside an array and then iterating over the array, outputting the <option> tags and adding the selected attribute if the value matches the submitted value.
<select name="formSubject" class="dropdown">
<?php
$options = ['Accounting', 'Agricultural Science', '...'];
$selected = isset($_POST['formSubject']) ? $_POST['formSubject'] : '';
foreach($options as $option) {
echo '<option value="'.$option.'"'.($selected == $option ? ' selected' : '').'>'.$option.'</option>';
}
?>
</select>
He forgot the array(), that is why the parse error is there
here:
<select name="formSubject" class="dropdown">
<?php
$options = array('Accounting', 'Agricultural Science', '...');
$selected = isset($_POST['formSubject']) ? $_POST['formSubject'] : '';
foreach($options as $option) {
echo '<option value="'.$option.'"'.($selected == $option ? ' selected' : '').'>'.$option.'</option>';
}
?>
</select>
while ($row = $result->fetch_assoc()) {
$selected = "";
if(isset($_GET['spec'])) {
if($_GET['spec'] == $row['id']) {
$selected = "selected='selected'";
}
}
print "<option ". $selected ." value='". $row['id'] ."'>". $row['ime']. "</option>";
}
$result->free();