Sidenote: You can't use echo and header together, otherwise you will be outputting before header.
Consult the link following this (footnotes) and is intended to be run inside the same file:
Checkbox method: (which differs from the radio buttons below)
<?php
if(isset($_POST['submit'])){
if(isset($_POST['choice'])){
foreach($_POST['choice'] as $choice){
if($choice == "A"){
echo "You chose A" . "\n";
// header('Location: /4.html');
// exit;
}
if($choice == "B"){
echo "You chose B" . "\n";
}
if($choice == "C"){
echo "You chose C" . "\n";
}
} // brace for foreach
} // brace for if(isset($_POST['choice']))
// else for if(isset($_POST['choice']))
else{
echo "Please make a choice.";
}
} // brace for if(isset($_POST['submit']))
?>
<form action="" method="post">
<input id="checkbox" type="checkbox" name="choice[]" value="A" />A)Choice A
<input id="checkbox" type="checkbox" name="choice[]" value="B" />B)Choice B
<input id="checkbox" type="checkbox" name="choice[]" value="C" />C)Choice C
<input id="input" onclick="return myFunction()" name="submit" type="submit" value="Submit">
</form>
Radio buttons method: (edited) and added a name attribute to the submit button. Sidenote: </input>
isn't a valid tag; it's been removed.
<?php
$choice = $_POST['choice'];
if(isset($_POST['submit'])){
if($choice == "A"){
// echo "You chose A" . "\n";
header("Location: /4.html");
exit; // stop further execution
}
if($choice == "B"){
echo "You chose B" . "\n";
}
if($choice == "C"){
echo "You chose C" . "\n";
}
if(empty($_POST['choice'])){
header("Location: http://www.google.com/");
exit; // stop further execution
}
} // submit conditional
?>
<form action="" method="post">
<input id="radio" type="radio" name="choice" value="A" />A)Choice A
<input id="radio" type="radio" name="choice" value="B" />B)Choice B
<input id="radio" type="radio" name="choice" value="C" />C)Choice C
<input id="input" onclick="return myFunction()" name="submit" type="submit" value="Submit">
</form>
Footnotes:
See the following on the subject of outputting before header:
Also, you can use radio buttons for single choices and without the array.
If you're faced with errors/notices/warnings, somewhere down the line:
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.