Forum Moderators: coopster

Message Too Old, No Replies

redirecting a user to a website after they hit 'Submit' button

         

Bubzeebub

8:26 pm on Dec 29, 2006 (gmt 0)

10+ Year Member



I am working on a template. The template has a contact page where someone can enter in info and then click the 'Submit' button. I would like someone to be redirected to another website when they click the 'Submit' button. Is there a simple PHP script that would do that?

Thanks in advance,
Bubzee

Bubzeebub

8:48 pm on Dec 29, 2006 (gmt 0)

10+ Year Member



Let me also add that once they hit the 'Submit' button the mail should go to my email address. Then they user should be redirected to a particular website. Can anyone help?

BlueGhost

9:05 pm on Dec 29, 2006 (gmt 0)

10+ Year Member



im sure this is one of the easiest scripts in php. Let me look on my site

BlueGhost

9:10 pm on Dec 29, 2006 (gmt 0)

10+ Year Member


on your template, put this into the body. This is not actually a php script, its just using the <form> tag and the METHOD & ACTION paramaters to send it to the php page.

-------------------------------------------
<p>In the meantime, please send me an email </p>
<form method="post" action="../send.php">
Name:<br /><input name="name" type="text" size="30" /><br /><br />
Email:<br /><input name="email" type="text" size="30" /><br /><br />
Comments:<br /><input name="subject" type="text" size="30" /><br /><br />
Message:<br /><textarea name="msg" cols="24" rows="3"></textarea>
<br /><br />
<input type="reset" value="Reset" />
<input type="submit" value="Send" />
</form>

-------------------------------------------

Make a new page, called thankyou.php, or submit.php, or send.php or likewise. Have the following code in the body of your document.

-------------------------------------------
<?php
$to = "mail@mail.com"; // change to your email address
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$msg = $_POST['msg'];
$d = date('l dS \of F Y h:i:s A');
$sub = "form to mail";
$headers = "From: $name <$email>\n";
$headers .= "Content-Type: text/plain; charset=iso-8859-1\n";
$mes = "Subject: ".$subject."\n";
$mes .= "Message: ".$msg."\n";
$mes .= "Name: ".$name."\n";
$mes .= 'Email: '.$email."\n";
$mes .= 'Date & Time: '.$d;

if (empty($name) ¦¦ empty($email) ¦¦ empty($subject) ¦¦ empty($msg))
{
echo " <h3>Sorry all fields are required.</h3>";
}
elseif(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
print " <h3>Sorry the email address you entered looks like it's invalid.</h3>";
}
else
{
mail($to, $sub, $mes, $headers);
print " <h3>Thank you ".$name." for contacting us.<br>We will get back to you as soon as posiable<br></h3>";
}
?>

Bubzeebub

10:08 pm on Dec 29, 2006 (gmt 0)

10+ Year Member



Thanks for the response. I don't want to add anything to the template already. I'd just like to have the information emailed to me after they click the 'Submit' button and then have them automatically redirected to another website of my choice. Would that second code do all that?

BlueGhost

11:32 pm on Dec 29, 2006 (gmt 0)

10+ Year Member


yes, that second code is what you put on the page you want redirected. You can make one page and have every submit page linked to it, or you can make a new page for each site.

The long php code i presented is the php "results" or "redirected" page, you find that in the <form> tag. In the below case, the address is action="redirectedlink.php" sends the form to the php page, and emails you the result. You will get an email with all the forms filled in.

If you just want a comment box, try this:
____________

IN TEMPLATE:
____________

<form method="post" action="redirectedlink.php">
Comments:<br />
<input name="subject" type="text" size="30" /><br />
<input type="submit" value="Send" />
</form>

Vastio

4:37 pm on Dec 30, 2006 (gmt 0)

10+ Year Member



On the current action page, where it mails you the information, you can add a header redirect [us3.php.net].

After your mailing code, just add:


header("Location: [webmasterworld.com");...]
exit();

Then, after the script is done sending you the email, it redirects the visitor elsewhere.

Bubzeebub

4:56 am on Dec 31, 2006 (gmt 0)

10+ Year Member



Thanks guys. I'm going to fool around with this a bit. I have the form and comment box already as part of the template. All I need is the Submit box to work. I'll try and let you know how it goes. Thanks again.

Bubzeebub

5:22 am on Dec 31, 2006 (gmt 0)

10+ Year Member



I'm TOTALLY confused here. I'm a complete novice. Can anyone sticky me on this? Or can I sticky someone who can clear this up for me? Thanks.

grandpa

6:22 am on Dec 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can you show us the form and comment box that you already have?

I don't want to add anything to the template already. I'd just like to have the information emailed to me after they click the 'Submit' button and then have them automatically redirected to another website of my choice.

I've re-read this thread, and so yes, you will want to create a new script, and you will need to change the submit link on the form to point to your new script. In that script you will want to validate the input, and if OK send yourself mail and redirect. If something fails in the validation you will want to redirect back to your form page. NOTE - You CANNOT output anything from this script or the redirect will not work.

[edited by: grandpa at 6:36 am (utc) on Dec. 31, 2006]

Bubzeebub

6:35 am on Dec 31, 2006 (gmt 0)

10+ Year Member



Thanks grandpa. I have been fishing around with it a bit and seemed to have made just a baby step or two of progress. Now, when I fill out all the fields (Name, Email Address, Phone Number and Comments) and click the 'Submit' button, I get the email but it's blank! LOL

Here's the actual code (or a portion of it) from the section of the form. If you need more info let me know, or you can stick me as well:

<table width="235" style="height:217px; " border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" height="51"><img src="images/spacer.gif" alt="" width="4" height="1">Your full name:<br><input name="" type="text"></td>
</tr>
<tr>
<td valign="top" height="51"><img src="images/spacer.gif" alt="" width="4" height="1">E-mail address:<br><input name="" type="text"></td>
</tr>
<tr>
<td valign="top" height="115"><img src="images/spacer.gif" alt="" width="4" height="1">Your telephone:<br><input name="" type="text"></td>
</tr>
</table>

</td>
<td valign="top" width="270">Message:<br>
<textarea name="" rows="0" cols="0"></textarea><br><br style="line-height:5px; ">
<INPUT TYPE="submit" NAME="Submit" VALUE="Submit">&nbsp;<a href="http://www.widgets.com">
</td>
</tr>
</table>
</form>
</td>
</tr>
</table>

</td>
</tr>
</table>

</td>
</tr>
</table>

</td>
</tr>
<tr>
<td valign="top" height="100%" style="background:url(images/bgr_3.jpg) left top repeat-x #838383 ">
<table width="696" style="height:100%; " border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="top" width="190" class="footer">
<div style="margin:39px 0 0 39px; ">
<span>
</span>
</div>
</td>

grandpa

6:47 am on Dec 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<INPUT TYPE="submit" NAME="Submit" VALUE="Submit">&nbsp;<a href="http://www.widgets.com">

Change the URL on this line to point to your validation script. I made minor modifications to BlueGhost's example, calling it mailtest.php. Thus, the submit URL for your form would become something like: http://www.yourdomain.com/mailtest.php
I am assuming that widgets.com is the site where you want to redirect?

<?php
// This is mailtest.php
$to = "mail@mail.com"; // change to your email address
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$msg = $_POST['msg'];
$d = date('l dS \of F Y h:i:s A');
$sub = "form to mail";
$headers = "From: $name <$email>\n";
$headers .= "Content-Type: text/plain; charset=iso-8859-1\n";
$mes = "Subject: ".$subject."\n";
$mes .= "Message: ".$msg."\n";
$mes .= "Name: ".$name."\n";
$mes .= 'Email: '.$email."\n";
$mes .= 'Date & Time: '.$d;

if (empty($name) ¦¦ empty($email) ¦¦ empty($subject) ¦¦ empty($msg))
{
// Empty form field
// Redirect back to the form
header("Location: http://www.yourdomain.com/form.php");
exit();
}
elseif(!ereg("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
// Invalid email address
// Redirect back to the form
header("Location: http://www.yourdomain.com/form.php");
exit();
}
else
{
// Success - Mail to yourself and redirect
mail($to, $sub, $mes, $headers);
header("Location: http://www.widgets.com/");
exit();
}
?>

Bubzeebub

7:12 am on Dec 31, 2006 (gmt 0)

10+ Year Member



Ok....I've gone backwards a bit. :o(

I tried to do the changes as you suggested and now after I fill out the forms and comments I don't get any email. It now just redirects. Can you see anything here that might be wrong?

Here is the mail.php I'm using:

<html>

<head>

<title>Untitled</title>

</head>

<body>

<?php
$to = 'support@widgets.com'; //Address mail is to be sent to
$from = 'support@widgets.com'; //Sets the senders mail address
$subject = $_REQUEST['subject']; //Sets the subject from the mail form
$confirm = '/'; //change this value to your preferred page(it's set for homepage now)
$error = 'mailerror.php'; //Sets the page to direct to if the mail is unsent
// Don't touch anything else //
$mailheader = "";
$mailheader .= "From: support@widgets.com\r\n"; //so they know who it came from
if (isset($HTTP_POST_VARS)){
$mailbody = '';
while (list($key, $value) = each($HTTP_POST_VARS))
{
$mailbody .= $key . ' = ' . $value . "\r\n";
}
}
$mailforms = mail($to, $subject, $mailbody, $mailheader);
if($mailforms)
{
header('Location: ' . $confirm);
}
else
{
include('Location: ' . $error);
}

?>

</body>

</html>

Bubzeebub

8:01 am on Dec 31, 2006 (gmt 0)

10+ Year Member



I'm thinking it must be something in my contact page code that's screwring things up. So close but yet so far! Who is the king or queen that will solve the puzzle?

jatar_k

4:32 pm on Dec 31, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



is your copy being sent to the proper email address?

if the mail function wasn't returning true then it should go to this line

include('Location: ' . $error);

which isn't right, you should either change include to header or you need to just have

include $error;

as long as the $error page is in the same firectory as your mail script

grandpa

5:50 pm on Dec 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In spite of the warning to not change anything, I would change these 2 lines to a single line.

$mailheader = "";
$mailheader .= "From: support@widgets.com\r\n"; //so they know who it came from

To

$mailheader = "From: support@widgets.com"; //so they know who it came from

Try printing your variables to make sure they are what you expect. Just for testing, add these next 2 lines just before you execute the mail function.

echo "<br />To= $to Subj= $subject From = $mailheader";
exit();
$mailforms = mail($to, $subject, $mailbody, $mailheader);

(You're almost there!)

jatar_k

7:58 pm on Dec 31, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I don't see a form tag in your form

you will want something like

<form name="contactform" action="/mail.php" method="post">

that will point your form to the mail script. Then you can print your posted variables in your mail script by putting something like this in it

echo '<pre>my posted variables<p>';
print_r($_POST);
echo '</pre>';

then keep all your processing after that and do as grandpa showed to make sure you are getting those posted values properly into the other vars.

then it is a matter of getting the mail to go and then redirecting.

Bubzeebub

9:16 pm on Dec 31, 2006 (gmt 0)

10+ Year Member



Thanks for your replies. I changed the 2 mailheader lines to just one line. Still have a big problem because I don't know where exactly to add those lines Grandpa suggested.

Bubzeebub

9:39 pm on Dec 31, 2006 (gmt 0)

10+ Year Member



Can someone sticky me on this when you get a chance? Thanks.

grandpa

9:46 pm on Dec 31, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Add them right before the $mailforms line, as shown.


echo "<br />To= $to Subj= $subject From = $mailheader";
exit();
$mailforms = mail($to, $subject, $mailbody, $mailheader);

All this will do is display the information that you are trying to use to send your mail. You can visually see if there is something wrong with an address that will keep your mail form working. The exit(); will stop your code from redirecting so you can see the results. If they look OK, but the mail still won't work, post those results here so we can have a second look.

Bubzeebub

6:59 pm on Jan 1, 2007 (gmt 0)

10+ Year Member



Anyone PLEASE help! This is an urgent request. I have the mail.php file already. I believe the line that will allow the mail to get to me is: <form method=post action="http://www.widgets.com/mail.php">
I'm just not sure where to put that line in the code. Here is the page I'd like to insert that line in:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK HREF="style.css" TYPE="text/css" REL="stylesheet">
<title>Home</title>
</head>
<body>
<table width="100%" style="height:100%; " border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="top" height="190">
<table width="100%" style="height:190px; " border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="top" height="97" style="background:url(images/bgr_1.jpg) left repeat-x; ">
<table width="696" style="height:97px; " border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="top" width="191"><img src="images/logo.gif" alt="DTS" width="69" height="85"></td>
<td valign="top" width="351"><img src="images/slogan.jpg" alt="" style="margin-top:40px; "></td>
<td valign="top" width="154">&nbsp;
</td>
</tr>
</table>

</td>
</tr>
<tr>
<td valign="top" height="93">
<table width="100%" style="height:93px; " border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="191"><img src="images/spacer.gif" alt=""></td>
<td valign="top" width="505"><img src="images/about.jpg" alt="" border="0" style="margin-top:38px; "><img src="images/line_vertical.jpg" alt="" style="margin-left:20px; "><img src="images/services.jpg" alt="" border="0" style="margin-left:21px; "><img src="images/line_vertical.jpg" alt="" style="margin-left:19px; "><img src="images/solutions.jpg" alt="" border="0" style="margin-left:19px; "><img src="images/line_vertical.jpg" alt="" style="margin-left:19px; "><img src="images/contacts.jpg" alt="" border="0" style="margin-left:19px; ">
</td>
</tr>
</table>

</td>
</tr>
</table>

</td>
</tr>
<tr>
<td valign="top" height="416">
<table width="696" style="height:416px; " border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="top" width="182">
<table width="182" style="height:416px; " border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" height="179">
<table width="182" style="height:179px; " border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="bottom"><img src="images/1im1.jpg" alt=""></td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign="top" height="237"><p><img src="images/spacer.gif" alt=""></p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>Phone: (877) 387-4829</p>
<p>Fax: (914) 459-1171 </p></td>
</tr>
<tr>
<td valign="top" height="237">&nbsp;</td>
</tr>
</table>

</td>
<td valign="top" width="514">
<table width="514" style="height:416px; " border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="9"><img src="images/spacer.gif" alt=""></td>
<td valign="top" width="505">
<table width="505" style="height:416px; " border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" height="139">
<table width="505" style="height:139px; " border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" height="1"><img src="images/5line_1.jpg" alt=""></td>
</tr>
<tr>
<td valign="top" height="138">
<table width="505" style="height:138px; " border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="465" style="background:url(images/bgr_4.jpg) top left repeat-y; ">
<div style="margin:18px 0 0 19px; ">
<p align="right"><img src="images/5im1.jpg" alt="" align="left" style="margin-right:13px; "> Distributor Tax Services</p>
<p align="right">405 Tarrytown Road</p>
<p align="right">Suite 1573</p>
<p align="right"> White Plains, NY 10607 <br>
</p>
</div>
</td>
<td valign="top" width="40" style="background:url(images/bgr_5.jpg) top left repeat-y; "><img src="images/spacer.gif" alt=""></td>
</tr>
</table>

</td>
</tr>
</table>

</td>
</tr>
<tr>
<td valign="top" height="28"><img src="images/5line_2.jpg" alt=""></td>
</tr>
<tr>
<td valign="top" height="32"><img src="images/5_contacts.jpg" alt=""></td>
</tr>
<tr>
<td valign="top" height="217">
<form id="form" action="">
<table width="505" style="height:217px; " border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" width="235">
<table width="235" style="height:217px; " border="0" cellspacing="0" cellpadding="0">
<tr>
<td valign="top" height="51"><img src="images/spacer.gif" alt="" width="4" height="1">Your full name:<br><input name="" type="text"></td>
</tr>
<tr>
<td valign="top" height="51"><img src="images/spacer.gif" alt="" width="4" height="1">E-mail address:<br><input name="" type="text"></td>
</tr>
<tr>
<td valign="top" height="115"><img src="images/spacer.gif" alt="" width="4" height="1">Your telephone:<br><input name="" type="text"></td>
</tr>
</table>
</td>
<td valign="top" width="270"><div align="right">Message:<br>
<textarea name="" rows="0" cols="0"></textarea>
<br>
<br style="line-height:5px; ">
<img src="images/spacer.gif" alt="" width="115" height="1"><img src="images/spacer.gif" alt="" width="19" height="1">&nbsp;&nbsp;<a href="#"></a>
<INPUT TYPE="submit" NAME="Submit" VALUE="Submit">
&nbsp;<a href="http://www.widgets.com/mail.php">
</div></td>
</tr>
</table>
</form>
</td>
</tr>
</table>

</td>
</tr>
</table>

</td>
</tr>
</table>

</td>
</tr>
<tr>
<td valign="top" height="100%" style="background:url(images/bgr_3.jpg) left top repeat-x #838383 ">
<table width="696" style="height:100%; " border="0" cellspacing="0" cellpadding="0" align="center">
<tr>
<td valign="top" width="190" class="footer">
<div style="margin:39px 0 0 39px; ">
<span>
</span>
</div>
</td>
<td valign="top" width="506" class="footer">
<div style="margin-top:39px; "><strong><a href="index.html">Home</a><img src="images/spacer.gif" alt="" width="14" height="8"> <img src="images/line_vertical2.jpg" alt="" style="vertical-align:middle; "><img src="images/spacer.gif" alt="" width="14" height="8"><a href="recordkeeping.html">Recordkeeping</a><img src="images/spacer.gif" alt="" width="14" height="1"><img src="images/line_vertical2.jpg" alt="" style="vertical-align:middle; "><img src="images/spacer.gif" alt="" width="14" height="1"><a href="forms.html">Forms</a><img src="images/spacer.gif" alt="" width="14" height="8"><img src="images/line_vertical2.jpg" alt="" style="vertical-align:middle; "><img src="images/spacer.gif" alt="" width="14" height="1"><a href="recapexplained.htm">Recap Explained</a><img src="images/spacer.gif" alt="" width="14" height="8"><img src="images/line_vertical2.jpg" alt="" style="vertical-align:middle; "><img src="images/spacer.gif" alt="" width="14" height="1"><a href="contactus.html">Contact Us</a></strong><br>
<br style="line-height:5px; ">
<span>Distributor Tax Services &copy; 2007</span></div>
</td>
</tr>
<tr>
<td valign="top" class="footer">&nbsp;</td>
<td valign="top" class="footer">&nbsp;</td>
</tr>
</table>

</td>
</tr>
</table>

</BODY>
</HTML>

jatar_k

5:10 pm on Jan 3, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



replace this line

<form id="form" action="">

which is the form tag you have now, which has no action

with the new one