show radio button values in form - php

In this code in php, don´t show the values of radio buttons, echo don´t work
<html>
<head>
<title>Probando radio</title>
</head>
<body>
<form method="POST">
<input type="radio" name="sexo" value="m" checked>Mujer
<input type="radio" name="sexo" value="h">Hombre
</form>
<? php
$sexo = $_POST['sexo'];
// print ($sexo);
echo $sexo;
?>
</body>
</html>

specify the form action and add a submit button. upon submit, $_POST['sexo'] should contain your result (m or h)

This is not a recommended fix, but it will work:
Change:
<form method="POST">
To:
<form action="#" method="post">
NOTE: Technically method should be lower case. It's an XHTML requirement. You would be penalized by W3C mobileOK validation.

Related

Radio button and PHP

I have a question about radio buttons in html and linking into php. And i dont know how to link it without the name part because i need the name to be all 1 otherwise you can select every radiobutton. My second question is how do i link the button into php.
This is my HTML code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Pizza</h1>
<form method="POST" action="pizza.php">
Wat op pizza:<br>
<input type="radio" value="" name="pepperoni">Pepperoni<br>
<input type="radio" value="" name="ananas">Ananas<br>
<input type="radio" value="" name="ansjovis">Ansjovis<br>
<input type="radio" value="" name="broccoli">Broccoli<br><br>
<input type="submit" value="" name="Bestellen" value="Bestellen"><br>
</form>
</body>
</html>
So how do I say when pepperoni is selected echo "You ordered pepperoni pizza."
This is my PHP Code:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$pep = $_POST["pepperoni"];
$ana = $_POST["ananas"];
$ans = $_POST["ansjovis"];
$bro = $_POST["broccoli"];
?>
</body>
</html>
You can give the radio buttons all the same name with different values. So you can select 1.
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Pizza</h1>
<form method="POST" action="pizza.php">
Wat op pizza:<br>
<input type="radio" value="Pepperoni" name="type">Pepperoni<br>
<input type="radio" value="Ananas" name="type">Ananas<br>
<input type="radio" value="Ansjovis" name="type">Ansjovis<br>
<input type="radio" value="Broccoli" name="type">Broccoli<br><br>
<input type="submit" name="Bestellen" value="Bestellen"><br>
</form>
</body>
</html>
And in PHP you can read
<?php
echo $_POST['type']; //Pepperoni, ananas(pineapple?) etc.
?>
Good luck!
Radio buttons let a user select ONLY ONE of a limited number of choices, There for your radio button will need to have one name attribute for that particular groups of items. Then will have different values.
Your form should look like this :
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Pizza</h1>
<form method="POST" action="pizza.php">
Wat op pizza:<br>
<input type="radio" value="pepperoni" name="pizzaType">Pepperoni<br>
<input type="radio" value="ananas" name="pizzaType">Ananas<br>
<input type="radio" value="ansjovis" name="pizzaType">Ansjovis<br>
<input type="radio" value="broccoli" name="pizzaType">Broccoli<br><br>
<input type="submit" value="Bestellen" name="Bestellen" value="Bestellen"><br>
</form>
</body>
</html>
as you can see above all the radios have the same name attribute pizzaType
Then on pizza.php you need to execute the script when the submit button is clicked, then check if an option was selected, if option was selected then return the choice user selected, if they did not return a message that the user must atleast select an item.
your pizza.php should look like:
<?php
if(isset($_POST['Bestellen'])){ //button clicked
if(isset($_POST['pizzaType'])){//check if choice is checked
echo "You ordered ".$_POST['pizzaType']. " pizza";
}else{
echo "Please select an option";
die();
}
}
?>
NB: For your own good you will also need to learn how to validate and sanitize user inputs. This is very important, you must always treat form submissions as if they from a very dangerous hacker, which it's very important to filter and sanitize the user input before using it.
If you choose a pizza you can just pick 1. So you have to give every radio the Name pizzaName for example and for value you can use the Name (pepperoni). If you press submit then you can just read the values with the PHP File like this:
$chosenPizza = $_POST['pizzaName'];
echo $chosenPizza;
And the output will be pepperoni!
Instead of having different names, make all your names the same and enter what you have as names now, as values. Then get the value by using $val = $_POST["yourvalue"];
eg:
<input type="radio" value="ananas" name="samenameforall">
$val = $_POST["samenameforall"];
You've misused the radio tag attributes a bit here. The type of pizza should be the value, not the name of the radio-button. So if you change the name to type, you can use the following line in PHP to (safely) echo out the user's choice:
// If we have something submitted, process the form,
if (isset ($_POST['order'])) {
// Pepperonies are special!
if ($_POST['type'] == 'pepperoni') {
$output = "Congrats! You ordered pepperoni!";
} else {
// Use of htmlspecialchars () to prevent XSS attacks.
$output = "You ordered ".htmlspecialchars ($_POST['type'], ENT_QUOTES, 'UTF-8')." pizza.";
}
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<h1>Pizza</h1>
<form method="POST" action="pizza.php">
<fieldset>
<legend>Wat op pizza:</legend>
<input type="radio" id="pepp" value="pepperoni" name="type"><label for="pepp">Pepperoni</label>
<input type="radio" id="anna" value="ananas" name="type"><label for="anna">Ananas</label>
<input type="radio" id="ansj" value="ansjovis" name="type"><label for="ansj">Ansjovis</label>
<input type="radio" id="brocc" value="broccoli" name="type"><label for="broc">Broccoli</label>
</fieldset>
<fieldset>
<input type="submit" name="order" value="Bestellen">
</fieldset>
</form>
<?php
// Only show output if there is something to show.
if (!empty ($output)) {
echo "<p>$output</p>";
}
?>
</body>
</html>
Note the use of htmlspecialchars () here, as it's used to prevent XSS attacks.
When working with radio buttons, you should group them with the same name of the radio element. Take a look at this:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<?php
$toppings = filter_input(INPUT_POST, "toppings");
?>
<h1>Pizza</h1>
<form method="POST" action="pizza.php">
Wat op pizza:<br>
<input type="radio" value="pepperoni" name="toppings">Pepperoni<br>
<input type="radio" value="ananas" name="toppings">Ananas<br>
<input type="radio" value="ansjovis" name="toppings">Ansjovis<br>
<input type="radio" value="broccoli" name="toppings">Broccoli<br><br>
<input type="Submit" value="Bestellen">
<?php
echo "<br>";
if($toppings != "") {
echo "You ordered $toppings pizza. ";
}
?>
</form>
</body>
<input type="radio" value="pepperoni" name="type">Pepperoni<br>
<input type="radio" value="ananas" name="type">Ananas<br>
<input type="radio" value="ansjovis" name="type">Ansjovis<br>
<input type="radio" value="broccoli" name="type">Broccoli<br><br>
do like this .

Can't get input from radio buttons in PHP

My HTML code is as follows:
<form action="installs.php" method="get">
<input type="radio" name="interval" value="weekly">Weekly<br>
<input type="radio" name="interval" value="monthly" checked>Monthly<br>
</form>
And my PHP is simply
$temp = $_GET["interval"];
I already have another form of buttons and a select that I am successfully retrieving input from, what am I doing wrong here?
To answer some questions, installs.php is the name of the single file that I am writing this in. There are no other elements called 'interval'.
EDIT:
When I attempted to remove all other code in order to post it here, the call stack disappeared. It seems as though the issue may be elsewhere? I will update when I find the problem.
EDIT 2:
I was mistaken about the earlier problem, I have successfully pared down the code with the problem still persisting. Now the entirety of installs.php is as such:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="installs.php" method="get">
<input type="radio" name="interval" value="weekly">Weekly<br>
<input type="radio" name="interval" value="monthly" checked>Monthly<br>
</form>
</body>
</html>
<?php
$temp = $_GET["interval"];
?>
Check if variable exists :
<form action="installs.php" method="get">
<input type="radio" name="interval" value="weekly">Weekly<br>
<input type="radio" name="interval" value="monthly" checked>Monthly<br>
</form>
<?php
if ( isset( $_GET["interval"] ) ) // <====================
$temp = $_GET["interval"];
?>
index.php
<html>
<form action="installs.php" method="get">
<input type="radio" name="interval" value="weekly">Weekly<br>
<input type="radio" name="interval" value="monthly" checked>Monthly<br>
<input type="submit" name="SEND"> <!-- ADD THIS LINE -->
</form>
</html>
installs.php:
<?php
$temp = $_GET["interval"];
echo $temp;
?>
This work fine in my webserver
try $_REQUEST['interval']; instead

Can not get radio values in php

Okay this is kind of a noob question but I have been searching and searching and can not find out how to do this. I can not get the values of my radio buttons useing php.
<!DOCTYPE html>
<html>
<body>
<form action="" method="POST">
Do you buy your lunch?
<br>
<input type="radio" name="doyoubuy" value="Yes" checked>Yes
<br>
<input type="radio" name="doyoubuy" value="No">No
<br><br>
<input type="submit" value="submit">
</form>
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['doyoubuy'])) {
echo "You have selected :".$_POST['doyoubuy'];
}
}
?>
</body>
</html>
Thank you!!!
You are checking if (isset($_POST['submit'])) {
But your submit button is not actually named...
So just name it:
<input type="submit" value="submit" name="submit">
Then your code for the radios should work fine.
The action should be the current page. In your case is ""
I suggest this HTML and PHP for your consideration:
<html>
<body>
<form method="POST">
Do you buy your lunch?
<br>
<input type="radio" name="doyoubuy" value="Yes" checked>Yes
<br>
<input type="radio" name="doyoubuy" value="No">No
<br><br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if (isset($_POST) && $_POST != NULL) {
if ( $_POST['doyoubuy'] == 'Yes' || $_POST['doyoubuy'] == 'No') {
$answer = $_POST['doyoubuy'];
echo "You selected :". $answer;
}
}
?>
</body>
</html>
What needs to be tested is whether the form was submitted. If so, provided that each field bears a name in the form, then a value of some kind will be associated with every field, which obviates the need to test if the submit button is set. Also, if a user submitted a form but neglected to fill in, for example, a text input, then that field's value would still be set; it would be set to an empty string.
It is inadvisable to trust a posted form value and consider it safe to use. In this case, I test to be sure that the answer is what I'm expecting, just in case someone might have spoofed the form and changed the answer to something other than what I intended.
By default, if one leaves out the ACTION attribute of the form, its value is the url of the page containing the form.

2 forms with one PHP file

I have 2 FORMS on a single page, One below the other.
I would like to have such that second form should be always in disable mode.
and Once the first form submit button is pressed and validated second should get activated to enter the data in it.
Is there anything in PHP which can help me on this
You have 2 ways:
1) send validation of first form using ajax, and, if you receive 'true', enable second form.
2) make a POST from first form, if everything is good, set "validated" to 'true' and reload the same page. In the second form "enabling" must be only if you have $validated = true;
The logic below should help you out as a starting point:
<form method="post">
<input type="text" name="name" />
<input type="submit" name="form1" value="Proceed" />
</form>
<form method="post">
<input type="text" name="email"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
<input type="submit" name="form2" value="Submit"<?php if(!isset($_POST['form1'])) { echo ' disabled="disabled"'; } ?> />
</form>
Of course, it would be much more reliable to use either AJAX to validate the first form, or to have the forms appear on separate pages.
<?php
if(isset($_POST['next'])) {
if($_POST['name']!="") {
$disabled = "";
$val = $_POST['name'];
} else {
$disabled = " disabled='disabled'";
$val="";
}
} else {
$disabled = " disabled='disabled'";
$val="";
}
?>
<html>
<head>
<title></title>
</head>
<body>
<form id="frm1" name="frm1" method="POST" action="">
<label>Name</label><input type="text" id="name" name="name" value="<?php echo $val;?>"/>
<input type="submit" name="next" id="next_frm" value="Next"/>
</form>
<form name="frm2" id="frm2" method="POST" action="">
<label>Address</label><input type="text" name="address" id="address" value="" <?php echo $disabled;?>/>
<input type="submit" name="save" id="save" value="Save" <?php echo $disabled;?>/>
</form>
</body>
</html>
This is somewhat you were looking for ,I hope
You can do it by setting a class on all inputs within second form and set them as disabled of course someone who knows a bit of javascript will be able to change it.
So you can do it as your visual layer, but then check in PHP as well if second form can be passed in case someone wanted to sneak something in.
More complicated approach would be to show images that look like form fields and only change them to inputs where the first form is submitted. That can be done on client or server side
So in reality you will have 3 forms, but one would be "fake"
Thats simple just use if else condition.
// this if condition checks whether the form 1 is submitted or not. If form1 is submitted than form 2 is displayed else form1 wil only be displayed
if(isset($_POST['submit']))
{
//Display your form 2.
}
else
{
//Display your form1.
}

Auto Submitted form does not carry form variables

The following form does not carry form variables when it is submitted using quto javascript after 5 seconds.
Normal submission works fine.
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Test</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<SCRIPT LANGUAGE="JavaScript"><!--
setTimeout('document.test.submit()',5000);
//--></SCRIPT>
</head>
<body>
<form name="test" id="form1" method="post" action="auto2.php">
<p>
<INPUT TYPE=checkbox NAME="test" VALUE="option1"> Option1
<INPUT TYPE=checkbox NAME="test" VALUE="option2"> Option2
<INPUT TYPE=checkbox NAME="test" VALUE="option3"> Option3
</p>
<p><input type="submit" name="submit_test" value="Submit_test" />
</p>
</form>
</body>
</html>
---------------------------------------------------------------
output page:
--------------------------------------------------------------
<?php
if(isset($_POST['submit_test'])){
$variable=$_POST['test'];
echo $variable;
}
?>
Any help is highly appreciated.. thank you.
You are testing whether the submit_test button value is set:
if(isset($_POST['submit_test'])){
Submitting the form automatically won't set the value, so your test fails.
You should test for some other form field, like a hidden element.
Unless a checkbox is checked, you wont get any response (blank or otherwise)
What you should have instead is
<input type=hidden name="test" value="">
<INPUT TYPE=checkbox NAME="test" VALUE="option1"> Option1
<INPUT TYPE=checkbox NAME="test" VALUE="option2"> Option2
<INPUT TYPE=checkbox NAME="test" VALUE="option3"> Option3
That way you get a default value when you post your form
+1 to Pekka's answer.
If you do the following it will check to see that the page was posted to without requiring that a variable was set in the post.
if ($_SERVER['REQUEST_METHOD'] === 'POST') { ... }
You should probably also put in some form of validation to make sure it came from where you expected it to.

Categories