I have a form with multiple select items. I can select multiple items. But how do I select those items which I initially selected, if I wrongly input other fields of the form with PHP ?
<select name="interest[]" class="tr5" multiple="multiple">
<option value="">Select..</option>
<option value="art">Art</option>
<option value="litteratures">Litteratures</option>
<option value="business" >Business</option>
<option value="internet" >Internet</option>
</select>
I can do this without multiple tag with php.
<option value="internet" <?php if(isset($_POST['interest']) && $_POST['interest'] ==
"internet") echo 'selected = "selected"';?>>Internet</option>
Thanks for your help.
You can do like this
<option value="internet" <?php if(isset($_POST['interest']) &&
in_array("internet",$_POST['interest'])) echo 'selected = "selected"';?>>Internet</option>
Related
is that possible create default select in multiple select options but that default select not selected.
like default notification, when we not select anything will displaying "Nothing Selected".
i want to change that "Nothing Selected" for example with "Select Category"
my code
<select multiple class="selectpicker" data-live-search="true" id="nama_kat" name="nama_kat[]" form="tambahposting">
<?php foreach ($data2 as $value): ?>
<div class="form-group">
<option>
<?php echo $value['nama_kat'] ?>
</option>
<?php endforeach; ?>
</select>
You can achieve your result with this example.
You have to add attribute selected which option you want to select by default.
You have to put if condition when you have some value exact match with option value than you can select option by default.
Example
<option value="notification_default" <?php if( $i == 'notification_default'): echo 'selected="selected"'; endif; ?>>Default Notification</option>
You can add this in your option.
<select multiple class="selectpicker" data-live-search="true" id="nama_kat" name="nama_kat[]" form="tambahposting">
<option value="1">test 1</option>
<option value="2">test 2</option>
<option value="3" selected="selected">default notification</option>
<option value="4">test 4</option>
<option value="5">test 5</option>
</select>
I have a drop down list where I select options
<form action="" method="POST" class="styled-select">
<select name="seasons" onchange='this.form.submit()'>
<option value="">Select a Season</option>
<option value="1">2002/2003</option>
<option value="2">2003/2004</option>
<option value="3">2004/2005</option>
<option value="4">2005/2006</option>
<option value="5">2006/2007</option>
<option value="6">2007/2008</option>
<option value="7">2008/2009</option>
<option value="8">2009/2010</option>
<option value="9">2010/2011</option>
<option value="10">2011/2012</option>
<option value="11">2012/2013</option>
<option value="12">2013/2014</option>
</select>
<noscript><input type="submit" value="Submit"></noscript>
</form>
You can see the list here footystat
I am using the following PHP
if(isset($_POST['seasons'])){ $seasonette = $_POST['seasons']; }
if(isset($_POST['year'])){ $yearette = $_POST['year']; }
if(isset($_POST['comp'])){ $competitionette = $_POST['comp']; }
if(isset($_POST['which'])){ $whichette = $_POST['which']; }
When I select something from the list, I want selected item in the list to continue showing. At the moment when I select (for example) 2013/2014, it will show the results but the drop down menu goes back to its original state instead of showing 2013/2014.
Get Option value selected when it gets posted value, like this,
<option value="1" <?php if(isset($_POST['seasons']) && $_POST['seasons'] == '1'){ ?> selected="selected" <?php } ?>>2002/2003</option>
Set value like this for each option
You can set the "selected" property to the option , just like you set a value !
<option value="8" selected>2009/2010</option>
Use a if statement in PHP to determine which one should be selected.
Thats because the page refreshes.
On page load check if there is post variable than match the value with each option's HTML and write selected attribute.
The shorter way is
<option value="1" <?php echo $_POST['seasons']==1?"selected":""; ?>2002/2003</option>
please tell me how to set multiple option value in multiple select box after refreshing the page.
I have dynamic select box with different id like following
<select id="select_1" name="select_1"/>
<option value="a" selected="selected">Data 1</option>
<option value="b">Data 2</option>
</select>
<select id="select_2" name="select_2"/>
<option value="a">Data 1</option>
<option value="b" selected="selected">Data 2</option>
</select>
<select id="select_3" name="select_3"/>
<option value="a" selected="selected">Data 1</option>
<option value="b">Data 2</option>
</select>
I want to selected all option values of all selectbox after refreshing the page with php and ajax.
I'm not sure how your data is populated and presented, but here's an example:
<?foreach($selects as $select):?>
<?$selected = ($select->Selected === true) ? 'selected="selected"' :'';?>
<select id="<?=$select->Id?>" name="<?=$select->Name?>"/>
<?foreach($select->Options as $option):?>
<option value="<?=$option->Value?>"<?=$selected;?>><?=$option->Text?></option>
<?endforeach;?>
<?endforeach;?>
Or
foreach($selects as $select)
{
$selected = ($select->Selected === true) ? 'selected="selected"' :'';
echo "<select id=\"{$select->Id}\" name=\"{$select->Name}\"/>\n";
foreach($select->Options as $option)
{
echo "<option value=\"{$option->Value}\"{$selected}>{$option->Text}</option>\n";
}
echo "</select>\n";
}
I need to get all(Selected and unselected) the values of a multiple select box through POST. How can I do the trick?
Create a select tag like this
<select name="data[]" multiple="multiple" >
<option value="1">a</option>
<option value="2">b</option>
<option value="3">c</option>
</select>
You can get the selected values:
foreach ($_GET['data'] as $selected) {
echo $selected."\n";
}
You can not get the unselected values.
I have a form which is displayed when there is some editing is required in data. This form contains the multiple select box field i.e.,
<select name="multiple" multiple>
<option value="1">asd</option>
<option value="2">asaad</option>
<option value="3">aslan</option>
</select>
Now when i come to this form, I get every existing values populated in their respective fields but i do not get this multiple select box populated. In db the its respective values are stored in csv format like, 2,3,4.
Now how Can i use these csv and repopulate the multiple select box respective of the csv.?
<?
$csvString = "1,3";
?>
<select name="multiple" multiple>
<option value="1" <?=strpos($csvString ,'1')!== false?'selected="selected"':''?> >Option 1</option>
<option value="2" <?=strpos($csvString ,'2')!== false?'selected="selected"':''?> >Option 2</option>
<option value="3" <?=strpos($csvString ,'3')!== false?'selected="selected"':''?> >Option 3</option>
</select>
I assume you generate HTML for selectbox iterating over the $options. Then you just have to check in each iteration if the current value is present in you $csv and add the selected parameter to the option.
$selection = explode(',', $csv);
foreach ($options as $value => $name) {
$selected = in_array($value, $selection) ? ' selected="selected"' : '';
echo '<option value="'. $value .'"'. $selected .'>'. $name .'</option>';
}
This may help but there is probably a better way...
$csvString = "1,3";
$options = explode(",", $csvString);
foreach($options as $val) {
${"opt".$val} = "selected=\"selected\"";
}
?>
<select ...>
<option value="1" <?php echo $opt1 ?> >Option 1</option>
<option value="2" <?php echo $opt2 ?> >Option 2</option>
<option value="3" <?php echo $opt3 ?> >Option 3</option>
</select>