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>
Related
I am using HTML + PHP to build a web App.
I wanted to create an editable form that includes a couple of <select> fields. As an example, we can assume that I want to create a user profile.
The thing is, when new user wants to edit some data about himself, all fields are empty. For the field sex it may look like this:
<select class="form-select" name="sex" required>
<option value="">-</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
or:
<select class="form-select" name="sex" required>
<option selected value="">-</option>
<option value="M">Male</option>
<option value="F">Female</option>
</select>
Whereby default the empty value described by "-" will be displayed.
But what about when the user already has filled that field? Let's assume we have an object variable $user and may access his/hers sex by $user->sex. By default, I want the <select> to be selected with the proper attribute. Using PHP I may go for something like this:
<select class="form-select" name="sex" required>
<?php if ($user->sex == 'M') : ?>
<option value="">-</option>
<option selected value="M">Male</option>
<option value="F">Female</option>
<?php elseif($user->sex == 'F' : ?>
<option value="">-</option>
<option value="M">Male</option>
<option selected value="F">Female</option>
<?php else : ?>
<option selected value="">-</option>
<option value="M">Male</option>
<option value="F">Female</option>
<?php endif; ?>
</select>
But that requires a lot of if, elseif statements. As an example, I've provided only three-optional field. What about when there are more options?
Is there any elegant solution to choose a selected option of <select>, based on a variable (object fetched from database / POST / GET / SESSION - does not really matter), without using so many ifs? At best, I'd like to list possible options only once and then tell the site which is to be chosen by default.
Thanks everyone in advance!
Why do you repeat the options? Just check every option if it needs to be selected...
<select class="form-select" name="sex" required>
<option <?php if($user->sex == "") echo "selected" ?> value="">-</option>
<option <?php if($user->sex == "M") echo "selected" ?> value="M">Male</option>
<option <?php if($user->sex == "F") echo "selected" ?> value="F">Female</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>
my code is written below
<select name="year" required value="<?php echo htmlspecialchars($_POST['year']); ?>" >
<option>----SELECT----</option>
<option value="1">1<sup>st</sup></option>
<option value="2">2<sup>nd</sup></option>
<option value="3">3<sup>rd</sup></option>
<option value="4">4<sup>th</sup></option>
</select>
htmlspecialchars() is not working for select tag.is there any mistake or there is some other way to do it
if you want to set the default value of a select you have to set the option that contains this value as selected like this:
<select name="year" required="required">
<option>----SELECT----</option>
<option <?php if((int)($_POST['year'])==1){?>selected<?php } ?> value="1">1<sup>st</sup></option>
<option <?php if((int)($_POST['year'])==2){?>selected<?php } ?> value="2">2<sup>nd</sup></option>
<option <?php if((int)($_POST['year'])==3){?>selected<?php } ?> value="3">3</sup>rd<sup></b></option>
<option <?php if((int)($_POST['year'])==4){?>selected<?php } ?> value="4">4<sup>rth</sup></option>
</select>
You don't assign a value attribute to a select tag; you assign it to option tags. It's not clear what you are trying to do, but <select ... value="..."> is just invalid HTML. See the specification.
you cannnot give value in <select> tag. Value is given in <option> tag.
like
<select name="year">
<option>----SELECT----</option>
<option value="<?php echo htmlspecialchars($_POST['year']); ?>">
<?php echo htmlspecialchars($_POST['year']); ?></option>
<option value="1">1<sup>st</sup></option>
<option value="2">2<sup>nd</sup></option>
<option value="3">3</sup>rd<sup></b></option>
<option value="4">4<sup>rth</sup></option>
</select>
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";
}
<div>
<select name="mySelect" id="mySelect" class="select" onchange="if(this.options[this.selectedIndex].value != ''){window.top.location.href=this.options[this.selectedIndex].value}">
<option value="english.php">English</option>
<option value="french.php">French</option>
<option value="spanish.php">Spanish</option>
</select>
</div>
i want to change language it work fine when i change the language but in dropdown it always shows english
Add a selected attribute to the option you want to be selected when the page loads.
You need to add another tag that is blank.
Working example:
http://jsfiddle.net/XYSXA/
<select name="mySelect" id="mySelect" class="select" onchange="if(this.options[this.selectedIndex].value != ''){window.top.location.href=this.options[this.selectedIndex].value}">
<option value=""></option>
<option value="english.php">English</option>
<option value="french.php">French</option>
<option value="spanish.php">Spanish</option>
</select>
This way none of the options will be selected by default.
Alternatively, you may use the set the attribute selected inside one of the opening <option> tags to specify which language should be selected by default. Example selecting French by default:
<select name="mySelect" id="mySelect" class="select" onchange="if(this.options[this.selectedIndex].value != ''){window.top.location.href=this.options[this.selectedIndex].value}">
<option value=""></option>
<option value="english.php">English</option>
<option value="french.php" selected>French</option>
<option value="spanish.php">Spanish</option>
</select>
Working example: http://jsfiddle.net/XYSXA/1/
<?
$file=basename($_SERVER['PHP_SELF'],".php");
$file=ucwords($file);
?>
<option value="french.php" <?=($file=="French")?" selected='selected'":""?>>French</option>
I m assuming that you're using the pattern like French for french.php, Spanish For spanish.php,etc