单选按钮和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!

        <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 .

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.

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!

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>

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.