0

New guy here - I have three choices and want to let the user only pass to the next page "4.html" if they select A, else send them to google.com. This is where I've gotten so far :(

if(empty($_POST['choice'])){
    echo "Please select at least one choice..!!";
    //this should send them to google.com if they select none or the wrong one
}
else{
     foreach($_POST['choice'] as $choice){
        header('Location: /4.html');
    }
}


<form action="multichoice.php" method="post">
    <input id="checkbox" type="checkbox" name="choice[]" value="A" />A&#x29;Choice A
    <input id="checkbox" type="checkbox" name="choice[]" value="B" />B&#x29;Choice B
    <input id="checkbox" type="checkbox" name="choice[]" value="c" />C&#x29;Choice C
<input id="input"onclick="return myFunction()"  type="submit" value="Submit"></input>
</form>

I really appreciate your guys' help!

aynber
  • 22,380
  • 8
  • 50
  • 63
  • You can't use an echo and a header together if you plan on wanting to echo "Please select at least one choice..!!" and redirect. You can only use a header. If you want to do both, then you'll need to use a meta refresh method with enough seconds for it that will leave the user enough time to read the message. Plus this `foreach($_POST['choice'] as $choice){ header('Location: /4.html');` I have no idea what you want to do here. You should also be posting the relevant HTML form that goes with this. – Funk Forty Niner Mar 27 '15 at 01:57
  • I don't need to echo the "please select at least one choice" if that's the case. I just need a redirect if they select the wrong one or none and if they select the right one, I need them to be sent to the url "4.html" – Josh Aguirre Mar 27 '15 at 02:12
  • *"My form is as follows:"* - Place that in your question and not in comments. then remove it from comments. That is unlegible. – Funk Forty Niner Mar 27 '15 at 02:14
  • Got it, sorry about that. – Josh Aguirre Mar 27 '15 at 02:18
  • This would be easier/simpler if you used radio buttons but not impossible. Are you set on using checkboxes, or can you use radio buttons? Plus, where is the `return myFunction()` function? That's a JS function and it's not in your question. – Funk Forty Niner Mar 27 '15 at 02:22
  • I prefer to use radio buttons but my developer bailed on me so I'm trying to piece this together myself. What's the best way to post the JS file? – Josh Aguirre Mar 27 '15 at 02:33
  • My JS isn't so hot to be honest. I work mostly serverside. I've something ready to post, but is pure PHP. – Funk Forty Niner Mar 27 '15 at 02:33
  • You're welcome. It's posted below. Sidenote: `value="c"` was changed to uppercase C `value="C"` below. – Funk Forty Niner Mar 27 '15 at 02:42

1 Answers1

1

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&#x29;Choice A
    <input id="checkbox" type="checkbox" name="choice[]" value="B" />B&#x29;Choice B
    <input id="checkbox" type="checkbox" name="choice[]" value="C" />C&#x29;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&#x29;Choice A
    <input id="radio" type="radio" name="choice" value="B" />B&#x29;Choice B
    <input id="radio" type="radio" name="choice" value="C" />C&#x29;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.

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Thanks Fred! I don't see where it says "if A is selected, go to 4.html"? – Josh Aguirre Mar 27 '15 at 02:45
  • @JoshAguirre You're welcome. Well, you see where it says `echo "You chose A" . "\n";`? You can just replace it with `header('Location: /4.html');` but you can't have echo together. – Funk Forty Niner Mar 27 '15 at 02:46
  • @JoshAguirre Reload it, I've made an edit to show you where to use it. You can do the same for the others too using the same way I made it. – Funk Forty Niner Mar 27 '15 at 02:48
  • I've got it working except for the redirect if "A". It just takes me to google as well. – Josh Aguirre Mar 27 '15 at 02:56
  • @JoshAguirre Which version of my code are you using that is causing that, the checkbox or radios? – Funk Forty Niner Mar 27 '15 at 02:57
  • @JoshAguirre Ok hold on. I know what's going on. Give me a minute, I'll fix 'er up. – Funk Forty Niner Mar 27 '15 at 02:58
  • @JoshAguirre Reload it and look under the same place I did the radio button version, under **Radio buttons method: (edited)....** it's slightly different and I've added `name="submit"` to the submit button with a conditional statement for it. – Funk Forty Niner Mar 27 '15 at 03:05
  • Okay now A takes me to 4.html, B takes me to an echo "You chose B" and C takes me to domain/multiplechoice.php (blank screen) – Josh Aguirre Mar 27 '15 at 03:11
  • @JoshAguirre You did have a lowercase `c` in your original post in `value="c"` so if that has anything to do with your JS, then that's the problem. Variables are case-sensitive. Did you copy my code exactly as shown? I've fully tested this using A, B, C and nothing chosen. – Funk Forty Niner Mar 27 '15 at 03:14
  • Thanks, that was the issue. I'm still confused as to why its Echoing the text instead of taking me to google.com for the wrong answers. Do I need to add something to each of the wrong answers? – Josh Aguirre Mar 27 '15 at 03:17
  • @JoshAguirre what choice(s) would you consider being a wrong answer? – Funk Forty Niner Mar 27 '15 at 03:19
  • I got it! Haha thanks to you - I added header (same as the selection A but with google) in replace of the echo statement (as I didn't really need it to echo. Thank you so much for your help today. – Josh Aguirre Mar 27 '15 at 03:22