Click to See Complete Forum and Search --> : Help with Radio Buttons used for directing to Web page


webdeveloper01
12-30-2004, 12:13 AM
Halo,

I have been searching for this answer in these
forums and unable to find it so far so I thought
I would make a post. I'm hoping some kind soul
here could help get me on the right track or guide
me to an easy to understand resource?

What I would like to do is use 3 radio buttons and
3 Web pages and depending on which radio button is
selected it would go to that specific Web page.

So for example:

1) I would have some question that I asked my visitor
that I'd type. Then I'd have three radio buttons (below)
that the visitor could select for their choice. And the
following three choices they would have would be:

==> Yes
==> No
==> Maybe

A. If Radio Button #1 (e.g., "Yes") was selected and
the submit button pushed the current page would go to:
"page01.php"

B. If Radio Button #2 (e.g., "No") was selected and
the submit button pushed the current page would go to:
"page02.php"

C. If Radio Button #3 (e.g., "Maybe") was selected and
the submit button pushed the current page would go to:
"page03.php"

Each page (page01.php, page02.php, and page03.php) would
have totally different content that reflected the answer
from the previous page.

My question: How would I go about doing this using PHP?
Any tips or resources you'd like to share?

Thanks,

Mark
PS: I'm new to PHP and still learning so "laymen's terms"
would be greatly appreciated. =)

webdeveloper01
12-30-2004, 12:59 AM
Guess what? 10 minutes after posting this I figured it
out. So please ignore this post above. I apologize for
the post.

(I used a javascript and then used hidden fields and
it appears to do what I'm wanting.)

Just wanted to get this posted before anyone
went to the trouble to help me with something
I just found a solution for.

Thanks,

Mark

NogDog
12-30-2004, 09:45 AM
Well, a JavaScript solution is fine for the 90% or so of your potential visitors who have JavaScript available/enabled. What about the other 10%?

My suggestion:

Your first page would have the following form:

<form action="choice.php" method="post">
<p><input type="radio" name="choice" value="yes">Yes</p>
<p><input type="radio" name="choice" value="no">No</p>
<p><input type="radio" name="choice" value="maybe">Maybe</p>
<p><input type="submit" name="submit" value="Enter Choice"></p>
</form>

The choice.php page (or whatever you want to call it) would be:

<?php
function error($message)
{
echo <<<EOD
<html>
<head><title>Error</title></head>
<body>
<h1>Error</h1>
<p>Error processing request: $message</p>
</body></html>
EOD;

exit;
}

if(isset($_POST['choice']))
{
switch($_POST['choice'])
{
case "yes":
$page = "page01.php";
break;
case "no":
$page = "page02.php";
break;
case "maybe":
$page = "page03.php";
break;
default:
error("Invalid choice received.");
}
# send user to desired page:
header(sprintf("Location: http://www.yoursite.com/%s", $page));
}
else
{
error("No choice received from form.");
}
?>

webdeveloper01
12-30-2004, 11:43 AM
Wow! Thank you! I'll *definitely* check into this as it looks
like it will probably be much better than what I was using! =)

Mark

sydelct
12-30-2004, 12:57 PM
Or you could make the script a lot shorter to this:

First page should be:

<form action="choice.php" method="post">
<p><input type="radio" name="choice" value="page01.php">Yes</p>
<p><input type="radio" name="choice" value="page02.php">No</p>
<p><input type="radio" name="choice" value="page03.php">Maybe</p>
<p><input type="submit" name="submit" value="Enter Choice"></p>


choice.php should be:

<?php
if ($_POST['choice'] != "") {
header ("Location: " . $_POST['choice']);
} else {
echo "Error - you did not make a choice.";
}
?>


or if you want choice.php to send back user to first page when no choice is made then make choice.php like this (just change "senderpage.php" to the actual name of your first page:


<?php
if ($_POST['choice'] != "") {
header ("Location: " . $_POST['choice']);
} else {
header ("Location: senderpage.php");
}
?>

NogDog
12-30-2004, 01:04 PM
Originally posted by sydelct
Or you could make the script a lot shorter to this:

First page should be:

<form action="choice.php" method="post">
<p><input type="radio" name="choice" value="page01.php">Yes</p>
<p><input type="radio" name="choice" value="page02.php">No</p>
<p><input type="radio" name="choice" value="page03.php">Maybe</p>
<p><input type="submit" name="submit" value="Enter Choice"></p>


choice.php should be:

<?php
if ($_POST['choice'] != "") {
header ("Location: " . $_POST['choice']);
} else {
echo "Error - you did not make a choice.";
}
?>
...
I thought about something like that, then for some reason convinced myself that wasn't a good idea - but I'll be darned if I can figure out now why I thought that. :rolleyes: (Maybe it's just that I like using switches. :) )

sydelct
12-30-2004, 01:29 PM
Sometimes our minds give us thoughts that we really don't understand. Bad idea? LOL. Whatever the reason is, I sure want to know so don't forget to let me know once you find out. :D

As for me, I just like making things shorter and simpler. :D

Mike

NogDog
12-30-2004, 01:40 PM
Originally posted by sydelct
Sometimes our minds give us thoughts that we really don't understand. Bad idea? LOL. Whatever the reason is, I sure want to know so don't forget to let me know once you find out. :D

As for me, I just like making things shorter and simpler. :D

Mike
Simpler is almost always good. Shorter is often a help, as long as you don't go to extremes of terseness (like Perl programmers like to do) such that you can't easily figure out what it does when you come back to it a year later. (Of course, that's not really a problem because we all thoroughly comment all of our PHP code, right? :rolleyes: )

webdeveloper01
12-30-2004, 03:25 PM
WOW! Thanks guys! I really appreciate your help! =)

sydelct
01-01-2005, 08:35 AM
No problem, that's what we're here for. See if we can help and also ask for help when we need them.

As for commenting my PHP code - yes, I do it most of the time. :D
There are even cases when I just use descriptive variables such as $theresultofquery1 instead of just $r - but that's just sometimes - when I feel like it.

Ciao and Happy New Year to all!

webdeveloper01
06-12-2005, 04:53 PM
Greetings,

I just (finally) got to trying this and it seems I'm running into some problems? I was just wondering if I'm doing something wrong here or if anyone can offer some suggestions?

I've tried the example above (I included the examples below) and regardless of what option is selected, it keeps going to the choice.php page rather than page01.php, page02.php, or page03.php and on the choice.php page I keep getting an error like this:

Warning: Cannot modify header information - headers already sent by (output started at /home/un/public_html/max01/choice.php:7) in /home/un/public_html/max01/choice.php on line 59

---

In my HTML editor, line 59 is this:

<input type="radio" name="choice" value="page01.php">


--------------
(Below are the examples from above posts)

<form action="choice.php" method="post">
<p><input type="radio" name="choice" value="page01.php">Yes</p>
<p><input type="radio" name="choice" value="page02.php">No</p>
<p><input type="radio" name="choice" value="page03.php">Maybe</p>
<p><input type="submit" name="submit" value="Enter Choice"></p>

---

<?php
if ($_POST['choice'] != "") {
header ("Location: " . $_POST['choice']);
} else {
echo "Error - you did not make a choice.";
}
?>


---

Thanks,

Mark

NogDog
06-12-2005, 08:38 PM
When you use the header() function, you cannot have any output of any sort precede it. This not only includes echo and print statements, but also any HTML outside of the <?php ?> tags or even just blank lines or spaces before the opening <?php tag.

webdeveloper01
06-14-2005, 03:17 PM
OK. I got it and it's working great now!

Thanks again, NogDog. I appreciate your help
and your quick tips. =)