-2

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>
Jelles
  • 115
  • 1
  • 2
  • 7
  • 3
    give your radios the same name but different values. if you want multiples, use checkboxes and used as an array. Here is an example answer using checkboxes http://stackoverflow.com/a/18424178/1415724 should you want to go that route. – Funk Forty Niner Nov 21 '16 at 12:58
  • Your (stealth) edit: *"My second question is how do i link the button into php."* - Oh, so the plot thickens I see. Now people have posted answers based on your original post http://stackoverflow.com/revisions/40720784/1 and stand to get downvoted for it. – Funk Forty Niner Nov 21 '16 at 13:02
  • ...and I was right and not my downvotes down there. – Funk Forty Niner Nov 21 '16 at 13:04

7 Answers7

5

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!

Koen Hollander
  • 1,687
  • 6
  • 27
  • 42
0

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.

Masivuye Cokile
  • 4,754
  • 3
  • 19
  • 34
-1

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"];
RJParikh
  • 4,096
  • 1
  • 19
  • 36
Harold Scholtz
  • 99
  • 1
  • 10
-1

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.

ChristianF
  • 2,068
  • 9
  • 14
  • Notice: Undefined index: type in C:\xampp\htdocs\pizza2.php on line 11 Notice: Undefined index: type in C:\xampp\htdocs\pizza2.php on line 15 – Jelles Nov 21 '16 at 13:17
  • @user7147407 Just updated the code, as I noticed I missed some changes. Sorry about that. – ChristianF Nov 21 '16 at 13:18
  • To those who have downvoted my answer: Please explain why. – ChristianF Nov 21 '16 at 13:19
  • I didn't DV, but exactly how does `htmlspecialchars()` prevent XSS here? – Jay Blanchard Nov 21 '16 at 13:27
  • @JayBlanchard The PHP manual explains how: http://php.net/manual/en/function.htmlspecialchars.php Remember, the values in the POST array are generated by the client, and as such can contain _anything_. – ChristianF Nov 21 '16 at 13:29
-1

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!

WasteD
  • 758
  • 4
  • 24
  • This, too, is open to XSS. – ChristianF Nov 21 '16 at 13:16
  • Yeah i know right but I mean he didnt asked for crossside-scripting protection but yeah you are right :D But how do you use XSS there? just change the values of the inputs? – WasteD Nov 21 '16 at 13:19
  • @ChristianF I hope you're not downvoting answers because of this XSS stuff. I'm not blaming you here, but you have made a few comments in regards to the XSS stuff. – Funk Forty Niner Nov 21 '16 at 13:21
  • Yes, since the data comes from the client it can be changed to anything. Including a third party site that redirects the user with bad data, or a malicious user trying to attack the pizza-place itself. – ChristianF Nov 21 '16 at 13:22
  • @Fred-ii-: No, I haven't. In case you didn't notice, all but one answer was downvoted long before I commented upon the XSS problems. The answers I downvote are those who don't answer the question. Reason I commented it is because I feel it is irresponsible to teach new programmers insecure code, as we have no reasonable way of expecting them to know better. – ChristianF Nov 21 '16 at 13:25
  • @ChristianF Fair enough. Personally, I thought the question was a bad one and that stealth edit shouldn't have happened. I can honestly say that I did not downvote any answer, this includes yours also. – Funk Forty Niner Nov 21 '16 at 13:27
-1

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>

L. Herrera
  • 490
  • 4
  • 13
  • Not my downvote here, but that will throw an undefined index notice right away, plus the OP asked how to add an hyperlink. – Funk Forty Niner Nov 21 '16 at 13:10
  • This is vulnerable for XSS attacks! – ChristianF Nov 21 '16 at 13:14
  • @ChristianF I changed to filter_input ^_^ – L. Herrera Nov 21 '16 at 13:17
  • @ChristianF the question didn't ask how to prevent XSS injection and is a bad question IMHO. Plus, many have posted answers based on their original post earlier http://stackoverflow.com/revisions/40720784/1 and more than likely got downvoted for it; something I can honestly say were NOT mine. The question is too broad; they tried nothing. – Funk Forty Niner Nov 21 '16 at 13:19
-3
        <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 .

Amit Chauhan
  • 682
  • 7
  • 22
  • While this code snippet may solve the problem, it doesn't explain why or how it answers the question. Please [include an explanation for your code](//meta.stackexchange.com/q/114762/269535), as that really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion. **Flaggers / reviewers:** [For code-only answers such as this one, downvote, don't delete!](//meta.stackoverflow.com/a/260413/2747593) – Scott Weldon Nov 21 '16 at 21:23
  • okay i will remember that next time , sorry to post like that , – Amit Chauhan Nov 22 '16 at 04:36
  • My comment was not just to ask you to do better next time, but you can even [improve](//stackoverflow.com/help/editing) this post! To do so, just click the [edit] button in the lower left corner of your post. Improving your post will likely cause other users to remove their downvotes and/or upvote your post. – Scott Weldon Nov 22 '16 at 18:28