View Full Version : Form POST redirect based on radio button selected
wkenny
05-01-2007, 11:43 AM
I have three options on a form. When the form is posted the user is redirected to a thank you page.
<input type="hidden" id="U386058799TNT63" name="redirect" value="http://www.estateagentsespana.com/regtku.htm">
What I would like to be able to do is change the redirect depending on which radio button was selected. E.g. if option 1 was selected redirect to regtku1.htm, if option 2 was selected redirect to regtku2.htm
jscheuer1
05-01-2007, 04:14 PM
Give this a shot:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function usePage(frm,nm){
for (var i_tem = 0, bobs=frm.elements; i_tem < bobs.length; i_tem++)
if(bobs[i_tem].name==nm&&bobs[i_tem].checked)
frm.action=bobs[i_tem].value;
}
</script>
</head>
<body>
<div>
<form action="#" method="post" onsubmit="usePage(this, 'bob');">
<input type="radio" name="bob" value="regtku1.htm">
<input type="radio" name="bob" value="regtku2.htm">
<input type="radio" name="bob" value="regtku3.htm">
<input type="submit" value="Go">
</form>
</div>
</body>
</html>
wkenny
05-01-2007, 05:10 PM
Thanks, John.
The code supplied works fine - only problem is I was using onsubmit to check form input (onsubmit = "return checktheform()").
Is it possible to combine the input verification with the call to the redirect function?
jscheuer1
05-01-2007, 05:15 PM
onsubmit = "usePage(this, 'bob');return checktheform();"
wkenny
05-01-2007, 06:42 PM
Works brilliantly. Thanks, John
wkenny
05-02-2007, 06:32 PM
Everything worked fine on my machine but when I uploaded to the server I get this message:
Method Not Allowed
The requested method POST is not allowed for the URL /regtku1.htm.
Apache/1.3.37 Server at site Port 80
The address bar showed the full URL. Is there something else I need to do?
jscheuer1
05-03-2007, 04:02 AM
I may have misunderstood your requirements. I assumed that the redirect should be the action of the form, but perhaps not. Perhaps it needs to follow the format you laid out in your first post:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<script type="text/javascript">
function usePage(frm,nm){
for (var i_tem = 0, bobs=frm.elements; i_tem < bobs.length; i_tem++)
if(bobs[i_tem].name==nm&&bobs[i_tem].checked)
frm.redirect.value='http://www.estateagentsespana.com/'+bobs[i_tem].value;
}
</script>
</head>
<body>
<div>
<form action="#" method="post" onsubmit="usePage(this, 'bob');return checktheform();">
<input type="hidden" id="U386058799TNT63" name="redirect" value="http://www.estateagentsespana.com/regtku.htm">
<input type="radio" name="bob" value="regtku1.htm">
<input type="radio" name="bob" value="regtku2.htm">
<input type="radio" name="bob" value="regtku3.htm">
<input type="submit" value="Go">
</form>
</div>
</body>
</html>
Notes: You may need to adapt this further to suit your actual requirements. I don't know what the action should be, there is nothing that you've told me that would indicate that with any certainty. Also, I am only using the post method because you mentioned posting the form. And, I don't have a clue as to what checktheform(); does specifically.
If you have an example of any form for this that actually works live (even one that only redirects to one of the pages), I may be able to be more helpful should you require it.
Vergerider
04-18-2012, 10:23 AM
Hi John,
Later that decade ...
I accept your original post is now very old and I apologize but I cannot find a solution to my problem
I have used your example above to good effect thank you. Its very much appreciated.
But for the life of me it will not work in ie7 or safari .. any clues ?
Always online.
Richard
jscheuer1
04-18-2012, 02:02 PM
It might have to be live to work in some browsers. Problems in those browsers might be due to other factors.
If making it live doesn't solve the problem. because it is an old thread and because your situation might be different than that of the original person's in this thread, please post a link to a page on your site that demonstrates the problem. Also include in your post as detailed an explanation as possible of your current objectives with this code and what it doesn't do in the problem browsers, and what I have to do to see it misbehave.
Vergerider
04-18-2012, 04:17 PM
Hi John,
I'm very thankful for your time... cheers.
The URL is Live and still misbehaving in IE7
http://c26.futurestudios.info/contact-us/
If you visit using say firefox and post the form with a radio button selected you are redirected to a thank you page bespoke to the radio button you selected .. i.e. type of enquiry.
I cant seem to get this to work in IE7.
It seems fine in all others ?
Again thanks in advance for any assistance.
jscheuer1
04-18-2012, 04:49 PM
Is there any reason why you changed:
<script type="text/javascript">
function usePage(frm,nm){
for (var i_tem = 0, bobs=frm.elements; i_tem < bobs.length; i_tem++)
if(bobs[i_tem].name==nm&&bobs[i_tem].checked)
frm.return.value="http://c26.futurestudios.info/"+bobs[i_tem].value;
}
</script>
and:
<input type="hidden" name="return" value="" />
to return form redirect? The reason I ask is that's where IE 7 is complaining:
SCRIPT1010: Expected identifier
contact-us, line 25 character 5
The word return has a special meaning in javascript and this may be why IE 7 is balking at it.
I see you do have another hidden input:
<input type="hidden" name="redirect_on_duplicate" value="" />
That might have caused confusion in some browsers, and/or in your mind so that could be why you changed it, or because I don't see where this value is used - presumably it's a possible POST value on the http://c26.futurestudios.info/ paqe, so needs to be return.
Either way we can get IE 7 to accept it by changing the syntax:
<script type="text/javascript">
function usePage(frm,nm){
for (var i_tem = 0, bobs=frm.elements; i_tem < bobs.length; i_tem++)
if(bobs[i_tem].name==nm&&bobs[i_tem].checked)
frm.elements['return'].value="http://c26.futurestudios.info/"+bobs[i_tem].value;
}
</script>
This will not change its meaning in IE 7 or other browsers.
The browser cache may need to be cleared and/or the page refreshed to see changes.
Vergerider
04-19-2012, 10:02 AM
Hi John,
In depth and extensive answer that fixed my problem.
Superb!
I thank you from the bottom of my boots !
Richard
jscheuer1
04-20-2012, 12:15 AM
You know, you don't need javascript for this. You could remove the script and the onsubmit event, get rid of this:
<input type="hidden" name="return" value="" />
and change this section of the form:
<div class="radios">
<div>
<label class="check">Wedding Enquiry</label>
<input name="bob" type="radio" class="check" id="wedding" value="contact-us/thanks-for-your-wedding-enquiry/" checked="checked" />
<br /></div> <div>
<label class="check">HR Enquiry:</label>
<input name="bob" type="radio" class="check" value="contact-us/thanks-for-your-hr-enquiry/" />
<br /></div> <div>
<label class="check">Supplier Enquiry:</label>
<input name="bob" type="radio" class="check" value="contact-us/thanks-for-your-supplier-enquiry/" />
<br /></div> <div>
<label class="check">Other Enquiry:</label>
<input name="bob" type="radio" class="check" value="contact-us/thanks-for-your-other-enquiry/" />
</div></div>
to:
<div class="radios">
<div>
<label class="check">Wedding Enquiry</label>
<input name="return" type="radio" class="check" id="wedding" value="http://c26.futurestudios.info/contact-us/thanks-for-your-wedding-enquiry/" checked="checked" />
<br /></div> <div>
<label class="check">HR Enquiry:</label>
<input name="return" type="radio" class="check" value="http://c26.futurestudios.info/contact-us/thanks-for-your-hr-enquiry/" />
<br /></div> <div>
<label class="check">Supplier Enquiry:</label>
<input name="return" type="radio" class="check" value="http://c26.futurestudios.info/contact-us/thanks-for-your-supplier-enquiry/" />
<br /></div> <div>
<label class="check">Other Enquiry:</label>
<input name="return" type="radio" class="check" value="http://c26.futurestudios.info/contact-us/thanks-for-your-other-enquiry/" />
</div></div>
aidan.aob
08-27-2012, 08:57 AM
Hi John,
Sorry to hijack this thread once again, i have a contact form with 2 radio buttons, once people press submit i would like to redirect them to a certain page depending on there selection, i have tried both codes in you previous posts.
when i tried used your first piece of code I was redirected to the pages I wanted but no details were sent through on the form.
And when i used your second post the details were submitted but there was no redirection.
Below is the code I am using if you can shed any light on this I would be very grateful.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Contact Us</title>
<script src="freecontactformvalidation.js"></script>
<script>
required.add('Full_Name','NOT_EMPTY','Full Name');
required.add('email','EMAIL','Email Address');
required.add('AntiSpam','NOT_EMPTY','Anti-Spam Question');
</script>
<link rel="stylesheet" type="text/css" href="freecontactform.css">
<?PHP
$selected_radio = $_POST['payment'];
print $selected_radio;
?>
</head>
<body>
<form name="freecontactform" method="post" action="freecontactformprocess.php" onsubmit = "return validate.check(this)">
<table width="400px" class="freecontactform">
<tr>
<td colspan="2">
<div class="freecontactformheader">Contact Us Form</div>
<div class="freecontactformmessage">Fields marked with <span class="required_star"> * </span> are mandatory.</div>
</td>
</tr>
<tr>
<td valign="top">
<label for="Full_Name" class="required">Full Name<span class="required_star"> * </span></label>
</td>
<td valign="top">
<input type="text" name="Full_Name" id="Full_Name" maxlength="80" style="width:230px">
</td>
</tr>
<tr>
<td valign="top">
<label for="Address" class="required">Address<span class="required_star"> * </span></label>
</td>
<td valign="top">
<input type="text" name="Address" id="Address" maxlength="100" style="width:230px">
</td>
</tr>
<tr>
<td valign="top">
<label for="postcode" class="not-required">Post Code</label>
</td>
<td valign="top">
<input type="text" name="postcode" id="postcode" maxlength="100" style="width:230px">
</td>
</tr>
<tr>
<td valign="top">
<label for="email" class="not-required">Email Address</label>
</td>
<td valign="top">
<input type="text" name="email" id="email" maxlength="100" style="width:230px">
</td>
<tr>
<td valign="top">
<label for="Telephone_Number" class="not-required">Telephone Number</label>
</td>
<td valign="top">
<input type="text" name="Telephone_Number" id="Telephone_Number" maxlength="100" style="width:230px">
</td>
<tr>
<td valign="top">
<label for="Payment" class="not-required">Payment</label>
</td>
<td valign="top"><p>
<input type="radio" name="payment" value="thankyou.htm"/>
Pay Online Via PayPal<br />
<input type="radio" name="payment" value="page4.htm"/> Cash/Cheque on Day<br />
</p></td>
</tr>
<tr>
<td colspan="2" style="text-align:center" >
<div class="antispammessage">
To help prevent automated spam, please answer this question
<br /><br />
<div class="antispamquestion">
<span class="required_star"> * </span>
Using only numbers, what is 10 plus 15?
<input type="text" name="AntiSpam" id="AntiSpam" maxlength="100" style="width:30px">
</div>
</div>
</td>
</tr>
<tr>
<td colspan="2" style="text-align:center" >
<br /><br />
<input type="submit" value=" Submit Form " style="width:200px;height:40px">
<br /><br />
<br /><br />
</td>
</tr>
</table>
</form>
</body>
</html>
<?php
if(isset($_POST['email'])) {
include 'freecontactformsettings.php';
function died($error) {
echo "Sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
if(!isset($_POST['Full_Name']) ||
!isset($_POST['Address']) ||
!isset($_POST['postcode']) ||
!isset($_POST['email']) ||
!isset($_POST['Telephone_Number']) ||
!isset($_POST['payment']) ||
!isset($_POST['AntiSpam'])
) {
died('Sorry, there appears to be a problem with your form submission.');
}
$full_name = $_POST['Full_Name']; // required
$address = $_POST['Address']; // required
$postcode = $_POST['postcode']; // not required
$email_from = $_POST['email']; // not required
$telephone = $_POST['Telephone_Number']; // not required
$payment = $_POST['payment']; // required
$antispam = $_POST['AntiSpam']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(preg_match($email_exp,$email_from)==0) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
if(strlen($full_name) < 2) {
$error_message .= 'Your Name does not appear to be valid.<br />';
}
if($antispam <> $antispam_answer) {
$error_message .= 'The Anti-Spam answer you entered is not correct.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\r\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:");
return str_replace($bad,"",$string);
}
$email_message .= "".clean_string($full_name)."\r\n";
$email_message .= "".clean_string($address)."\r\n";
$email_message .= "".clean_string($postcode)."\r\n";
$email_message .= "".clean_string($email_from)."\r\n";
$email_message .= "".clean_string($telephone)."\r\n";
$email_message .= "".clean_string($payment)."\r\n";
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($email_to, $email_subject, $email_message, $headers);
?>
<?php
}
die();
?>
Many Thanks
Aidan
jscheuer1
08-27-2012, 09:49 AM
This is an old thread. If you want to redirect on the basis of data in a form, have PHP detect that and redirect for you.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.