Log in

View Full Version : Checkboxes



Medyman
07-16-2007, 01:00 AM
Hey all..

I have several checkboxes on a page. When a particular checkbox is clicked and the "next" button is clicked, I'd like the browser to point to a particular URL.

How do I:

1) Make it so only 1 check box is clicked at a time and/or check if more than 1 is selected and then promt user to only pick one.

2) Go to a particular webpage based on the checkbox selected.

Would I do this with JS, PHP, what?

Twey
07-16-2007, 01:10 AM
PHP, preferably. It can be done with JS, but it would be a bad idea, since non-JS users wouldn't be able to use it. If you only want one item selected, you actually want a radio button.
<?php
error_reporting(E_ALL); // because headers always cause trouble.
if(isset($_POST['url']))
header('Location: ' . $_POST['url']);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>Redirect to Page</title>
</head>
<body>
<form action="<?php print $_SERVER['PHP_SELF']; ?>" method="post">
<div>
<label>
<input type="radio" name="url" value="http://www.google.com/">
Google
</label>
<label>
<input type="radio" name="url" value="http://www.yahoo.com/">
Yahoo!
</label>
<label>
<input type="radio" name="url" value="http://www.clusty.com/">
Clusty
</label>
<label>
<input type="radio" name="url" value="http://www.twey.co.uk/">
Twey
</label>
</div>
</form>
</body>
</html>

thetestingsite
07-16-2007, 01:13 AM
This line would give an error:



header('Location: ' . $_POST['url']);


So instead use this:



header('Location: ' . $_POST["url"]);


Note: This is only for certain PHP configurations or something to that effect (read my post below).

Twey
07-16-2007, 01:15 AM
What? No it wouldn't. And the double quotes would be a whole fraction of a millisecond slower :p

thetestingsite
07-16-2007, 01:18 AM
That's wierd, I just tested it on two seperate installations. It worked fine with one (with the code you posted) but not with the other as it through up a parse error. I guess it just depends on the server config; so for that, just ignore my last post unless you have that problem as well.

Twey
07-16-2007, 01:23 AM
Hmm? I can't see where the parse error would come from... what was the exact message?

thetestingsite
07-16-2007, 01:35 AM
Ok, again, forget my post yet again. I figured out that I added something on the one that wasn't working that shouldn't have been there (I actually forgot a quote). Sorry for the confusion, and I will pay more attention next time (it's been a hell of a day with my daughter's birthday party and all).

Twey
07-16-2007, 01:52 AM
Oh, congratulations to her. How old?

thetestingsite
07-16-2007, 01:55 AM
She turns 5 on the 17th. Getting so big and sassy (like her mother) :p.

Medyman
07-16-2007, 08:35 PM
Thanks for the help guys.

But it's not working for me.
Not matter which radio button I select and then click submit, it takes me to "/<"

What could be causing this?

I didn't change anything except adding a submit button.

Medyman
07-19-2007, 02:39 AM
any ideas?