I have 4 dropdowns namely dd1(parent),dd2,dd3,dd4 with last three dropdowns having null as a initial value.
I use ajax to fetch last three dropdowns from parent dropdown.
I have used php validation(mkValid) to validate whether the dropdowns are empty or not.
The problem is when i select a value in first 2 dropdowns and when i click on submit,it gets validated but the page gets refreshed and the selected value in first two dropdowns becomes empty.So,i need to retain those values until all dropdowns have values.I used ajax.stop() in jquery but no use.Help.!
You must validate the dropdowns clientside.
PHP is serverside and only validate after posting it to the server. This happens after you click "submit".
Show us your code ;)
Prevent the submit with:
$('#form_id').submit(function(e) {
e.preventDefault();
// or
//return false;
})
You can do it like this:
<select name="dropdown">
<option value="1" <?php echo isset($_POST["dropdown"]) && $_POST["dropdown"] == "1" ? "selected"; ?> >Option 1</option>
<option value="2" <?php echo isset($_POST["dropdown"]) && $_POST["dropdown"] == "2" ? "selected"; ?> >Option 2</option>
<option value="3" <?php echo isset($_POST["dropdown"]) && $_POST["dropdown"] == "3" ? "selected"; ?> >Option 3</option>
</select>
You can refer to PHP How can I keep the selected option from a drop down to stay selected on submit? for more information
EDIT: I just read you are using Smarty, in which case you can use:
<select name="dropdown">
<option value="1" {if !empty($dropdown) && $dropdown == "1"}selected{/if}>Option 1</option>
<option value="2" {if !empty($dropdown) && $dropdown == "2"}selected{/if}>Option 2</option>
<option value="3" {if !empty($dropdown) && $dropdown == "3"}selected{/if}>Option 3</option>
</select>
And in your PHP:
global $smarty;
if(isset($_POST["dropdown"])){
//validation
$smarty->assign("dropdown", $_POST["dropdown"]);
}
Related
<select name="title">
<selected value="<?php echo $title; ?>"><?php echo $title; ?></selected>
<option value="Mrs">Mrs.</option>
<option value="Ms">Ms.</option>
<option value="Mr">Mr.</option>
<option value="Dr">Dr.</option>
</select>
I am trying to read a value from a column Title in a MySQL database, which is suppose to read the value, whether it be Mr., Ms., Mrs., then compare it with the values in the drop-down list. It then lets the user select another title to then update the one stored in MySQL database.
I am creating a user profile. So when the user logs in and navigates to the view to edit a profile, he/she should be presented with a drop-down list containing the title he/she selected when registered. Then if he/she wants to they can change the title in the drop-down list and press the update button and it should now update to the new title in the database.
Change your <select> dropdown list in the following way,
<select name="title">
<option value="Mrs"<?php if($title == "Mrs"){ echo " selected='selected'"; } ?>>Mrs.</option>
<option value="Ms"<?php if($title == "Ms"){ echo " selected='selected'"; } ?>>Ms.</option>
<option value="Mr"<?php if($title == "Mr"){ echo " selected='selected'"; } ?>>Mr.</option>
<option value="Dr"<?php if($title == "Dr"){ echo " selected='selected'"; } ?>>Dr.</option>
</select>
There is no selected HTML tag. Use the selected attribute of the option tag:
selected
If present, this Boolean attribute indicates that the option is initially selected. If the <option> element is the descendant of a <select> element whose multiple attribute is not set, only one single <option> of this <select> element may have the selected attribute.1
So consider this example from the Examples section of the Mozilla Developer Network page for <select>:
<!-- The second value will be selected initially -->
<select name="select">
<option value="value1">Value 1</option>
<option value="value2" selected>Value 2</option>
<option value="value3">Value 3</option>
</select>
Your example code can be updated similarly:
<select name="title">
<option value="Mrs" <?php if($title=="Mrs"){ echo "selected"; } ?>>Mrs.</option>
<option value="Ms" <?php if($title=="Ms"){ echo "selected"; } ?>>Ms.</option>
<option value="Mr" <?php if($title=="Mr"){ echo "selected"; } ?>>Mr.</option>
<option value="Dr" <?php if($title =="Dr"){ echo "selected"; } ?>>Dr.</option>
</select>
A simpler way to do this would be to process the names first, using array_reduce():
<?php
$title = 'Dr';
$names = array('Mrs','Ms','Mr','Dr');
$options = array_reduce($names,function($carry,$name) use ($title) {
return $carry .= '<option value="'.$name.'"'.($title == $name?' selected':'').'>'.$name.'.</option>';
});
?>
<select name="title">
<?php echo $options;?>
</select>
See it in action in this playground example.
1https://developer.mozilla.org/en-US/docs/Web/HTML/Element/option
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 have created a profile page in php where a user using an html drop down list chooses gender.
The html code is the following:
Gender<select name="gender">
<option value=" "> EMPTY </option>
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
After the user chooses gender the form dispatches and saves the data into database. All I want is that the next time that the user visits the profile page, the drop down list to keep the value that the user selected before. For example if user selected in drop down list "male", next time he visited profile page to make changes, the drop down list must show "male" as selected value(keeping it from previous time). Any idea how to do this in PHP?
There is a pretty simplistic way to do this if you are using a form. Also, you probably want to use isset() in case it is the first time they visited the page.
<select name="gender">
<option value="male" <?php echo isset($_GET["gender"]) && $_GET["gender"] == "male" ? "selected" : "" ?>>Male</option>
<option value="female" <?php echo isset($_GET["gender"]) && $_GET["gender"] == "female" ? "selected" : "" ?>>Female</option>
</select>
All this does is insert "selected" into the option tag if the $_GET variable is set and the last one was that specific option. Hope this helps!
What you need to do is give the selected attribute to the option. Assuming you store profile information on the session and have short-tags enabled on your server, you can do something like this:
Gender: <select name="gender">
<option value=" "> Not Selected </option>
<option value="Male"<?=$_SESSION['gender'] == "Male" ? ' selected="selected"' : ''?>>Male</option>
<option value="Female"<?=$_SESSION['gender'] == "Female" ? ' selected="selected"' : ''?>>Female</option>
</select>
You must read the database and add a php script like:
<option value="Male" <?php
if ($gender == "Male") { echo " selected"; }
?>>Male</option>
<option value="Female" <?php
if ($gender == "Female") { echo " selected"; }
?>>Female</option>
I need to create a dropdownlist which has two values "OK" and "NOK" one should be selected by default based of an value of $Status. The user should than be able to change the selection or let the default one. How can i manage this? What i have tried:
<select name="Status">
<option value="OK">OK selected="selected"</option>
<option value="NOK">NOK</option>
</select>
<select>
<option value="OK" <?php if($Status == "OK") echo 'selected="selected"';?>>OK</option>
<option value="NOK" <?php if($Status == "NOK") echo 'selected="selected"';?>>NOK</option>
</select>
You can add an id and/or name attribute to the select as you like.