Jelles Jelles - 13 days ago 5
PHP Question

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>

Answer

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!

Comments