PDA

View Full Version : Go to different url with different radio button



Freedom35
12-21-2006, 04:50 PM
I intend to add two radio/option buttons to Davidundalicia's contact_us form template (see below in blue). What I need is that after clicking "Send Now", those users who selected Radio Button A will be directed to URL A and those selected Radio Button B will be directed to URL B. What should I add to the php file to make this possible?

<?PHP
// Receiving variables
@$name = addslashes($_POST['name']);
@$email = addslashes($_POST['email']);
@$comments = addslashes($_POST['comments']);
// error checking
if (strlen($name) == 0 )
{
header("Location: name_error.html");
exit;
}
if (strlen($email) == 0 )
{
header("Location: email_error.html");
exit;
}
if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
header("Location: email_error.html");
exit;
}
if (substr_count($comments, '@') > "2")
{
header("Location: comments_error.html");
exit;
}
//Sending Email to form owner
$mailto = "your email address here";
$mailsubj = "Feedback from my website form";
$mailhead = "From: $email\n";
reset ($HTTP_POST_VARS);
$mailbody = "Values from my website form:\n";
while (list ($key, $val) = each ($HTTP_POST_VARS))
{
if ($key!="submit")
{
$mailbody .= "$key : $val\n";
}
}
mail($mailto, $mailsubj, $mailbody, $mailhead);
// Autoresponder
$mailto = $email;
$mailsubj = "Copy of the info you Submitted";
$mailhead = "From: website form\n";
mail($mailto, $mailsubj, $mailbody, $mailhead);
header("Location: confirmationpage.html");
?>

navaldesign
12-21-2006, 07:28 PM
Let's suppose that you will name your Radio Button "choice", and the two values are "A" and "B". You code should become:

<?PHP
// Receiving variables
@$name = addslashes($_POST['name']);
@$email = addslashes($_POST['email']);
@$comments = addslashes($_POST['comments']);
@$choice = addslashes($_POST['choice']);

if ($choice == "A") {
$redirecturl =" www.yourdomain.com/page_for_choice_A.html "
}
elseif ($choice == "B") {
$redirecturl = " www.yourdomain.com/page_for_choice_B.html "
}
else {
$redirecturl = " www.yourdomain.com/default_redirect_page.html "
}



// error checking
if (strlen($name) == 0 )
{
header("Location: name_error.html");
exit;
}
if (strlen($email) == 0 )
{
header("Location: email_error.html");
exit;
}
if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
header("Location: email_error.html");
exit;
}
if (substr_count($comments, '@') > "2")
{
header("Location: comments_error.html");
exit;
}
//Sending Email to form owner
$mailto = "your email address here";
$mailsubj = "Feedback from my website form";
$mailhead = "From: $email\n";
reset ($HTTP_POST_VARS);
$mailbody = "Values from my website form:\n";
while (list ($key, $val) = each ($HTTP_POST_VARS))
{
if ($key!="submit")
{
$mailbody .= "$key : $val\n";
}
}
mail($mailto, $mailsubj, $mailbody, $mailhead);
// Autoresponder
$mailto = $email;
$mailsubj = "Copy of the info you Submitted";
$mailhead = "From: website form\n";
mail($mailto, $mailsubj, $mailbody, $mailhead);
header("Location: $redirecturl ");
?>

Change yourdomain.com/page_for_choice_A.html , yourdomain.com/page_for_choice_B.html , and yourdomain.com/default_redirect_page.html with the actual pages where your visitors must be redirected after the submission. The default page can be the same or different from the two others.

Freedom35
12-22-2006, 07:50 AM
1. When using radio buttons, either choice A or B will be select, then what is the purpose for the code below?

else {
$redirecturl = " www.yourdomain.com/default_redirect_page.html (http://www.yourdomain.com/default_redirect_page.html) "
}

2. when processing the php file, does it process the code from top to bottom? If yes, should the error checking process come before the redirect instruction, so that all errors are checked before the users are directed to respective url?

navaldesign
12-22-2006, 08:23 AM
1. Your radio buttons might be selected by default or not. So, with two radio buttons, you have three available options: A, B, or none. I just gave you a code that covers all three cases. If you dont need the third option, simply don't use it.

Make the code like this:
@$choice = addslashes($_POST['choice']);

if ($choice == "A") {
$redirecturl =" www.yourdomain.com/page_for_choice_A.html (http://www.yourdomain.com/page_for_choice_A.html) " ;
}
if ($choice == "B") {
$redirecturl = " www.yourdomain.com/page_for_choice_B.html (http://www.yourdomain.com/page_for_choice_B.html) " ;
}


2. Yes. the redirect command is in the very bottom line. The above is only the code defining the redirect url, according to the radio button choice.

Freedom35
12-22-2006, 02:46 PM
thank you, Navaldesign. Will try out your suggestion.

Freedom35
12-30-2006, 03:50 PM
After adding in the "redirecturl", the following error occurred. How should I amend it?

"Parse error: syntax error, unexpected '}' in /home/dhuhmhsc/public_html/cancelprocessing.php on line 32"

(Error highlighted in red below)

<?PHP
// Receiving variables
@$first_name = addslashes($_POST['first_name']);
@$last_name = addslashes($_POST['last_name']);
@$email = addslashes($_POST['email']);
@$cancel = addslashes($_POST['cancel']);
@$feedback = addslashes($_POST['feedback']);
// error checking
if (strlen($first_name) == 0 )
{
header("Location: name_error.html");
exit;
}
if (strlen($last_name) == 0 )
{
header("Location: name_error.html");
exit;
}
if (strlen($email) == 0 )
{
header("Location: email_error.html");
exit;
}
if (! ereg('[A-Za-z0-9_-]+\@[A-Za-z0-9_-]+\.[A-Za-z0-9_-]+', $email))
{
header("Location: email_error.html");
exit;
}
//Trial or cancel
if ($cancel == "trial") {
$redirecturl =" www.horsedummies.com/thankyou.html (http://www.horsedummies.com/thankyou.html) " ;
}
if ($cancel == "cancel") {
$redirecturl = " https://www.paypal.com/cgi-bin/webscr?cmd=_subscr-find&alias=paypal%40horsedummies%2ecom " ;
}
//Sending Email to form owner
$mailto = "cancel_feedack@horsedummies.com";
$mailsubj = "Cancel Feedback from my website form";
$mailhead = "From: $email\n";
reset ($HTTP_POST_VARS);
$mailbody = "Values from my website form:\n";
while (list ($key, $val) = each ($HTTP_POST_VARS))
{
if ($key!="submit")
{
$mailbody .= "$key : $val\n";
}
}
mail($mailto, $mailsubj, $mailbody, $mailhead);
header("Location: $redirecturl ");
?>

navaldesign
12-31-2006, 06:20 AM
There is nothing wrong with the code, apart the fact that you have missed the last line, which should redirect the visitor after submission:

header("Location: $redirecturl ");

Make sure that what you have posted above is what you have actually uploaded (in php a dot or a comma may result in errors). Or, if you wish, send me (http://www.navaldesign.info/feedback13.html) your loggin details and i'll have a look.

fadikaouk
09-15-2007, 07:38 PM
Hi there,

I tried to create a simple form with a radio button and two choices in addition to a submit button. I simply want my client to identify himself as either A or B and based on the information A or B, he should be directed to a page with a form for A or a Form for B.

I solved the problem with a hyper link, but I wanted to learn how I may do so with a radio button instead.

Thanks