Radio button and PHP - 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 .

Related

How can I submit radio buttons answer in database?

This is the code I have written:
index.html
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="db.php" method="post">
YES:<input type="radio" name="YES" value="YES">
NO:<input type="radio" name="NO" value="NO" >
<input type="submit" name="submit value="submit">
</form>
</body>
<html>
db.php
<?php
$con = mysqli_connect('localhost','my user id','my password');
if(!con)
{ echo 'not connected to server';}else { echo 'something else is wrong' ;}
if(!mysqli_select_db($con,'my user id')
{ echo 'Database error selection';}
$YES = $_POST['YES'];
$NO = $_POST['NO'];
$sql = INSERT INTO users (YES,NO) VALUES ('$YES','$NO');
if(!mysqli_query($con,$sql))
{ echo 'Answer not submitted please try again!!!!!!!!';}
else{
echo 'Your answer successfully submitted \n Thanks for participating';}
header(" refresh:2; url=index.html");
?>
There is something wrong in it.
Can anyone please tell what is wrong?
Note: If 2 users submit YES then in database YES would get int value as 2
If 1 users submit NO then in database NO would get int value as 1.
You are using radio button the wrong way. Radio button should have the same 'name'. In your case you may use name="yesno".
YES:<input type="radio" name="yesorno" value="yes">
NO:<input type="radio" name="yesorno" value="no">
For your PHP it should be like this.
<?php
if (isset($_POST['submit'])) {
$yesorno=$_POST['yesno'];
$sql = INSERT INTO users (columnName) VALUES ('$yesorno');
mysqli_query($con,$sql); // Execute query
}
?>
And please express your goal as clear as possible. For now i pinpointed what is wrong on your coding semantics.
Above looks good. Another way is to add a label:
<label for="yesorno">Question:</label>
<input type="radio" name="yesorno" value="yes">YES:
<input type="radio" name="yesorno" value="no">NO:<br />

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

show radio button values in form

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.

Adding numbers using HTML forms and PHP

I'm trying to make a simple web application that adds scores from a physical game such as Scrabble. Most of the code is a HTML form, asking for one input per form element. It then puts the data generated from the form and initializes the appropriate variables. The one part I can't figure out is how to add the new score to the last score. I tried to add variables, like $lastScore, but that didn't seem to work either. Does anyone have any suggestions?
<?php
//Gets data from HTML form
$addScore1 = $_REQUEST['addScore1'];
$addScore2 = $_REQUEST['addScore2'];
//Generates HTML form
echo "<!DOCTYPE html>
<html>
<head>
<title>Score Add</title>
</head>
<body>
<div id=\"displayNames\">
<p>
$player1
<form method=\"post\" action=\"\">
<label for=\"addScore1\">Enter your score:</label>
<input type=\"text\" name=\"addScore1\" id=\"addScore1\" />
<input type=\"submit\" />
</form>
</p>
<p>
$player2
<form method=\"post\" action=\"\">
<label for=\"addScore2\">Enter your score:</label>
<input type=\"text\" name=\"addScore2\" id=\"addScore2\" />
<input type=\"submit\"/>
</form>
</p>
</div>
</body>
</html>";
?>
On submit, the script is calling itself and loses all the variables. You need to store their current score, e.g. in the session, in a database, or in a hidden field in the form.
With hidden field in your form:
<form method="post" action="">
<label for="addScore1">Enter your score:</label>
<input type="text" name="addScore1" id="addScore1" />
<--! the addition is done in the next line in value-->
<input type="hidden" name="oldScore1" id="oldScore1" value="<?=($oldscore1 + $addScore1)?>" />
<input type="submit" />
</form>
Do the same with your second form --> oldScore2 etc...
At the top of your script, read the oldscore from $_REQUEST
//Gets data from HTML form
$addScore1 = $_REQUEST['addScore1'];
$addScore2 = $_REQUEST['addScore2'];
$oldScore1 = $_REQUEST['oldScore1'];
$oldScore2 = $_REQUEST['oldScore2'];
// as alternative, do the addition here:
$oldScore1 += $addScore1;
$oldScore2 += $addScore2;
Rewriting your code as a whole, try it:
I edited all those <?php=$somevalue?>because they don't seem to work with your PHP-setup, and replaced them with <?php echo $somevalue> ?>... let me know how it works...
<?php
// Get data from HTML form. $_POST is fine, because form method is set to POST.
$addScore1 = $_POST['addScore1'];
$addScore2 = $_POST['addScore2'];
$oldScore1 = $_POST['oldScore1'];
$oldScore2 = $_POST['oldScore2'];
// if these are numeric values, add them up
if (is_numeric($addScore1) && is_numeric($oldScore1)) $oldScore1 += $addScore1;
if (is_numeric($addScore2) && is_numeric($oldScore2)) $oldScore2 += $addScore2;
// Generate HTML form -- in HTML, much to complicated in PHP, unless it is necessary for sth else
?>
<html>
<head>
<title>Score Add</title>
</head>
<body>
<div id="displayNames">
<p><?php echo $player1; ?> current score: <?php echo $oldScore1; ?>
<form method="post" action="">
<label for="addScore1">Enter your score:</label>
<input type="text" name="addScore1" id="addScore1" />
<input type="hidden" name="oldScore1" id="oldScore1" value="<?php echo $oldscore1; ?>" />
<input type="submit" />
</p>
<p><?php echo $player2; ?> current score: <?php echo $oldScore2; ?>
<label for="addScore2">Enter your score:</label>
<input type="text" name="addScore2" id="addScore2" />
<input type="hidden" name="oldScore2" id="oldScore2" value="<?php echo $oldscore2; ?>" />
<input type=\"submit\"/>
</form>
</p>
</div>
</body>
</html>

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