How can I use PHP to set the select option to the one that was chosen when the form was sent? This is my code where I am stuck:
<?php
if(isset($_POST['btSubmit'])) {
$selectedOption = $_POST['s']; // Value of selected option … but how to use it below?
}
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
<select name="s">
<option value="" selected disabled>-- choose one --</option>
<option value="a">choose a</option>
<option value="b">choose b</option>
</select>
<input type="submit" name="btSubmit">
</form>
So when you send the form with a selected on the next load of the page the a should be selected by default.
Just add the selected="selected" attribute to the option tag that you received under $_POST['s'].
<?php
$selectedOption = '';
if(isset($_POST['btSubmit'])) {
$selectedOption = $_POST['s']; // Value of selected option … but how to use it below?
}
function injectSelectedAttribute($selectedOption, $option_value){
return strtolower($selectedOption) === strtolower($option_value) ? 'selected="selected"' : '';
}
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
<select name="s">
<option value="" disabled>-- choose one --</option>
<option value="a" <?php echo injectSelectedAttribute($selectedOption, 'a'); ?>>choose a</option>
<option value="b" <?php echo injectSelectedAttribute($selectedOption, 'b'); ?>>choose b</option>
</select>
<input type="submit" name="btSubmit">
</form>
You can further improve this to echo HTML of the options via looping on a PHP array of select options.
Try this,
<?php
if(isset($_POST['btSubmit'])) {
$selectedOption = $_POST['s']; // Value of selected option … but how to use it below?
}
?>
<form action="<?php echo $_SERVER['SCRIPT_NAME']; ?>" method="POST">
<select name="s">
<option value="" selected disabled>-- choose one --</option>
<option value="a" <?php echo $selectedOption=='a'?'selected':'' ;?>>choose a</option>
<option value="b" <?php echo $selectedOption=='b'?'selected':'' ;?>>choose b</option>
</select>
<input type="submit" name="btSubmit">
</form>
Related
Im tryng to create a simple application that when you click on the send button it removes the
option that is selected. I only can use PHP and no JS.
<form action="prueba.php" method="POST">
<select name="cars" id="cars">
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
<input type="submit" value="Eliminar">
<form>
You can do this with some sort of data storage. In this case I'm using sessions, but you could use a database.
<?php
session_start();
if(!isset($_SESSION['removed_options'] )){
$_SESSION['removed_options'] = [];
}
if(isset($_POST['Eliminar'])){
$_SESSION['removed_options'][] = $_POST['cars'];
}
$options = ["volvo", "saab", "opel", "audi"];
?>
<form action="prueba.php" method="POST">
<select name="cars" id="cars">
<?php foreach(array_diff($options, $_SESSION['removed_options']) as $option): ?>
<option value="<?= $option ?>"><?= ucfirst($option) ?></option>
<?php endforeach; ?>
</select>
<input type="submit" value="Eliminar" name="Eliminar">
<form>
You could also possibly store the options in a hidden field on the frontend now that I think about it.
Consider:
<form method="get" action="">
<select name="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<select name="location">
<option value="x">x</option>
<option value="y">y</option>
</select>
<input type="submit" value="Submit" class="submit" />
</form>
On submitting the form, how do I make sure that the selected values remain selected in the dropdowns? This form is inside WordPress (PHP).
To avoid many if-else structures, let JavaScript do the trick automatically:
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>
<select name="location" id="location">
<option value="x">x</option>
<option value="y">y</option>
</select>
<script type="text/javascript">
document.getElementById('location').value = "<?php echo $_GET['location'];?>";
</script>
<select name="name">
<option <?php if ($_GET['name'] == 'a') { ?>selected="true" <?php }; ?>value="a">a</option>
<option <?php if ($_GET['name'] == 'b') { ?>selected="true" <?php }; ?>value="b">b</option>
</select>
After trying all these "solutions", nothing work. I did some research on W3Schools before and remember there was explanation of keeping values about radio.
But it also works for the Select option. See below for an example. Just try it out and play with it.
<?php
$example = $_POST["example"];
?>
<form method="post">
<select name="example">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
If you are using WordPress (as is the case with the OP), you can use the selected function.
<form method="get" action="">
<select name="name">
<option value="a" <?php selected( isset($_POST['name']) ? $_POST['name'] : '', 'a' ); ?>>a</option>
<option value="b" <?php selected( isset($_POST['name']) ? $_POST['name'] : '', 'b' ); ?>>b</option>
</select>
<select name="location">
<option value="x" <?php selected( isset($_POST['location']) ? $_POST['location'] : '', 'x' ); ?>>x</option>
<option value="y" <?php selected( isset($_POST['location']) ? $_POST['location'] : '', 'y' ); ?>>y</option>
</select>
<input type="submit" value="Submit" class="submit" />
</form>
Since WordPress already uses jQuery you can try something like this:
var POST=<?php echo json_encode($_POST); ?>;
for(k in POST){
$("#"+k).val(POST[k]);
}
JavaScript only solution:
var tmpParams = decodeURIComponent(window.location.search.substr(1)).split("&");
for (var i = 0; i < tmpParams.length; i++) {
var tmparr = tmpParams[i].split("=");
var tmp = document.getElementsByName(tmparr[0])[0];
if (!!tmp){
document.getElementsByName(tmparr[0])[0].value = tmparr[1];
}
}
Or if you are using jQuery you can replace
var tmp = document.getElementsByName(tmparr[0])[0];
if (!!tmp){
document.getElementsByName(tmparr[0])[0].value = tmparr[1];
}
with:
$('*[name="'+tmparr[0]+'"]').val(tmparr[1]);
Try this solution for keep selected value in dropdown:
<form action="<?php echo get_page_link(); ?>" method="post">
<select name="<?php echo $field_key['key']; ?>" onchange="javascript:
submit()">
<option value="">All Category</option>
<?php
foreach( $field['choices'] as $key => $value ){
if($post_key==$key){ ?>
<option value="<?php echo $key; ?>" selected><?php echo $value; ?></option>
<?php
}else{?>
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php }
}?>
</select>
</form>
This works for me!
<label for="reason">Reason:</label>
<select name="reason" size="1" id="name" >
<option value="NG" selected="SELECTED"><?php if (!(strcmp("NG", $_POST["reason"]))) {echo "selected=\"selected\"";} ?>Selection a reason below</option>
<option value="General"<?php if (!(strcmp("General", $_POST["reason"]))) {echo "selected=\"selected\"";} ?>>General Question</option>
<option value="Account"<?php if (!(strcmp("Account", $_POST["reason"]))) {echo "selected=\"selected\"";} ?>>Account Question</option>
<option value="Other"<?php if (!(strcmp("Other", $_POST["reason"]))) {echo "selected=\"selected\"";} ?>>Other</option>
</select>
Just change this line:
<input type="submit" value="Submit" class="submit" />
with this line:
<input type="submit" value="Submit/Reload" class="submit" onclick="history.go(-1);">
I don't work in WordPress much, but for forms outside of WordPress, this works well.
PHP
location = ""; // Declare variable
if($_POST) {
if(!$_POST["location"]) {
$error .= "Location is required.<br />"; // If not selected, add string to error message
}else{
$location = $_POST["location"]; // If selected, assign to variable
}
HTML
<select name="location">
<option value="0">Choose...</option>
<option value="1" <?php if (isset($location) && $location == "1") echo "selected" ?>>location 1</option>
<option value="2" <?php if (isset($location) && $location == "2") echo "selected" ?>>location 2</option>
</select>
<select name="name">
<?php $options = ["a", "b", "c", "d", "e"];
foreach($options as $o){
if($o == $_GET['name']) $attr="selected='true'";
else $attr = "";
echo "<option value='{$o}' {$attr}>{$o}</option>";
}
?>
</select>
We can create an array of option before hand and only use simple if/else to check if selected or not. If you want different values for data and different value for displaying option you can use associative arrays too.
<select name="name">
<?php $options = ["a"=>"Option A", "b"=>"Option B", "c"=>"Option C", "d"=>"Option D", "e"=>"Option E"];
foreach($options as $index=>$o){
if($index == $_GET['name']) $attr="selected='true'";
else $attr = "";
echo "<option value='{$index}' {$attr}>{$o}</option>";
}
?>
</select>
<form method="get" action="">
<select name="name" value="<?php echo $_GET['name'];?>">
<option value="a">a</option>
<option value="b">b</option>
</select>
<select name="location" value="<?php echo $_GET['location'];?>">
<option value="x">x</option>
<option value="y">y</option>
</select>
<input type="submit" value="Submit" class="submit" />
</form>
how do i get the select tag value, i am using the syntax below but not working.
Controller
'role' => $this->input->post('rol'));
View
these are the options in my select tag
<select name="rol">
<option value="Employee">Employee</option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
Just try it to post like below:
<form action="your_controller_action" method="POST">
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
<input type="submit" name="submit" value="Post it">
</form>
Then in your controller:
public function youControllerAction() {
if( $this->input->post) {
echo $this->input->post("rol");
}
}
You will get the selected option value.
how did you write your selection dropdown list!did you take it as i posted below?
<select name="rol" id="pos_select" class="form_input">
<option value="Employee">Employee </option>
<option value="HR">HR</option>
<option value="Student">Student</option>
</select>
$role = $this->input->post("rol");
put this code in view. Only you have to call controller function in action. In this example I am getting option values from database
<form method="post" action="<?php echo base_url()."main/project_filter_skills"; ?>">
<select class="demo-default " id="skilltags" name="skilltags[]" data-placeholder="Choose skills">
<option value="">Choose skills</option>
<?php foreach ($all_skill_tags as $skilltags) { ?>
<option value="<?php echo $skilltags->secondCat_id;?>">
<?php echo $skilltags->secondCat_title;?>
</option>
<?php } ?>
</select>
</form>
now put this in modal or controller to get the vlaue
$skilltags= $this->input->post('skilltags');
I have a simple dropdow box, I want to get the value of the selected item with php. If anyone can help me with it please.
<form id="s" method="post">
<select name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
</select>
<input type="submit" name="Submit" value="Send">
</form>
<?php
---
echo "selected size".$selected;
?>
Provided you put everything in one file:
<form id="s" method="post">
<select name="size">
<option value="small">Small</option>
<option value="medium">Medium</option>
<option value="large">Large</option>
</select>
<input type="submit" name="Submit" value="Send">
</form>
<?php
if(isset($_POST['size'])) {
echo "selected size: ".htmlspecialchars($_POST['size']);
}
?>
<select name="rider_select">
<option value="">Select Rider</option>
<?php
foreach ($riderlist as $rows) {
$rname = $rows['name'];
$rnum = $rows['number'];
$rider_full = $rname.'-'.$rnum;
?>
<option value="<?php echo $rname.'-'.$rnum; ?>"
<?= (($rider_fromdb == $rider_full) ? 'selected="selected"' : ''); ?>>
<?php echo $rname.'-'.$rnum; ?></option>
<?php } ?>
</select>
Consider:
<form method="get" action="">
<select name="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<select name="location">
<option value="x">x</option>
<option value="y">y</option>
</select>
<input type="submit" value="Submit" class="submit" />
</form>
On submitting the form, how do I make sure that the selected values remain selected in the dropdowns? This form is inside WordPress (PHP).
To avoid many if-else structures, let JavaScript do the trick automatically:
<select name="name" id="name">
<option value="a">a</option>
<option value="b">b</option>
</select>
<script type="text/javascript">
document.getElementById('name').value = "<?php echo $_GET['name'];?>";
</script>
<select name="location" id="location">
<option value="x">x</option>
<option value="y">y</option>
</select>
<script type="text/javascript">
document.getElementById('location').value = "<?php echo $_GET['location'];?>";
</script>
<select name="name">
<option <?php if ($_GET['name'] == 'a') { ?>selected="true" <?php }; ?>value="a">a</option>
<option <?php if ($_GET['name'] == 'b') { ?>selected="true" <?php }; ?>value="b">b</option>
</select>
After trying all these "solutions", nothing work. I did some research on W3Schools before and remember there was explanation of keeping values about radio.
But it also works for the Select option. See below for an example. Just try it out and play with it.
<?php
$example = $_POST["example"];
?>
<form method="post">
<select name="example">
<option <?php if (isset($example) && $example=="a") echo "selected";?>>a</option>
<option <?php if (isset($example) && $example=="b") echo "selected";?>>b</option>
<option <?php if (isset($example) && $example=="c") echo "selected";?>>c</option>
</select>
<input type="submit" name="submit" value="submit" />
</form>
If you are using WordPress (as is the case with the OP), you can use the selected function.
<form method="get" action="">
<select name="name">
<option value="a" <?php selected( isset($_POST['name']) ? $_POST['name'] : '', 'a' ); ?>>a</option>
<option value="b" <?php selected( isset($_POST['name']) ? $_POST['name'] : '', 'b' ); ?>>b</option>
</select>
<select name="location">
<option value="x" <?php selected( isset($_POST['location']) ? $_POST['location'] : '', 'x' ); ?>>x</option>
<option value="y" <?php selected( isset($_POST['location']) ? $_POST['location'] : '', 'y' ); ?>>y</option>
</select>
<input type="submit" value="Submit" class="submit" />
</form>
Since WordPress already uses jQuery you can try something like this:
var POST=<?php echo json_encode($_POST); ?>;
for(k in POST){
$("#"+k).val(POST[k]);
}
JavaScript only solution:
var tmpParams = decodeURIComponent(window.location.search.substr(1)).split("&");
for (var i = 0; i < tmpParams.length; i++) {
var tmparr = tmpParams[i].split("=");
var tmp = document.getElementsByName(tmparr[0])[0];
if (!!tmp){
document.getElementsByName(tmparr[0])[0].value = tmparr[1];
}
}
Or if you are using jQuery you can replace
var tmp = document.getElementsByName(tmparr[0])[0];
if (!!tmp){
document.getElementsByName(tmparr[0])[0].value = tmparr[1];
}
with:
$('*[name="'+tmparr[0]+'"]').val(tmparr[1]);
Try this solution for keep selected value in dropdown:
<form action="<?php echo get_page_link(); ?>" method="post">
<select name="<?php echo $field_key['key']; ?>" onchange="javascript:
submit()">
<option value="">All Category</option>
<?php
foreach( $field['choices'] as $key => $value ){
if($post_key==$key){ ?>
<option value="<?php echo $key; ?>" selected><?php echo $value; ?></option>
<?php
}else{?>
<option value="<?php echo $key; ?>"><?php echo $value; ?></option>
<?php }
}?>
</select>
</form>
This works for me!
<label for="reason">Reason:</label>
<select name="reason" size="1" id="name" >
<option value="NG" selected="SELECTED"><?php if (!(strcmp("NG", $_POST["reason"]))) {echo "selected=\"selected\"";} ?>Selection a reason below</option>
<option value="General"<?php if (!(strcmp("General", $_POST["reason"]))) {echo "selected=\"selected\"";} ?>>General Question</option>
<option value="Account"<?php if (!(strcmp("Account", $_POST["reason"]))) {echo "selected=\"selected\"";} ?>>Account Question</option>
<option value="Other"<?php if (!(strcmp("Other", $_POST["reason"]))) {echo "selected=\"selected\"";} ?>>Other</option>
</select>
Just change this line:
<input type="submit" value="Submit" class="submit" />
with this line:
<input type="submit" value="Submit/Reload" class="submit" onclick="history.go(-1);">
I don't work in WordPress much, but for forms outside of WordPress, this works well.
PHP
location = ""; // Declare variable
if($_POST) {
if(!$_POST["location"]) {
$error .= "Location is required.<br />"; // If not selected, add string to error message
}else{
$location = $_POST["location"]; // If selected, assign to variable
}
HTML
<select name="location">
<option value="0">Choose...</option>
<option value="1" <?php if (isset($location) && $location == "1") echo "selected" ?>>location 1</option>
<option value="2" <?php if (isset($location) && $location == "2") echo "selected" ?>>location 2</option>
</select>
<select name="name">
<?php $options = ["a", "b", "c", "d", "e"];
foreach($options as $o){
if($o == $_GET['name']) $attr="selected='true'";
else $attr = "";
echo "<option value='{$o}' {$attr}>{$o}</option>";
}
?>
</select>
We can create an array of option before hand and only use simple if/else to check if selected or not. If you want different values for data and different value for displaying option you can use associative arrays too.
<select name="name">
<?php $options = ["a"=>"Option A", "b"=>"Option B", "c"=>"Option C", "d"=>"Option D", "e"=>"Option E"];
foreach($options as $index=>$o){
if($index == $_GET['name']) $attr="selected='true'";
else $attr = "";
echo "<option value='{$index}' {$attr}>{$o}</option>";
}
?>
</select>
<form method="get" action="">
<select name="name" value="<?php echo $_GET['name'];?>">
<option value="a">a</option>
<option value="b">b</option>
</select>
<select name="location" value="<?php echo $_GET['location'];?>">
<option value="x">x</option>
<option value="y">y</option>
</select>
<input type="submit" value="Submit" class="submit" />
</form>