1

I have a login form and a section with 3 checkboxes and a final submit button. I want to write code such that when a checkbox is checked, and the submit button is clicked, the user will be redirected to another page depending on which checkbox was checked.

I have:

<input type="checkbox" name="gender" value="male">
<input type="checkbox" name="gender" value="female">

I know that you can use

if (isset($_POST['male'])) {
    // Checkbox male is selected
} else {
    // Do something else
}

To see if the checkbox is checked, and

 <input type="button" value="goElsewhere" class="GE" id="GELSEWHERE" 
  onClick="Javascript:window.location.href = 'http://www.google.com';" />

For a page redirect when the button is clicked.

But I'm not sure how to combine these. How would I go about it?

Edit: The page should redirect only after the final submit button is clicked. The checkboxes should only determine which page the user is directed to. Checking a checkbox should not redirect the page.

jvnna
  • 61
  • 1
  • 3
  • 10

2 Answers2

3

Try this

<input type="checkbox" name="gender" value="male" id="male" >
<input type="checkbox" name="gender" value="female" id="female" >

<input type="submit" onClick="redirect()">

Use client sided JS (more practical)

function redirect()
{
   if(document.getElementById("male").checked == true)
        window.location.href = 'http://www.google.com';
   else if(document.getElementById("female").checked == true)
        window.location.href = 'http://www.yahoo.com';        
}

It may be more practical to use radio button not check boxes since only one option can be selected. Then you use a switch statement.

<input type="radio" name="gender" value="Male">
<input type="radio" name="female" value="Female">

Getting the chosen value is similar to jbabey's answer to this question.

Another approach is to simply pass the URL in the function

 function redirect(url)
 {
    window.location.href = url;
 }

Then you would have

<input type="checkbox" name="gender" value="female" id="female" onClick="redirect('www.google.com')">
Community
  • 1
  • 1
Moussa Khalil
  • 635
  • 5
  • 12
  • sorry, I've just started learning PHP so I'm a bit confused. I have a single PHP file with an HTML section, CSS style in the header, and PHP commands below everything. If I use javascript, can I input the redirect() function as is into my PHP file between php ?> ? Or do I have to make a separate JS file and reference it in my PHP file somehow? – jvnna Jun 12 '16 at 05:40
  • I also want it so that the user is redirected on button click, not when a certain radio button is clicked. I'm not sure if your code does that – jvnna Jun 12 '16 at 05:41
  • echo ""; would do it if insisde . If inside regular HTML just put directly. having JS in a separate file is healthy programming but not for something this specific – Moussa Khalil Jun 12 '16 at 05:42
  • @jvnna modified code to call function on submit click – Moussa Khalil Jun 12 '16 at 05:44
  • @jvnna Any time. Please use radiobuttons not checkboxes and try to pick up JS then jQuery – Moussa Khalil Jun 12 '16 at 05:47
0

If you want to redirect with PHP, the method of doing so is as follows.

header("Location: http://url.com");

so:

if (isset($_POST['male'])) {
    header("Location: http://male-website.com");
    die(); // Stop from doing anything else
} else {
    header("Location: http://something-else.com");
    die(); // Stop from doing anything else
}

Die is added as a safety precaution for any crawlers etc. that ignore header requests (stops anything else from happening.)

But I'd do it with JS, as Moussa said.

Chris Evans
  • 1,235
  • 10
  • 14