I have the following html drop down form:
<form method='post' action='signup.php'>$error
<span class='fieldname'>Sex</span>
<select name ="sex">
<option value="female">Female</option>
<option value="male">Male</option>
</select> <br />
_END;
But when I process the form in php I get a blank (no value) for $sex:
$sex = $_POST['sex'];
echo $sex;
Give us more script.Perhaps some error.I used the below code and it worked fine.
<?php
if(isset($_POST['sex']))
{
$sex = $_POST['sex'];
echo 'Result: '.$sex;
}
?>
<form method='post' action=''>
<span class='fieldname'>Sex</span>
<select name ="sex">
<option value="female">Female</option>
<option value="male">Male</option>
</select><input type="submit"> <br />
</form>
option value so you must use selected="selected" any one. and try following code.
<form method='post' action=''>
<span class='fieldname'>Sex</span>
<select name ="sex">
<option value="female" selected="selected">Female</option>
<option value="male">Male</option>
</select><input type="submit"> <br />
</form>
<?php
$sex = isset($_POST['sex']) ? mysql_real_escape_string($_POST['sex']) : '';
echo 'Gender: '. $sex;
?>
Related
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>
I am trying to make a dropdown list and take three parameters from the user which will be stored in a php file for later use. My current code is :
<body>
<header>
<form action="parameter.php" method="post">
<label class="heading">First</label>
<select name="First">
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<input type="submit">
</form>
<form action="parameter.php" method="post">
<label class="heading">Second</label>
<select name="Second">
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<input type="submit">
</form>
<form action="parameter.php" method="post">
<label class="heading">Third</label>
<select name="Third">
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
<input type="submit">
</select>
</form>
</header>
</body>
Here, the problem is, I don't want submit button for each parameter, instead there should be only one button for all the parameters. I have tried several things but none is working.
My php file
<?php
echo $_POST['First'];
echo $_POST['Second'];
echo $_POST['Third'];
?>
Where am I going wrong?
EDIT : Corrected the php code
Just wrap all your selects under one form instead of 3 different forms.
Make sure that the input submit is not inside the select tag.
Also, note that your php code is trying to echo parameters that don't exist in the html you shared. The name of your select will be the parameter name.
<form action="parameter.php" method="post">
<label class="heading">First</label>
<select name="First" >
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<label class="heading">Second</label>
<select name="Second" >
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<label class="heading">Third</label>
<select name="Third" >
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
</select>
<input type="submit">
</form>
PHP
<?php
echo $_POST['First'];
echo $_POST['Second'];
echo $_POST['Third'];
?>
you can add all there in one form
<form action="parameter.php" method="post">
<label class="heading">First</label>
<select name="First" >
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<label class="heading">Second</label>
<select name="Second" >
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<label class="heading">Third</label>
<select name="Third" >
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
<input type="submit">
</select>
<input type="submit" name="submit" value="submit">
</form>
in your code you have missed button "name".
parameter.php code should be this.
if(isset($_POST['submit'])){
echo $First = $_POST['First'];
echo $Second = $_POST['Second'];
echo $Third = $_POST['Third'];
}
<form action="parameter.php" method="post" id="frm">
<label class="heading">First</label>
<select name="First" >
<option value="First-1">First-1</option>
<option value="First-2">First-2</option>
</select>
<label class="heading">Second</label>
<select name="Second" >
<option value="Second-1">Second-1</option>
<option value="Second-2">Second-2</option>
</select>
<label class="heading">Third</label>
<select name="Third" >
<option value="Third-1">Third-1</option>
<option value="Third-2">Third-2</option>
</select>
<input type="submit">
</form>
JQuery AJAX
<script>
$('#frm').on('submit', function(e){
e.preventDefault();
$.ajax({
url : $(this).attr('action'),
type: "POST",
dataType: "json",
data : $(this).serialize(),
success: function(data)
{
console.log(data)
}
});
});
</script>
PHP
<?php
echo $_POST['First'];
echo $_POST['Second'];
echo $_POST['Third'];
?>
I made a form that receives source code that can be written in C/C++/Python/Java/Ruby
<?php
if(isset($_GET['id']))
{
$problem_code=$_GET['id'];
echo '<form name="submit-button" action="/codejudge/result-board.php?id='.$problem_code.'" method="post">';
}
?>
<textarea id="editor" class="editor" rows="25" cols="100" name="code">
</textarea>
<select id="lang" name="lang" class="lang">
<option value="11" selected>C</option>
<option value="41">C++</option>
<option value="4">Python 2</option>
<option value="116">Python 3</option>
<option value="55">Java</option>
<option value="17">Ruby</option>
</select>
<button type="submit" >Submit</button>
</form>
and here is the code for result-board.php
<?php
include "conn_db.php";
$code=stripcslashes($_POST['code']);
$lang= stripcslashes($_POST['lang']);
echo $code.$lang;
?>
What should I use in case of POST method so that correct data can be receive.
I think you can also try:
<?php
if(isset($_GET['id']))
{
$problem_code=$_GET['id'];
}else{
$problem_code=0;
}
echo '<form name="submit-button" action="/codejudge/result-board.php" method="post">';
echo '<input type="hidden" value="'.$problem_code.'" name="id"/>';
?>
<textarea id="editor" class="editor" rows="25" cols="100" name="code"></textarea>
<select id="lang" name="lang" class="lang">
<option value="11" selected>C</option>
<option value="41">C++</option>
<option value="4">Python 2</option>
<option value="116">Python 3</option>
<option value="55">Java</option>
<option value="17">Ruby</option>
</select>
<button type="submit" >Submit</button>
</form>
your form should be something like:
<form name="submit-button" action="/codejudge/result-board.php' method="post">
and then add your problem code like,
<input type="hidden" value="<?php echo $problem_code;?>" name="code"/>
and then access with your name like,
$_POST['code']
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>