Page 1 of 1
Someone please come to my rescue with checkbox code
Posted: Sat Jan 08, 2011 3:17 pm
by pradaprince
Hello everyone,
I hope I am posting this in the right section but if not please feel free to move it.
I have been banging my head for days over this small piece of code.
I am trying to use the checkbox to redirect to different pages.
Example: If checkbox for circle is checked, direct to http://www.whatever.com
and if checkbox for square is checked, direct to http://www.nowhere.com
I have some knowledge of php but not this advanced.
Can someone help me as I know others are probably looking for a situation to the same problem.
Thanks
Jason
Re: Someone please come to my rescue with checkbox code
Posted: Sat Jan 08, 2011 6:16 pm
by spedula
Suggest moving this to Javascript, since this wouldn't be PHP.
The logic for the code could be something like this, but this is just one of many ways to do this.
Create a recursive function that checks which check box is checked off. Execute window.location = "URL" depending on which check box is checked.
Or you can use jQuery to do it:
Code: Select all
$("#checkboxID1").change( function() {
window.location = "URL"
});
$("#checkboxID2").change( function() {
window.location = "URL2"
});
It all depends on your personal style.
Re: Someone please come to my rescue with checkbox code
Posted: Sat Jan 08, 2011 6:46 pm
by Neilos
you could do it in php where you have the action of a form send the user to a handler, perhaps formhandler.php in which you read the post variable from the form concerning the selection of circle or square.
Code: Select all
$selection = $_POST['selection'];
// 1 for circle, 2 for square
if ($selection == 1) {
// redirect to circle
header("Location: http://www.whatever.com");
} else {
// redirect to square
header("Location: http://www.nowhere.com");
}
Then use header() to redirect to the appropriate URL
Re: Someone please come to my rescue with checkbox code
Posted: Sun Jan 09, 2011 1:56 am
by spedula
Neilos, I completely agree with you on that. My question is why waste the page reload and server processing power on that? Granted the script is minuscule and will run in under 0.0002 seconds but it's still a page refresh that can easily be avoided.
I always do my best to to avoid code that reduces performance even the slightest or unnecessary page loads. It's just good practice, especially in complex applications where every bit counts (no pun intended).
Just a thought.
Re: Someone please come to my rescue with checkbox code
Posted: Sun Jan 09, 2011 2:28 pm
by Neilos
lol unintended but funny.
I agree with you too. Personally I would do this client side as it seems unnecessary to load the server with it, but seeing as this is a php forum I thought I'd give a working solution for php.
If server load is not a worry then either way would be ok, it all depends on the application. But I would use javascript personally.
Re: Someone please come to my rescue with checkbox code
Posted: Mon Jan 10, 2011 4:31 pm
by pradaprince
Hi neilos,
I tried what you suggested but it didn't work for me.
I am a complete noob when it comes to php though.
Is there anything else I have to write and where would I put it?
I put the code you wrote above the form and tested it. Didn't work.
I tried putting it between the checkboxes, it didn't work.
Thanks
Re: Someone please come to my rescue with checkbox code
Posted: Mon Jan 10, 2011 4:32 pm
by pradaprince
Sorry, I forgot to mention that it comes up with the error code" header has already been executed on line 246"
thanks
Re: Someone please come to my rescue with checkbox code
Posted: Mon Jan 10, 2011 5:03 pm
by spedula
You have to set the method form to either post or get. And also you need a submit button, so that the data can actually be sent to the server.
<form method="post">
// CHECKBOX'S
<input type="submit" />
</form>
Syntax for the code that needs to be placed in that same page.
Code: Select all
<?php
if ($_POST['circle']) {
header("Location: http://www.whatever.com");
}
elseif ($_POST['square']) {
header("Location: http://www.nowhere.com");
}
?>
Remember to name your checkbox's. (square and circle in the example code)
Re: Someone please come to my rescue with checkbox code
Posted: Mon Jan 10, 2011 5:24 pm
by pradaprince
Again,
This is still giving me the "Header already Sent" error message
Re: Someone please come to my rescue with checkbox code
Posted: Mon Jan 10, 2011 7:47 pm
by spedula
Oh. Wow. I'm stupid.
You have to put it at the very top of the page, before anything else.
So this:
Code: Select all
<?php
if ($_POST['circle']) {
header("Location: http://www.whatever.com");
}
elseif ($_POST['square']) {
header("Location: http://www.nowhere.com");
}
?>
...then the rest of your code.
If that doesn't work. Try creating a separate file with just that php code and put
action="pageNameHere.php"
into your form element in the original page.
Re: Someone please come to my rescue with checkbox code
Posted: Tue Jan 11, 2011 8:28 am
by pradaprince
Thank you for the advice.
I am going to try it but I think the problem is that I am using
<?php
include (header.php)
?>
Could that be the problem why I am getting the error message?
Re: Someone please come to my rescue with checkbox code
Posted: Wed Jan 12, 2011 8:38 pm
by spedula
It's hard to tell. Whats code is actually inside header.php and where are you placing that?