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 = '';" />
For a page redirect when the button is clicked.
But I'm not sure how to bine 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.
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.';" />
For a page redirect when the button is clicked.
But I'm not sure how to bine 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.
Share Improve this question edited Jun 12, 2016 at 5:44 jvnna asked Jun 12, 2016 at 5:31 jvnnajvnna 611 gold badge3 silver badges10 bronze badges2 Answers
Reset to default 3Try 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.';
else if(document.getElementById("female").checked == true)
window.location.href = 'http://www.yahoo.';
}
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.')">
If you want to redirect with PHP, the method of doing so is as follows.
header("Location: http://url.");
so:
if (isset($_POST['male'])) {
header("Location: http://male-website.");
die(); // Stop from doing anything else
} else {
header("Location: http://something-else.");
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.