How can I retain a option from a select field? - php

I have a <select>:
<select id="countries">
<option value="1">USA</option>
<option value="2">Spain</option>
</select>
The user selects an option, then he press a send button to make a query, thru PHP. The result of the query appears in the same page, so the page reloads.
How do i retain the selected option? when the page reloads??
I mean, if they select spain, how can I see spain again when the page reloads?

You need to first give your select dropdown a name.
For example:
<select id="countries" name="countries">
Then you will have access to the value in your PHP when the form is submitted. The value can be retrieved like this in PHP (after submit):
$countries = $_POST['countries'];
Then you can do something like #JohnConde did by setting the selected attribute with PHP.

Here's a very basic way to do it:
<option value="1"<?php if (1 === (int) $_POST['countries']) echo ' selected="selected"'; ?>>USA</option>
<option value="2"<?php if (2 === (int) $_POST['countries']) echo ' selected="selected"'; ?>>Spain</option>

Related

how to insert value from select list into mysql database

<select id='selectCat' name='selectCat'>
<option disabled selected value='0'>Select Type</option>
<option value='book'>Book</option>
<option value='txtbook'>Text Books</option>
<option value='notes'>Notes</option>
</select>
Above is the code in html.
I want the user to select one option from the select list and want to store that value into my database. But before storing it into database i am just retrieving the selected value as shown below.
$userCaT = mysqli_real_escape_string($conn,$_POST['selectCat']);
echo $userCat;
But it is showing an empty page.
So plz get me out from this problem.
Thanks.

PHP: How to make a select box work and how to stop drop down menu selection from resetting to first option on button click

I'm still relatively new to PHP and I am having some issues.
I have a paramaterized query set up that uses a selected Region and selected Subsystem to print a report. The user selects the Region from a select box with 5 regions hard coded. Then they select a Subsystem from a drop down menu that queries rows from a database.
I have not copied all the code below, just the relevant parts.
Here is my code on the functions page for the listbox:
<?php
function listbox($query, $name, $previous, $db){
$result=mysqli_query($db,$query) or die("Query ($query) is incorrect!");
print "<select name=\"$name\">\n";
while ($row = mysqli_fetch_row($result)){ //Table body
if($row[0] == $previous){
print "\t<option value=\"$row[0]\" selected=\"selected\">$row[0]</option>\n";
}else{
print "\t<option value=\"$row[0]\">$row[0]</option>\n";
}
}
print "</select>";
}
?>
Here is my code on the index page that calls the function page:
<?php
$default = "";
$previous = "";
$name4 = "region";
$name1 = "sub";
?>
<select name="<?php print $name4 ?>" size="5">
<option value="Option 1" selected>West</option>
<option value="Option 2">North Central</option>
<option value="Option 3">South Gulf</option>
<option value="Option 4">North East</option>
<option value="Option 5">South Atlantic</option>
</select>
<?php listbox($SubsystemListbox_strSQL, $name1, $previous, $db) ?>
if(isset($_POST['Submit'])){
$ByRegionCap_strSQL = "CALL `unit_costs_db`.`Subsystem_Average_Capital_Costs`('{$_POST[$name4]}', '{$_POST[$name1]}');";
}
as of right now, the region select box doesn't work at all, so I have been using code similar to the subsystem listbox:
listbox($RegionListbox_strSQL, $name4, $previous, $db)
Also, right now after the submit button is clicked, both selected option values reset to the first value in the dropdown. I would like to have both boxes remain showing their previously selected values after the submit button has been clicked.
If anyone has any ideas on how to help, please let me know!!
thanks in advance :)
When you reload your page, all your HTML elements go back to the initial state.
You have to get the content of the select via HTTP POST and set it the right option to "selected"
do you submit the form to the same script?
then you could do smth like:
<select name="<?php print $name4 ?>" size="5">
<option value="Option 1" <?=$_POST[$name4] == 'West' ? 'selected' : ''?>>West</option>
<option value="Option 2" <?=$_POST[$name4] == 'North Central' ? 'selected' : ''?>>North Central</option>
<option value="Option 3" <?=$_POST[$name4] == 'South Gulf' ? 'selected' : ''?>>South Gulf</option>
...
</select>
if it is another script you may save the data in the session and access over $_SESSION instead of $_POST.
Might not be relevant, but the first thing that I see is the way you print your options:
print "\t<option value=\"$row[0]\">$row[0]</option>\n";
PHP won`t let you do this for an array. You need to break your string:
print "\t<option value=\"".$row[0]."\">".$row[0]."</option>\n";
Or use curly braces:
print "\t<option value=\"{$row[0]}\">{$row[0]}</option>\n";
To preserver the selected values you need to keep track of them and manually select the needed values. In your case - check if the $previous variable is properly filled with data.

How to keep showing selected option from drop down list?

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>

set select <option> as selected in form

I have a form wich inserts some data in a mysql database.
This form contain a select and some options with their respective values like
<select name="car_type">
<option value="sport">Sports car</option>
<option value="van">Van</option>
<option value="large">Large family sedan</option>
<option value="small">Small city car</option>
</select>
The form can also be used to update a car's details in the database, it does so by loading the values from the database and fills the form automatically but I am stuck at making the <option> in the select, selected by default based on the value already set in the DB.
So if the user chooses to edit a car, lets say a car that already has Sports type filled in the DB, I want the form to automatically set the <option value="sport">Sports car</option> as selected, <option selected="selected" value="sport">Sports car</option>. By not doing this, the user has to choose again the type every time he submits the form, otherwise the first <option> and its value (sport) is sent by POST.
I am able to retrieve the value from the database by using $data['type'] but I did not find the exact php code to set the selected <option> to that in the database, can you guys help ?
Although the code looks messy, you can do something like this:
<select name="car_type">
<option value="sport" <?php if($data['type']=='sport') echo "selected='selected'"; ?> >Sports car</option>
<option value="van" <?php if($data['type']=='van') echo "selected='selected'"; ?>>Van</option>
<option value="large" <?php if($data['type']=='large') echo "selected='selected'"; ?>>Large family sedan</option>
<option value="small" <?php if($data['type']=='small') echo "selected='selected'"; ?>>Small city car</option>
</select>
<option value="large" <?php echo ($dbvalue=="large") ? "selected=\"selected\"" : "" ;?>>Large family sedan</option>
Work out the sql query bit yourself and replace my $dbvalue with the column data from your db
If you're trying to mark the active car type I would do it like this:
foreach ($car_types as $car_type){
echo '<option value="'.$car_type.'" '.($data['type']==$car_type?'selected':'').'>'.$car_type.'</option>';
}

How to receive the value in the form submit php

<select name="gender" id="Gender">
<option selected="selected" value="woman">女士</option>
<option value="man">先生</option>
</select>
the above code is in a form, when submit the form to test.php. how to get the selected value?女士 or 先生.
i know this can get.
if($_POST['gender']=='woman'){
$_POST['gender']="女士";
}else{
$_POST['gender']="先生";
}
is there a way to get the text. thank you
Use the option text as option value. Like this,
<option selected="selected" value="女士">女士</option>
<option value="先生">先生</option>
But keep in mind any one can change the gender and become something else.
<option value="transexual">transexual</option>
To prevent this its better you also maintain a white list in the server side code.
$gender == isset($_POST['gender'])? $_POST['gender']: "";
if(!in_array(array("女士", "先生"), $gender)){
$gender = "先生"; // set the default gender.
}

Categories