Someone please come to my rescue with checkbox code

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pradaprince
Forum Newbie
Posts: 5
Joined: Sat Jan 08, 2011 3:13 pm

Someone please come to my rescue with checkbox code

Post 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
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: Someone please come to my rescue with checkbox code

Post 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.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Someone please come to my rescue with checkbox code

Post 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
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: Someone please come to my rescue with checkbox code

Post 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.
Neilos
Forum Contributor
Posts: 179
Joined: Fri Nov 19, 2010 2:07 am

Re: Someone please come to my rescue with checkbox code

Post 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.
pradaprince
Forum Newbie
Posts: 5
Joined: Sat Jan 08, 2011 3:13 pm

Re: Someone please come to my rescue with checkbox code

Post 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
pradaprince
Forum Newbie
Posts: 5
Joined: Sat Jan 08, 2011 3:13 pm

Re: Someone please come to my rescue with checkbox code

Post by pradaprince »

Sorry, I forgot to mention that it comes up with the error code" header has already been executed on line 246"

thanks
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: Someone please come to my rescue with checkbox code

Post 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)
pradaprince
Forum Newbie
Posts: 5
Joined: Sat Jan 08, 2011 3:13 pm

Re: Someone please come to my rescue with checkbox code

Post by pradaprince »

Again,
This is still giving me the "Header already Sent" error message
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: Someone please come to my rescue with checkbox code

Post 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.
pradaprince
Forum Newbie
Posts: 5
Joined: Sat Jan 08, 2011 3:13 pm

Re: Someone please come to my rescue with checkbox code

Post 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?
User avatar
spedula
Forum Commoner
Posts: 81
Joined: Mon Mar 29, 2010 5:24 pm

Re: Someone please come to my rescue with checkbox code

Post by spedula »

It's hard to tell. Whats code is actually inside header.php and where are you placing that?
Post Reply