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>
Related
I have a table and I put the values in it with an array
I want to add a select tag that takes the values in each row of the table from the array and displays them in the select and sends the new values through the post method.
please help
<?php for ($i = 0; $i < count($pays_percents); $i++) { ?>
<td>
<select class="form-control" name="pay_method[]" id="pay_method">
<option value="0" <?php if ($pay_method[$i] == '0') echo 'selected=""'; ?>>نقد</option>
<option value="1" <?php if ($pay_method[$i] == '1') echo 'selected=""'; ?>>چک</option>
<option value="2" <?php if ($pay_method[$i] == '2') echo 'selected=""'; ?>>کارت خوان</option>
<option value="3" <?php if ($pay_method[$i] == '3') echo 'selected=""'; ?>>کارت به کارت</option>
</select>
</td>
<?php } ?>
It is unclear what you are expecting. Here are the fixes for the flaws in the code. In particular, ids and names must be unique, so I have appended the iterator. And you don't need to say selected=''.
<?php for ($i = 0; $i < count($pays_percents); $i++) { ?>
<td>
<select class="form-control" name="pay_method<?php echo $i;?>" id="pay_method<?php echo $i;?>">
<option value="0" <?php if ($pay_method[$i] == '0') echo 'selected'; ?>>نقد</option>
<option value="1" <?php if ($pay_method[$i] == '1') echo 'selected'; ?>>چک</option>
<option value="2" <?php if ($pay_method[$i] == '2') echo 'selected'; ?>>کارت خوان</option>
<option value="3" <?php if ($pay_method[$i] == '3') echo 'selected'; ?>>کارت به کارت</option>
</select>
</td>
<?php } ?>
<select name="operator">
<option name="green">green</option>
<option name="purple">purple</option>
<option name="orange">Orange</option>
</select><p></p><br>
<button type = "submit">Check</button>
for ($i=0; $i<=5; $i++) {
echo "<img src = ""
}
<select name="operator">
<option value="green">green</option>
<option value="purple">purple</option>
<option value="orange">Orange</option>
</select><p></p><br>
<button type = "submit">Check</button>
if($request->operator == 'green') {
for ($i=0; $i<=5; $i++) {
echo "<img src = ""
}
}
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
please, i'm somewhat new with php bear with me.
I have a selectbox...
<select name="criteria">
<option value="1">Student ID</option>
<option value="2">Firstname </option>
<option value="3">Lastname</option>
<option value="6">All</option>
</select>
<input name="searchvalue" type="text">
<input name="search" type="submit" value="Search">
When i search with a particular option, after the post request, the selected option usually defaults to Student ID. But i want it to still remain the option i searched with. Any suggestion would be appreciated.
To be honest I'd adjust your select code just a little bit to allow for a bit more dynamic editing..
<?php
$options = array('1'=>'Student ID', '2'=>'Firstname', '3'=>'Lastname', '6'=>'All');
?>
<select name="criteria">
<?php
foreach($options as $key=>$value){
$addtItem = '';
if(isset($_POST['criteria']) && $_POST['criteria']==$key){
$addtItem = 'selected="selected"';
}
echo('<option '.addtItem.' value="'.key.'">'.$value.'</option>');
}
?>
</select>
This way you can quickly add more values without a ton of repeat code..
<select name="criteria">
<option value="1" <?php if ($_POST["criteria"] == 1) echo "SELECTED"; ?>>Student ID</option>
<option value="2" <?php if ($_POST["criteria"] == 2) echo "SELECTED"; ?>>Firstname </option>
<option value="3" <?php if ($_POST["criteria"] == 3) echo "SELECTED"; ?>>Lastname</option>
<option value="6" <?php if ($_POST["criteria"] == 6) echo "SELECTED"; ?>>All</option>
</select>
Really ugly, but that works.
You need to check the $_POST array and select the corresponding select item
<?php
$criteria = isset($_POST['criteria']) ? $_POST['criteria']: 0;
?>
<select name="criteria">
<option value="1" <?php echo ($criteria == '1') ? 'selected="selected"': ''; ?>>Student ID</option>
<option value="2" <?php echo ($criteria == '2') ? 'selected="selected"': ''; ?>>Firstname </option>
<option value="3" <?php echo ($criteria == '3') ? 'selected="selected"': ''; ?>>Lastname</option>
<option value="6" <?php echo ($criteria == '6') ? 'selected="selected"': ''; ?>>All</option>
</select>
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>