Someone please come to my rescue with checkbox code
Moderator: General Moderators
-
pradaprince
- Forum Newbie
- Posts: 5
- Joined: Sat Jan 08, 2011 3:13 pm
Someone please come to my rescue with checkbox code
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
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
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:
It all depends on your personal style.
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"
});Re: Someone please come to my rescue with checkbox code
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.
Then use header() to redirect to the appropriate URL
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");
}
Re: Someone please come to my rescue with checkbox code
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.
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
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.
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
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
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
Sorry, I forgot to mention that it comes up with the error code" header has already been executed on line 246"
thanks
thanks
Re: Someone please come to my rescue with checkbox code
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.
Remember to name your checkbox's. (square and circle in the example code)
<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");
}
?>
-
pradaprince
- Forum Newbie
- Posts: 5
- Joined: Sat Jan 08, 2011 3:13 pm
Re: Someone please come to my rescue with checkbox code
Again,
This is still giving me the "Header already Sent" error message
This is still giving me the "Header already Sent" error message
Re: Someone please come to my rescue with checkbox code
Oh. Wow. I'm stupid.
You have to put it at the very top of the page, before anything else.
So this:
...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.
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");
}
?>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
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?
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
It's hard to tell. Whats code is actually inside header.php and where are you placing that?