Button to reset the selected option in dropdown list - php

I have a page with 2 select option dropdown lists and I can only use one of them each time I have to submit the selected option but if I use two and click on submit i get an error Only use one list and thats how it should work.
for example:
I have 2 drop lists like this one
if i choose both like the pic above and click on submit I get an error and the selected options are shown as the deffult option and thats what I want,
but when I click on the reset button it need to reset them to the default value but nothing happens.
like this
The Options for the dropdown list is from two different arrays.
$test1 and $test2 are two different arrays
<form name="rechner" method = "POST">
<select name="one">
<option value="" selected hidden>Choose One</option>
<?php foreach ($test1 as $one => $value) { ?>
<option value="<?= $value ?>"
<?php if ($_POST['one'] == $value) {
echo 'selected="selected"';}?> ><?= $value ?>
</option><?php
}
?>
</select>
<select name="two">
<option value="" selected hidden>Choose one</option>
<?php foreach ($test2 as $two => $value) { ?>
<option value="<?= $value ?>"
<?php if ($_POST['two'] == $value) {
echo 'selected="selected"';}?> ><?= $value ?>
</option><?php
}
?>
</select>
<input type="submit" name="calc" value="Submit" />
<input type="reset" name="reset" value="Reset">
</form>
if ($_POST['calc'] == "Berechnen") {
/// do something
}

found the answer on other question
Here
<script type="text/javascript">
function resetForm($myform) {
$myform.find('input:password, input:file, select, textarea').val('');
$myform.find('input:radio,input:checkbox')
.removeAttr('checked').removeAttr('selected);
}
</script>
<form name="rechner" method = "POST" id="myform">
/////html code/////
<input type="button" value="Reset" onclick="resetForm($('#myform'));">
</form>

Related

keep selected values in select box field after search

I want to keep the selected values in select box after search .This is a vehicle search filter page and Page is coded using php and connected with database.
Database have 2 columns for both options that is driver age for 21-24 and driver_age for 25+ as some vehicles also available for both age of drivers
code below
<script>
$('#driver').change(function() {
if($('#driver option:selected').val() == '21-24') {
$('#driver').attr('name','driverage');
}
if($('#driver option:selected').val() == '25+') {
$('#driver').attr('name','driver_age');
}
});
</script>
<form id="myform" method="GET" action="">
<div class="fields">
<p>Vehicle Type</p>
<select class="car" name="car_type" id="car_type">
<option value="0">Select Vehicle</option>
<?php if(count($vehicleType) > 0 ){
foreach($vehicleType as $vt){ ?>
<option value="<?php echo $vt ?>" <?=isset($_GET['car_type'])&&$_GET['car_type']==$vt?'selected':'';?> ><?php echo $vt ?></option>
<?php }
} ?>
</select>
</div>
<div class="fields">
<p>Age of Youngest Driver</p>
<select class="half" id="driver" name="">
<option value="21-24">21-24</option>
<option value="25+">25+</option>
</select>
</div>
<input type="hidden" name="search" />
<div class="fields" style="text-align:center">
<input type="submit" value="Search" class="mitsub" />
</div>
</form>
Thanks and please help
You can add a conditional statement inside your foreach() that adds selected attribute to option. This will ensure that the selected value is selected in the list of options.
<?php
if(count($vehicleType) > 0 ){
foreach($vehicleType as $vt){
echo "<option value='$vt'";
if(isset($_GET['car_type']) && $_GET['car_type'] == $vt){
echo " selected";
}
echo ">$vt</option>\r\n";
}
}
?>
You can do this all in one, you just have to make sure the logic is correct.
echo "<option value='$vt'" . ((isset($_GET['car_type'] && $_GET['car_type'] == $vt) ? " selected" : "") . ">$vt</option>\r\n";
I just find it easier to work with a full if statement.

Get previous value of select option

Is there a way to get the previous value of select option?
the previous value is Red
If i change it to green it will echo the selected color
How can i echo the previous color which is red?
this is my code : thanks :)
<form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color'];
echo "You have selected :" .$selected_val . "<p>";
echo "The previous value is :" ;
}
?>
Use hidden field to store current value. On form post, check if the current value is different than selected value show it.
Pro tip: Always encrypt your hidden field data
store the previous value in your form as hidden value and get it
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color'];
$previous = $_POST['previous'];
}
?>
<form action="#" method="post">
<select name="Color">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="hidden" name="previous" value="<?php echo $selected_val; ?>"/>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php echo "You have selected :" .$selected_val . "<p>";
echo "The previous value is :".$previous ; ?>
Since you must never trust user submitted content, another way is to build your form from PHP and do the test on the server side.
Example:
<?php
$colors = Array('Red', 'Green', 'Blue', 'Pink', 'Yellow');
?>
<form method="POST">
<select name="Color"><?php
foreach($colors as $color)
{
printf(' <option value="%1$s">%1$s</option>'."\n", $color);
}
?>
</select>
<input type="submit">
</form>
<?php
if (!empty($_POST['Color']))
{
$previous_index = array_search($_POST['Color'], $colors) - 1;
// wrap around if the color is the first
if ($previous_index < 0) {
$previous_index = count($colors);
}
printf(
"You have selected <q>%s</q>, the previous value is: %s\n",
$_POST['Color'],
$colors[ $previous_index ]
);
}
?>
The code is obviously longer than it could for educative purpose.
Here is another solution using jQuery, as a bonus you keep track of whole history and not just previous selection.
I have not tested the PHP part, but you get the idea.
HTML
<form action="#" method="post">
<select name="Color" onChange="updateHistory(this);">
<option value="Red">Red</option>
<option value="Green">Green</option>
<option value="Blue">Blue</option>
<option value="Pink">Pink</option>
<option value="Yellow">Yellow</option>
</select>
<input type="hidden" id="history" name="history" value="">
</div>
</div>
<input type="submit" name="submit" value="Get Selected Values" />
</form>
<?php
if(isset($_POST['submit'])){
$selected_val = $_POST['Color'];
$hist = explode(',',$_POST['history']);
// pop the last value, which is current selection
array_pop($hist);
// get the last value, which now is previous
$last_valaue = end($hist);
echo "You have selected :" .$selected_val . "<p>";
echo "The previous value is :" . $last_valaue ;
echo 'DEBUG:<br>';
var_dump($hist);
}
?>
JS
function updateHistory(el){
var val = $(el).val();
var hist = $('#history').val();
if (hist.length > 0) {hist += ','}
hist += val;
$('#history').val(hist);
}
Example JS Fiddle
UPDATE 1:
You can further manipulate history array in PHP to your needs, for example, if you need unique values, you can use array_unique() (http://php.net/manual/en/function.array-unique.php)

Handling isset and select box in php html and for loop

Here is my code. This needs some optimization if you think the below code is bad and someone can really help me
I'm using two if condition and two else condition. Is there anyway this can be fine-tuned.
In the search box i need to retain the previous selected book name. i.e when the submit button is clicked the select box should display the result with the
selected item in the select box. for ex: if in dropdown if i select "book1" and click the submit button, the result should be displayed along with the "book1" as default inside the select box. in my case, the search box is getting reset on each submit click.
My Code:
<?php
if(isset($_POST['submit'])){
$get_project_id = $_POST['project_name'];
if($get_project_id == 1){
..display all the books(mysql query)..
}else{
..display the particular book(mysql query)..
}
}else{
..if the search button is not clicked then display all the books(mysql query)..
}
?>
<form method="post" name="book_list">
<select name="book_list" id="book_list">
<option value="1">All Books</option>
<?php while($row=mysql_fetch_array($result_books)){?>
<option value="<?php echo $row['Project_Auto_Id'];?>">
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>
</select>
<input type="submit" name="submit" class="btn btn-default pull-right" value="Search"/>
</form>
Any help will be appreciable.
Than,ks
Kimz
You can do it by checking the condition inside while loop. Checking the value of option and the post value are same or not as shown in the below code.
<form method="post" name="book_list">
<select name="book_list" id="book_list">
<option value="1">All Books</option>
<?php while($row=mysql_fetch_array($result_books)){?>
<option <?php if(isset($_POST['book_list']) && ($_POST['book_list'] == $row['Project_Auto_Id'])) { echo 'selected'; } ?> value="<?php echo $row['Project_Auto_Id'];?>">
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>
</select>
<input type="submit" name="submit" class="btn btn-default pull-right" value="Search"/>
</form>
Not sure if there is any better way to do the two if... else statements but even if there is there's nothing wrong with the way you've done it, just a different way.
With regards to having the option selected after submitting you need to change your output of the options to this:
<?php while($row=mysql_fetch_array($result_books)){?>
<option value="<?php echo $row['Project_Auto_Id'];?>"<?php if(isset($_POST['book_list']) && $_POST['book_list'] == $row['Project_Auto_Id']) echo ' selected'; ?>>
<?php echo $row['Book_Id']. " - ".$row['Book_Name'] ;?>
</option>
<?php }?>

Get an output from a <select> field with $_POST when a submit button is clicked

I'm making a rock paper scissors game and players need to choose if they want to play best out of 1, 3, 5 or 7 before they start the game and i need it to work with a Select field and a submit button.
I am very new to the select tag but it would suit me best if the selected number could be exctracted with $_POST or $_GET
This is the form:
<form method="post">
<h1>Best out of </h1>
<select>
<option value="one">1</option>
<option value="three">3</option>
<option value="five">5</option>
<option value="seven">7</option>
</select>
<input type="submit" name="start" value="START" />
</form>
This is the PHP:
<?php
if(isset($_POST['start']) && isset($_POST['one']))
{
echo "do something 1";
};
if(isset($_POST['start']) && isset($_POST['three']))
{
echo "do something 2";
};
if(isset($_POST['start']) && isset($_POST['five']))
{
echo "do something 3";
};
if(isset($_POST['start']) && isset($_POST['seven']))
{
echo "do something 4";
};
?>
Give your select a name attribute
<form method="post">
<h1>Best out of </h1>
<select name="my_select">
<option value="one">1</option>
<option value="three">3</option>
<option value="five">5</option>
<option value="seven">7</option>
</select>
<input type="submit" name="start" value="START" />
</form>
After submitting your form in the PHP script you can get select value using $_POST array with index my_select ( name attribute value of the select element):
<?php
if(isset($_POST['start']) && isset($_POST['my_select']))
{
if($_POST['my_select'] === 'one'){}
....
}
?>

How to preSelect an html dropdown list with php? [duplicate]

This question already has answers here:
Using $_POST to get select option value from HTML
(8 answers)
Closed 6 years ago.
I am trying to get the option selected using PHP, but I ran out of ideas!
Below is the code I have tried until now:
<select>
<option value="1">Yes</options>
<option value="2">No</options>
<option value="3">Fine</options>
</select>
<input type="text" value="" name="name">
<input type="submit" value="go" name="go">
So, what do I have to do?
Programmers are lazy...er....efficient....I'd do it like so:
<select><?php
$the_key = 1; // or whatever you want
foreach(array(
1 => 'Yes',
2 => 'No',
3 => 'Fine',
) as $key => $val){
?><option value="<?php echo $key; ?>"<?php
if($key==$the_key)echo ' selected="selected"';
?>><?php echo $val; ?></option><?php
}
?></select>
<input type="text" value="" name="name">
<input type="submit" value="go" name="go">
<select>
<option value="1" <?php if ($myVar==1) echo 'selected="selected"';?>>Yes</options>
<option value="2" <?php if ($myVar==2) echo 'selected="selected"';?>>No</options>
<option value="3" <?php if ($myVar==3) echo 'selected="selected"';?>>Fine</options>
</select>
<input type="text" value="" name="name">
<input type="submit" value="go" name="go">
This is a very simple and straightforward way, if I understand your question correctly.
you can use this..
<select name="select_name">
<option value="1"<?php echo(isset($_POST['select_name'])&&($_POST['select_name']=='1')?' selected="selected"':'');?>>Yes</option>
<option value="2"<?php echo(isset($_POST['select_name'])&&($_POST['select_name']=='2')?' selected="selected"':'');?>>No</option>
<option value="3"<?php echo(isset($_POST['select_name'])&&($_POST['select_name']=='3')?' selected="selected"':'');?>>Fine</option>
</select>
First of all give a name to your select. Then do:
<select name="my_select">
<option value="1" <?= ($_POST['my_select'] == "1")? "selected":"";?>>Yes</options>
<option value="2" <?= ($_POST['my_select'] == "2")? "selected":"";?>>No</options>
<option value="3" <?= ($_POST['my_select'] == "3")? "selected":"";?>>Fine</options>
</select>
What that does is check if what was selected is the same for each and when its found echo "selected".
I suppose that you are using an array to create your select form input.
In that case, use an array:
<?php
$selected = array( $_REQUEST['yesnofine'] => 'selected="selected"' );
$fields = array(1 => 'Yes', 2 => 'No', 3 => 'Fine');
?>
<select name=‘yesnofine'>
<?php foreach ($fields as $k => $v): ?>
<option value="<?php echo $k;?>" <?php #print($selected[$k]);?>><?php echo $v;?></options>
<?php endforeach; ?>
</select>
If not, you may just unroll the above loop, and still use an array.
<option value="1" <?php #print($selected[$k]);?>>Yes</options>
<option value="2" <?php #print($selected[$k]);?>>No</options>
<option value="3" <?php #print($selected[$k]);?>>Fine</options>
Notes that I don't know:
how you are naming your input, so I made up a name for it.
which way you are handling your form input on server side, I used $_REQUEST,
You will have to adapt the code to match requirements of the framework you are using, if any.
Also, it is customary in many frameworks to use the alternative syntax in view dedicated scripts.
I use inline if's
($_POST['category'] == $data['id'] ? 'selected="selected"' : false)
I have 2 php files and i made this, and it works.
(this is an example) the first code is from the one file and the second code from two file.
<form action="two.php" method="post">
<input type="submit" class="button" value="submit" name="one"/>
<select name="numbers">
<option value="1"> 1 </option>
<option value="2"> 2 </option>
<option value="3"> 3 </option>
</select>
</form>
if(isset ($_POST['one']))
{
if($_POST['numbers']=='1')
{
$a='1' ;
}
else if($_POST['numbers']=='2')
{
$a='2' ;
{
else if ($_POST['numbers']=='3')
{
$a='3' ;
}
}
This answer is not relevant for particular recepient, but maybe useful for others.
I had similiar issue with 'selecting' right 'option' by value returned from database.
I solved it by adding additional tag with applied display:none.
<?php
$status = "NOT_ON_LIST";
$text = "<html>
<head>
</head>
<body>
<select id=\"statuses\">
<option value=\"status\" selected=\"selected\" style=\"display:none\">$status</option>
<option value=\"status\">OK</option>
<option value=\"status\">DOWN</option>
<option value=\"status\">UNKNOWN</option>
</select>
</body>
</html>";
print $text;
?>
This is the solution that I've came up with:
<form name = "form1" id = "form1" action = "#" method = "post">
<select name = "DropDownList1" id = "DropDownList1">
<?php
$arr = array('Yes', 'No', 'Fine' ); // create array so looping is easier
for( $i = 1; $i <= 3; $i++ ) // loop starts at first value and ends at last value
{
$selected = ''; // keep selected at nothing
if( isset( $_POST['go'] ) ) // check if form was submitted
{
if( $_POST['DropDownList1'] == $i ) // if the value of the dropdownlist is equal to the looped variable
{
$selected = 'selected = "selected"'; // if is equal, set selected = "selected"
}
}
// note: if value is not equal, selected stays defaulted to nothing as explained earlier
echo '<option value = "' . $i . '"' . $selected . '>' . $arr[$i] . '</option>'; // echo the option element to the page using the $selected variable
}
?>
</select> <!-- finish the form in html -->
<input type="text" value="" name="name">
<input type="submit" value="go" name="go">
</form>
The code I have works as long as the values are integers in some numeric order ( ascending or descending ). What it does is starts the dropdownlist in html, and adds each option element in php code. It will not work if you have random values though, i.e: 1, 4, 2, 7, 6. Each value must be unique.

Categories