I want to set the current date by default but it works only on year's select (the last one).
I wouldn't like to use javascript in this case. Just html and php.
I have already tried "selected='selected'" and "autocomplete=off" as shown in different other posts.
Here my code
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if (sprintf('%02d', $i) == date("d")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<select id="orders-month">
<?php
for ($i=1; $i <= 12; $i++) {
if (sprintf('%02d', $i) == date("m")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if (sprintf('%02d', $i) == date("Y")) {
echo "<option value=\"".sprintf('%02d', $i)."\" selected>".sprintf('%02d', $i)."</option>";
}
else{
echo "<option value=\"".sprintf('%02d', $i)."\">".sprintf('%02d', $i)."</option>" ;
}
}
?>
</select>
<input type="submit" value="OK" />
</form>
here the plain HTML when the PHP has been run:
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<option value="01">01</option>
<option value="02" selected>02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
<option value="13">13</option>
<option value="14">14</option>
<option value="15">15</option>
<option value="16">16</option>
<option value="17">17</option>
<option value="18">18</option>
<option value="19">19</option>
<option value="20">20</option>
<option value="21">21</option>
<option value="22">22</option>
<option value="23">23</option>
<option value="24">24</option>
<option value="25">25</option>
<option value="26">26</option>
<option value="27">27</option>
<option value="28">28</option>
<option value="29">29</option>
<option value="30">30</option>
<option value="31">31</option>
</select>
<select id="orders-month">
<option value="01">01</option>
<option value="02">02</option>
<option value="03" selected>03</option>
<option value="04">04</option>
<option value="05">05</option>
<option value="06">06</option>
<option value="07">07</option>
<option value="08">08</option>
<option value="09">09</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<select id="orders-year">
<option value="2012">2012</option>
<option value="2013">2013</option>
<option value="2014">2014</option>
<option value="2015">2015</option>
<option value="2016">2016</option>
<option value="2017" selected>2017</option>
<option value="2018">2018</option>
<option value="2019">2019</option>
<option value="2020">2020</option>
</select>
<input type="submit" value="OK" />
</form>
This is the code you are looking for
Remember to use single quotes (') when including double quotes inside the PHP echo or else if using double quotes use " inside the outer double quotes.
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if ($i == date("d")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
<select id="orders-month">
<?php
for ($i=1; $i <= 12; $i++) {
if ($i == date("m")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if ($i == date("Y")) {
echo '<option value="'.$i.'" selected>'.$i.'</option>';
}
else{
echo '<option value="'.$i.'">'.$i.'</option>';
}
}
?>
</select>
Can you try like this.
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php
for ($i=1; $i <= 31; $i++) {
if ($i == date("d")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
?>
</select>
<select id="orders-month">
for ($i=1; $i <= 12; $i++) {
if ($i == date("m")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
</select>
<select id="orders-year">
<?php
for ($i=2012; $i <= 2020; $i++) {
if ($i == date("Y")) {
echo "<option value='$i' selected='selected'>$i</option>";
} else {
echo "<option value='$i'>$i</option>";
}
}
?>
</select>
<input type="submit" value="OK" />
</form>
Here's a more compact way of achieving this:
<form id="form-orders-date" onsubmit="selectOrdersDate();">
<select id="orders-day">
<?php for ($d = 1; $d <= 31; $d++) { ?>
<option value="<?php echo $d; ?>" <?php if ($d == date('j')) { echo 'selected'; ?>><?php echo $d; ?></option>
<?php } ?>
</select>
<select id="orders-month">
<?php for ($m = 1; $m <= 12; $m++) { ?>
<option value="<?php echo $m; ?>" <?php if ($m == date('n')) { echo 'selected'; ?>><?php echo $m; ?></option>
<?php } ?>
</select>
<select id="orders-year">
<?php for ($y = 2012; $y <= 2020; $y++) { ?>
<option value="<?php echo $y; ?>" <?php if ($y == date('Y')) { echo 'selected'; ?>><?php echo $y; ?></option>
<?php } ?>
</select>
<input type="submit" value="OK" />
</form>
Have you tried autocomplete="off" for select? It works for me.
I know you said you tried this already, but your sample code doesn't show it. Here's how your tag should be formed:
<select>
<option value="01">01</option>
<option value="02" selected="selected">02</option>
<option value="03">03</option>
...
</select>
Your example above looks very different for the selected element. I'd suggest using whatever inspection tool is built into your web browser to see what exactly was rendered, and that will probably make it the solution obvious.
For reference, see:
Definition of select tag
Definition of option tag
Related
I need multiple values in select2's select box, but it selecting the last one.
I can insert apple, tomato into the database, but only tomato displays in select2 in the browser.
This is the code that I have, that is only selecting one value for the select, not multiple.
<?php if ($views['mb_medical']) $medi_arr = explode('|', $views['mb_medical']);
for ($i = 0; $i < count($medi_arr); $i++) { ?>
<select class="medical_subject" name="mb_medical[]" id="mb_medical" multiple="multiple">
<option value="apple" <?php if($medi_arr[$i] == 'apple') echo 'selected'; ?>>apple</option>
<option value="tomato" <?php if($medi_arr[$i] == 'tomato') echo 'selected'; ?>>tomato</option>
<option value="banana" <?php if($medi_arr[$i] == 'banana') echo 'selected'; ?>>banana</option>
<option value="melon" <?php if($medi_arr[$i] == 'melon') echo 'selected'; ?>>melon</option>
</select>
<?php } ?>
In picture, select2 is missing apple, but it does have tomato.
I have already checked the $medi_arr[$i] outputs correclty.
I am adding a sample code so you can use this code in your code to full-fill your desired task.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/js/select2.min.js" integrity="sha512-2ImtlRlf2VVmiGZsjm9bEyhjGW4dU7B6TNwh/hx/iSByxNENtj3WVE6o/9Lj4TJeVXPi4bnOIMXFIJJAeufa0A==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.13/css/select2.min.css" integrity="sha512-nMNlpuaDPrqlEls3IX/Q56H36qvBASwb3ipuo3MxeWbsQB1881ox0cRv7UPTgBlriqoynt35KjEwgGUeUXIPnw==" crossorigin="anonymous" referrerpolicy="no-referrer" />
<select name="category" class="select2 select2-offscreen" id="category" style="width:100%" data-placeholder="Select Fields" tabindex="-1" multiple>
<option value="">Select Category</option>
<option value="1">Business</option>
<option value="7">Education</option>
<option value="5" selected="selected">Environment</option>
<option value="3" selected="selected">Health</option>
<option value="11">Movies</option>
<option value="4">Science</option>
<option value="2">Sports</option>
<option value="6">Tech</option>
<option value="10">Top Stories</option>
</select>
<script>
$("#category").select2();
</script>
it's work finally.
<?php if ($views['mb_medical']) {
$medi_arr = explode('|', $views['mb_medical']); ?>
<select class="medical_subject" name="mb_medical[]" id="mb_medical" multiple>
<option value="apple"
<?php for ($i = 0; $i < count($medi_arr); $i++) {
if($medi_arr[$i] == 'apple') echo 'selected';} ?>>apple
</option>
<option value="tomato"
<?php for ($i = 0; $i < count($medi_arr); $i++) {
if($medi_arr[$i] == 'tomato') echo 'selected';} ?>>tomato
</option>
<option value="banana"
<?php for ($i = 0; $i < count($medi_arr); $i++) {
if($medi_arr[$i] == 'banana') echo 'selected';} ?>>banana
</option>
<option value="melon"
<?php for ($i = 0; $i < count($medi_arr); $i++) {
if($medi_arr[$i] == 'melon') echo 'selected';} ?>>melon
</option>
</select>
I am trying to retain the value which is selected in my drop down list, I am currently using:
<?php if ($_POST['day']==1) {echo "selected='selected'"; } ?>
to achieve this, I am just wondering if there is a more effective way of doing this because in my day drop down list I have 32 values including default, month 13 including default and year 51 including default. any suggestions would be appreciated.
Use a for loop.
For example, using a month with 31 days:
for($i=1; $i<32; $i++){
echo "<option value='$i'".($i==$_POST["day"] ? " selected" : null).">$i</option>";
}
If $_POST["day"]==1, this will output:
<option value='1' selected>1</option>
<option value='2'>2</option>
<option value='3'>3</option>
<option value='4'>4</option>
<option value='5'>5</option>
<option value='6'>6</option>
<option value='7'>7</option>
<option value='8'>8</option>
<option value='9'>9</option>
<option value='10'>10</option>
<option value='11'>11</option>
<option value='12'>12</option>
<option value='13'>13</option>
<option value='14'>14</option>
<option value='15'>15</option>
<option value='16'>16</option>
<option value='17'>17</option>
<option value='18'>18</option>
<option value='19'>19</option>
<option value='20'>20</option>
<option value='21'>21</option>
<option value='22'>22</option>
<option value='23'>23</option>
<option value='24'>24</option>
<option value='25'>25</option>
<option value='26'>26</option>
<option value='27'>27</option>
<option value='28'>28</option>
<option value='29'>29</option>
<option value='30'>30</option>
<option value='31'>31</option>
Completed:
//Days
echo "<select name='day'><option value='default' disabled>Select a day</option>";
for($i=1; $i<32; $i++){
echo "<option value='$i'".($i==$_POST["day"] ? " selected" : null).">$i</option>";
}
echo "</select>";
//Months
echo "<select name='month'><option value='default' disabled>Select a month</option>";
for($i=0; $i<30; $i++){
echo "<option value='$i'".($i==$_POST["month"] ? " selected" : null).">$i</option>";
}
echo "</select>";
//Years
echo "<select name='year'><option value='default' disabled>Select a year</option>";
for($i=2016; $i<2056; $i++){
echo "<option value='$i'".($i==$_POST["year"] ? " selected" : null).">$i</option>";
}
echo "</select>";
I am building a lightweight school management system, that takes in grades directly from the database, how do I update all values at the same time. Please help
<?php
$mydb->setQuery("SELECT * FROM tblbible");
loadresult();
function loadresult(){
global $mydb;
$cur = $mydb->loadResultList();
foreach ($cur as $result) {
echo '<tr>';
echo '<td> <input type="hidden" name="studentID" value="'. $result->studentID.'" /></td>';
?>
<?php
$rowz = mysql_query("SELECT * FROM tblstudent where studentID = '$result->studentID' ")or die(mysql_error());
$detailz = mysql_fetch_array($rowz);
echo '<td>'.$detailz['name'].' '.$detailz['initial'].' '.$detailz['lastname'].'</td>';
?>
<?php
echo '<td> <select name="studentID[]" class="form-control" required>
<option>Select Grade</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
</select></td>';
echo '<td> <select name="studentID[]" class="form-control" required>
<option>Select Grade</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
</select></td>';
echo '<td> <select name="studentID[]" class="form-control" required>
<option>Select Grade</option>
<option value="4">4</option>
<option value="3">3</option>
<option value="2">2</option>
<option value="1">1</option>
</select></td>';
?>
<?php
echo '</tr>';
}
?>
then the insert function below:
<?php
function doInsert(){
if (isset($_POST['save'])){
if ($_POST['value1'] == "" OR $_POST['value2'] == "" OR $_POST['value3'] == "") {
$messageStats = false;
message("All fields are required!","alert alert-success alert-dismissable");
redirect('index.php?page=5');
}else{
$studentID = $_POST['studentID'];
for($i = 0; $i<10; $i++) {
foreach( ($studentID) as $key => $value ) :
$cla = new Bible();
$value1 = $_POST['value1'];
$value2 = $_POST['value2'];
$value3 = $_POST['value3'];
$cla->sing =$value1;
$cla->verses =$value2;
$cla->prayer =$value3;
$cla->update($studentID);
redirect('index.php?page=5');
message("Report generated successfully!", "info");
endforeach;
}
}
}
}
And I would love to add all those in a one click button.
I have an HTML form that a user can use to edit a row in a mysql database, the PHP script queries the database and then displays the current data in form. I have this working fine for text fields with something like:
Correct Answer:<input type="text" name="answer" value="<?php echo $row['CorrectAnswer']; ?>"><br>
and simple radio set up like:
<?php
if ($row['Hazardous'] == "no"){
?>
Hazardous?:<input type="radio" name="hazardous" value="yes">Yes
<input type="radio" name="hazardous" value="no" checked="checked">No<br>
<?php
}else{
?>
Hazardous?:<input type="radio" name="hazardous" value="yes" checked="checked">Yes
<input type="radio" name="hazardous" value="no" >No<br>
<?php } ?>
I also have a select option element like below:
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
Which im trying to set up, I could use:
<?php if ($row['Category'] == "hazardawareness"){ ?>
Category: <select name="category">
<option value="hazardawareness" selected="selected">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "observation"){ ?>
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation" selected="selected">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "insurance"){ ?>
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance"selected="selected">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "attitude"){ ?>
Category: <select name="category">
<option value="hazardawareness">Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude" selected="selected">Attitude</option>
<option value="knowledge">Gen. Knowledge</option>
</select><br>
<?php }else if ($row['Category'] == "knowledge"){ ?>
Category: <select name="category">
<option value="hazardawareness" >Hazard Awareness</option>
<option value="observation">Observation</option>
<option value="insurance">Insurance</option>
<option value="attitude">Attitude</option>
<option value="knowledge" selected="selected">Gen. Knowledge</option>
</select><br>
<?php } ?>
But perhaps there is a better method to do this without duplicating so much code?
You can add an if clause on your option creation that will at the selected attribute if needed.
<?php $options = array(
"Hazard Awareness" => hazardawareness,
"Observation" => observation,
"Insurance" => insurance,
"Attitude" => attitude
); ?>
<select name="category">
<?php foreach($options as $display => $value) { ?>
<option value='<?= $value ?>' <?php if($row['Category'] == trim($value)) { ?>selected='selected'<?php } ?>>
<?= $display ?>
</option>
<?php } ?>
</select>
I think this will help you to do the work using less code:
<?php
$options = array(
"Hazard Awareness" => hazardawareness,
"Observation" => observation,
"Insurance" => insurance,
"Attitude" => attitude
);
echo 'Category: <select name="category">';
foreach($options as $display => $value) {
if ($row['Category'] == trim($value)) {
echo '<option value="' . $value . '" selected>' . $display .'</option>';
}
else {
echo '<option value="' . $value . '">' . $display . '</option>';
}
}
echo '</select>';
Put your option data in array().
On my main page, I have a simple dropdown box:
<select name="miles" id="miles">
<option value="5">5</option>
<option value="10">10</option>
<option value="15">15</option>
<option value="25">25</option>
<option value="50">50</option>
<option value="75">75</option>
<option value="100">100</option>
</select>
<button type="button" name="button" id="button">Search</button>
When this button is clicked, it performs a JQuery $.load:
$("#miles").load("main_data.php #miles_data");
And here the page button is calling, main_data.php:
<?php
... code to calculate $b
?>
<span id="miles_data">
<option value="5" <?php if ($b == 5) { echo "selected"; } ?>>5</option>
<option value="10" <?php if ($b == 10) { echo "selected"; } ?>>10</option>
<option value="15" <?php if ($b == 15) { echo "selected"; } ?>>15</option>
<option value="25" <?php if ($b == 25) { echo "selected"; } ?>>25</option>
<option value="50" <?php if ($b == 50) { echo "selected"; } ?>>50</option>
<option value="75" <?php if ($b == 75) { echo "selected"; } ?>>75</option>
<option value="100" <?php if ($b == 100) { echo "selected"; } ?>>100</option>
</span>
My goals seem simple: Execute a $.load to another page, calculate an integer and set it to a variable, select the matching dropdown item and return it to the main page.
With my code above, the dropdown box on the main page becomes blank. Does anyone notice a solution?
One possible issue is that the format for a selected 'option' is:
<select>
<option>Volvo</option>
<option selected="selected">Saab</option>
<option>Mercedes</option>
<option>Audi</option>
</select>
Blatantly ripped from here
So you want to have
<option value="5" <?php if ($b == 5) { echo 'selected="selected"'; } ?>>5</option>