-2
<h1> Hotel kamer reservering </h1>
<br><br>

<form method="POST">
<input name="radio" type="radio" value="éénpersoonskamer">éénpersoonskamer</input><br><br>
<input name="radio1" type="radio" value="tweepersoonskamer">tweepersoonskamer</input><br><br>
<input name="radio2" type="radio" value="ontbijt">ontbijt</input><br><br><br>
<input name="radio3" type="radio" value="lunch">lunch</input><br><br><br>
<input name="radio4" type="radio" value="diner">diner</input><br><br><br>
<input name="submit" type="submit" <value="Klik"></input>
</form>

<?php 

if(isset($_POST['submit']) and ! empty($_POST['submit'])) {
    if(isset($_POST['radio']) ||  ($_POST['radio1']) || ($_POST['radio2']) || ($_POST['radio3']) || ($_POST['radio4']) ) {
        $radio = $_POST['radio'] . '&nbsp' . $_POST['radio1'] . '&nbsp' . $_POST['radio2'] . '&nbsp' . $_POST['radio3'] . '&nbsp' . $_POST['radio4'];
        echo $radio;
    }
}
?>

I am a beginner with PHP, am learning it now but i can't seem to lose the undefined index message. If i choose all then it won't give a bad message. Can someone explain me how i can fix this with this code and how i can resolve it with another code in the future.

I am thankfull for your time.

Barmar
  • 741,623
  • 53
  • 500
  • 612

3 Answers3

1

You're only calling isset() on the $_POST variable for the first radio button. You need to check that all the radio buttons are set. If any of them aren't set, you'll get that warning.

if(isset($_POST['radio'], $_POST['radio1'], $_POST['radio2'], $_POST['radio3'], $_POST['radio4']) ) {
    $radio = $_POST['radio'] . '&nbsp' . $_POST['radio1'] . '&nbsp' . $_POST['radio2'] . '&nbsp' . $_POST['radio3'] . '&nbsp' . $_POST['radio4'];
    echo $radio;
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • What is the difference? –  May 04 '18 at 07:10
  • Did you select something from every radio group? It only echoes something when there's a value for *all* the `$_POST['radioX']` parameters. – Barmar May 04 '18 at 19:34
0

1) Remove typo "<" before "value" in <input name="submit" type="submit" <value="Klik"></input> 2) This is pointless: "empty($_POST['submit']" 3) All radio buttons should have the same name, not "radio1", "radio2" etc., perhaps it would be better to use checkboxes here if you want to be able to select/deselect more than one option 4) The only reason you're getting an error is because you're checking "radio" variable if it exists with isset($_POST['radio']) as for other radio inputs you're only checking if they have any kind of value: ($_POST['radio1']), you should be checking if all of them exist before checking their values :)

  • I don’t understand the last part, how can i fix that? –  May 04 '18 at 07:08
  • You need to change every `($_POST['radio1'])` to `isset($_POST['radio1'])` etc. – MrTaikoDrummer May 04 '18 at 15:00
  • What does if(!empty($_POST['submit'])) do? That if it is not empty it's going to start the function? –  May 05 '18 at 18:07
  • Indeed, `empty()` function checks if a variable is empty, it returns TRUE if that's the case. However you have an exclamation mark before the function, which means you are checking if it's NOT empty. – MrTaikoDrummer May 06 '18 at 19:21
-1

A better approach, as I guess you really are looking for a combination of radio- and checkboxes and that you want to output the checked options:

<h1> Hotel kamer reservering </h1>
<br><br>

<form method="POST">
<input name="roomtype" type="radio" checked="checked" value="éénpersoonskamer">éénpersoonskamer <br><br>
<input name="roomtype" type="radio" value="tweepersoonskamer">tweepersoonskamer <br><br>
<input name="food[]" type="checkbox" value="ontbijt">ontbijt <br><br><br>
<input name="food[]" type="checkbox" value="lunch">lunch <br><br><br>
<input name="food[]" type="checkbox" value="diner">diner <br><br><br>
<input name="submit" type="submit"> 
</form>

<?php 

if(!empty($_POST['submit'])) {
    echo "roomtype: " . $_POST['roomtype'] . "<br>";
    $food = $_POST['food'];
    foreach ($food AS $k => $v) {
        echo "Food $k = $v<br>";
    }
}
?>
Hampus Brynolf
  • 1,296
  • 11
  • 20
  • You seem have to have the best answer, i will check it later and let you know –  May 04 '18 at 07:09
  • Your answer worked and was good explained for a beginner like me. Thank you for your time. The radio buttons with the checkbox is a perfect solution. You thought about it and it works good. Thanks –  May 05 '18 at 17:52