drop down list keep previous selected value in php - php

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>

Related

Read value from MySQL and compare which option is selected from drop-down list

<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

Retain values in jquery

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"]);
}

How to set selected value in drop list in HTML?

How to set selected value in drop list in HTML in Edit Form?
The selected value came from MySql
This code is not work !! >> selected=""
<select name="TutorGender" id="TutorGender" selected="<?php echo $Gender ?>">
<option value="Male">Male</option>
<option value="Female">Female</option>
</select>
W3Schools does a good job of explaining this here
http://www.w3schools.com/tags/att_option_selected.asp
Your code might look something like this.
<select name="TutorGender" id="TutorGender">
<option value="Male" <?php if($Gender == "Male") echo "selected"; ?>>Male</option>
<option value="Female" <?php if($Gender == "Female") echo "selected"; ?>>Female</option>
</select>
Though my PHP syntax may be a little off, but you should get the idea.
Selected goes on the option tag, not on the select tag.
<select name="TutorGender" id="TutorGender" >
<option value="Male" selected="selected">Male</option>
<option value="Female">Female</option>
</select>
<select name="TutorGender" id="TutorGender">
<option value="Male" <?php if($Gender=='Male') echo 'selected' ?>>Male</option>
<option value="Female" <?php if($Gender=='Female') echo 'selected' ?>>Female</option>
</select>
The selected attribute appears on the option(s) that should be selected, not the select element (if it went on the select element itself, then (since multiple options can be selected at the same time) it would have to take a list of ids and you would need to add an id to each option).
<option value="foo" selected>Foo</option>
Generally you would have an array of values (or of value/human text pairs) that you would loop over. (i.e. you wouldn't hard code your options)
You would generate one option each time you went around the loop, and test to see if you should include a selected attribute each time.
<option <?php if($Gender == 'Male'){ echo(' selected '); } ?> value="Male">Male</option>
<option <?php if($Gender == 'Female'){ echo(' selected '); } ?> value="Female">Female</option>
<select name="TutorGender" id="TutorGender">
<?php
$genders = array( 'Male', 'Female' );
foreach( $genders as $value )
{
$selected = ( $Gender == $value )? ' selected="selected"': '';
echo '<option value="' . $value . '"' . $selected . '>' . $value . '</option>';
}
?>
</select>
just another way of doing it. I prefer to use shorthand if statements for code such as this.
<select name="TutorGender" id=\"TutorGender\">
<option value="Male" <?php echo ($Gender == "Male" ? "selected" : ""); ?>>Male</option>
<option value="Female" <?php echo ($Gender == "Female" ? "selected" : ""); ?>>Female</option>
here is a link to using shorthand "if" statements.
In a case where I needed to use a select as part of a user update form that was populated via a database I found the array option works best. The line by line option can get very unwieldy very quickly. This way you simply need to add an option to the $optionDept array should any new options be required. In this example "$row['Dept']" is from the larger overarching results from the database request.
$optionDept= array('sales','warehouse','inspection','production','accounts','admin','inactive');
$optionCount= count($optionDept);
echo"<select name=\"UserDept\">";
for($x = 0; $x < $optionCount; $x++) {
echo"<option value=".$optionDept[$x];
if($optionDept[$x]==$row['Dept']){echo " selected";}
echo">".$optionDept[$x]."</option>";}
echo"</select</P>
try this
if you want to keep male as selected
<select name="TutorGender" id="TutorGender" >
<option value="Male" selected="selected">Male</option>
<option value="Female">Female</option>
</select>
This worked for me: I send througt the GET method of the submit the value i want to restore, and then in the php, AT THE END OF THE PAGE (if not, default values will be set if the code is before the php) i set those values.
echo "select_language.selectedIndex = ".$_GET['iL'].";";

dropdown list with 2 values

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.

Select field drop down select state based on mysql field and php variable status

I'm creating a user form that requires the functionality for people to come back and finish the form at different times, this requires it to save its content intermittently. The issue is that if a user submits some information, come backs and submits some more, it erases the previous content. To combat this and improve usability I have it set up for the form to call back the content from the database and insert it into the form before the user edits the content. This works great for textareas but not so well with dropdowns etc. I've looked at other code that allows you to give a selected state to content that matches within the database. HOWEVER, unlike all the other examples that have the dropdown content in the database, mine is hard-coded into the site like so:
<div class="block-a">
<label>Likelihood</label>
<select class="large-input" name="questions['.$questions['id'].'][1]">
<option></option>
<option value="1">Negligible</option>
<option value="2">Low</option>
<option value="3">Medium</option>
<option value="4">High</option>
<option value="5">Very High</option>
<option value="N/A">Not Applicable</option>
</select>
</div>
So say at 1pm a user set the likelihood to "4" and then comes back at 3pm all it does now is go back to blank, I want it to communicate with the database and see that "4" is in the database and then set "High" to the selected state.
DOES anyone know how to do this? I've been trying to no success. Cheers
Just to answer your question - assuming the list above is hard-coded and you want to stick to that ...
<option <?php echo (isset($var_which_holds_value) && $var_which_holds_value == '') ? 'selected="selected"' : ''); ?>></option>
<option value="1" <?php echo (isset($var_which_holds_value) && $var_which_holds_value == 1) ? 'selected="selected"' : ''); ?>>Negligible</option>
<option value="2" <?php echo (isset($var_which_holds_value) && $var_which_holds_value == 2) ? 'selected="selected"' : ''); ?>>Low</option>
<option value="3" <?php echo (isset($var_which_holds_value) && $var_which_holds_value == 3) ? 'selected="selected"' : ''); ?>>Medium</option>
<option value="4" <?php echo (isset($var_which_holds_value) && $var_which_holds_value == 4) ? 'selected="selected"' : ''); ?>>High</option>
<option value="5" <?php echo (isset($var_which_holds_value) && $var_which_holds_value == <?php echo (isset($var_which_holds_value) && $var_which_holds_value == '') ? 'selected="selected"' : ''); ?>) ? 'selected="selected"' : ''); ?>>Very High</option>
<option value="N/A" <?php echo (isset($var_which_holds_value) && $var_which_holds_value == 'N/A') ? 'selected="selected"' : ''); ?>>Not Applicable</option>
however there are better ways (in terms of code readability and management) to achieve that.

Categories