Jump to content

[SOLVED] Forms: Redirect to a Thank You Page


BHG

Recommended Posts

:)

 

Hi all...

 

Got this nice little contact form that is called into the page with the 'include' command.

 

So that I can use conversion tracking I now need to direct the user to a 'Thank You' page to be able to count the form submission as a 'conversion'.

 

Is it something simple to just change in the echo command below to do that?

 

thanks for any help with it.

 

<?php

 

$to = "info@youremailaccount.com";

 

if (!isset($_POST['submit'])) {

 

  showForm();

 

} else { //form submitted

 

  $error = 0;

 

  if(empty($_POST['name'])) {

    $error = 1;

    $errstr[] = "Please enter a name - click the back button to avoid re-entering the data";

  }

 

  if(!preg_match("/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/", $_POST['email'])) {

    $error = 1;

    $errstr[] = "Please enter a valid email address - click the back button to avoid re-entering the data";

  }

 

  if (empty($_POST['telephone'])) {

    $error = 1;

    $errstr[] = "Please enter a telephone number - click the back button to avoid re-entering the data";

  }

 

  if(empty($_POST['imagetext'])) {

    $error = 1;

    $errstr[] = "Please validate the image code - click the back button to avoid re-entering the data";

  } else {

    include "securimage.php";

    $img = new securimage();

    $valid = $img->check($_POST['imagetext']);

 

    if(!$valid) {

      $error = 1;

      $errstr[] = "The code you entered was incorrect - click the back button to avoid re-entering the data";

    }

  }

 

  if ($error == 1) {

    echo "<center>\n<font style=\"color: #FF0000\">\n";

    foreach($errstr as $err) {

      echo "<li> " . $err . "</li>\n";

    }

    echo "</font>\n</center>\n<br />\n\n";

 

    showForm();

 

  } else {

    @mail($to, "Client Contact Request" . $_POST['subject'],

    "Dear Company Name," . "\n\nName: " . $_POST['name'] . "\n\nEmail: " . $_POST['email'] . " \n\nTelephone: " . $_POST['telephone'] .

    "\n\nsent the following message.\n\nReason for email: " . $_POST['reason'] . "\n\nNotes: " . ($_POST['message']), "From: " . $_POST['email']);

   

    echo "\n\nThanks for contacting us.<a href=http://www.yourdomain.com>Return to the Home Page</a>";

  }

 

} //else submitted

 

function showForm()

{

  $_POST['message'] = @htmlspecialchars(@$_POST['message']);

 

  echo <<<EOD

 

<form method="post">

<table class="main" border="1" bordercolor="#bfdfef" cellpadding="3" cellspacing="1" width="300" align="center">

 

<tr>

<td align="center" colspan="2"><h2>Contact Request Form</h2></td>

</tr>

 

<tr>

<td id="main" align="right">Name:</td><td align="left"><input type="text" name="name" value="" /></td>

</tr>

 

<tr>

<td id="main" align="right">Email Address:</td><td align="left"><input type="text" name="email" value="" /></td>

</tr>

 

<tr>

<td id="main" align="right">Telephone Number:</td><td align="left"><input type="text" name="telephone" value="" /></td>

</tr>

 

<tr>

<td id="main" align="right">Contact Reason:</td>

<td align="left"><select name="reason">

<option value="Question" selected>Quick Question</option>

<option value="Sales Enquiry">Sales Enquiry</option>

<option value="Call Back">Call Back Request</option>

<option value="Other">Other</option>

</select>

</td>

</tr>

 

<tr>

<td id="main" align="right">Notes:</td><td align="left"><textarea name="message" rows="8" cols="40"></textarea></td>

</tr>

 

<tr>

<td id="main" align="right">Security Code:</td><td align="center" colspan="2"><img src="securimage_show.php" alt=""></td>

</tr>

 

<tr>

<td id="main" align="right">Please enter the security code from above:</td><td align="center"><input type="text" name="imagetext" /></td>

</tr>

 

<tr>

<td align="right"></td><td align="center"><input type="submit" name="submit" value="Send Request" /></td>

</tr>

 

</table>

</form>

EOD;

}

 

?>

Link to comment
Share on other sites

Hey there,

 

if you are simply wanting to send users to a thank you page after a valid submission happens in your form

you should use the header() function

ref: http://us.php.net/header

 

ex:

  } else {
    @mail($to, "Client Contact Request" . $_POST['subject'],
    "Dear Company Name," . "\n\nName: " . $_POST['name'] . "\n\nEmail: " . $_POST['email'] . " \n\nTelephone: " . $_POST['telephone'] .
    "\n\nsent the following message.\n\nReason for email: " . $_POST['reason'] . "\n\nNotes: " . ($_POST['message']), "From: " . $_POST['email']);
   
    //echo "\n\nThanks for contacting us.Return to the Home Page";
    header ('Location:' . $_SERVER['DOCUMENT_ROOT'] . '/thank_you.php');
    exit();
  }

 

just a side comment, it is best practice to keep your logic and presentation apart. there is no real benefit to having your form html inside a function that simply echo's it out. and the downside is it is difficult to maintain, customize or enhance. Moving your processing to another page is another way to keep your logic away from your presentation. having all your validation done on the same page as the form is very inflexible, what if you have another form that is close to this one but you need to make a few changes, you would need to have 2 different form pages with very similar logic, consolidating that logic into a single process page will save you time and energy down the line if not right up front.

 

good luck

Link to comment
Share on other sites

Thanks freakstyle...I switched out the code from...

 

echo "\n\nThanks for contacting us.Return to the Home Page";

 

to...

 

//echo "\n\nThanks for contacting us.Return to the Home Page";

    header ('Location:' . $_SERVER['DOCUMENT_ROOT'] . '/thank_you.php');

    exit();

 

and I get this error:

 

Warning: Cannot modify header information - headers already sent by (output started at /html/header.php:7) in /html/contactmeform.php on line 56

 

I found that error message on 'google' a lot but can't understand the fix for it.

 

I feel so close to getting it to work!

 

My intention is when they have filled in a form on my site to send them to a thank you page.

 

Adrock could you type in the code for the meta refresh suggestion?

 

 

Link to comment
Share on other sites

Hi darkfreaks...could you tell me what file?

 

I have a page that:

 

<includes: header;

leftnav;>

 

Then has some normal html code

 

<include the form>

 

html continues

 

includes rightnav;

footer;

 

should the ob_start(): be in my header, the html page or the form page?

 

if on the form page...this is how is starts: does it go inside or outside of the <?php start code?

 

<?php

 

$to = "info@mywebsite.com";

 

if (!isset($_POST['submit'])) {

 

  showForm();

 

} else { //form submitted

 

 

sorry if these questions are obvious!  ::)

Link to comment
Share on other sites

Hi darkfreaks...

 

tried that...was getting errors with the colon(:) on the end but when I changed it to a semi-colon(;) it accepted it but still gave the error:

 

Warning: Cannot modify header information - headers already sent by (output started...

 

this is my new code:

 

<?php

 

ob_start();

 

$to = "info@mywebsite.com";

 

if (!isset($_POST['submit'])) {

 

showForm();

 

} else { //form submitted

 

  $error = 0;

 

  if(!preg_match("/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/", $_POST['email'])) {

    $error = 1;

    $errstr[] = "Please enter a valid email address - click the back button to avoid re-entering the data";

  }

}

 

  if ($error == 1) {

    echo "<center>\n<font style=\"color: #FF0000\">\n";

    foreach($errstr as $err) {

      echo "<li> " . $err . "</li>\n";

    }

    echo "</font>\n</center>\n<br />\n\n";

 

    showForm();

 

  } else {

    @mail($to, "Client Contact Request" . $_POST['subject'],

    "Dear," . "\n\nName: " . $_POST['name'] . "\n\nEmail: " . $_POST['email'] . " \n\nTelephone: " . $_POST['telephone'] .

    "\n\nsent the following message.\n\nReason for email: " . $_POST['reason'] . "\n\nNotes: " . ($_POST['message']), "From: " . $_POST['email']);

//echo "\n\nThanks for contacting us.Return to the Home Page";

    header ('Location:' . $_SERVER['DOCUMENT_ROOT'] . '/thank_you.php');

    exit();

  }

 

} //else submitted

 

function showForm()

{

$_POST['message'] = @htmlspecialchars(@$_POST['message']);

 

echo <<<EOD

 

<form method="post">

<table class="main" border="1" bordercolor="#bfdfef" cellpadding="3" cellspacing="1" width="300" align="center">

<tr>

<td id="main" align="right">Name:</td>

<td align="left"><input type="text" name="name" value="" /></td>

</tr>

<tr>

<td id="main" align="right">Email Address:</td>

<td align="left"><input type="text" name="email" value="" /></td>

</tr>

<tr>

<td id="main" align="right">Telephone Number:</td>

<td align="left"><input type="text" name="telephone" value="" /></td>

</tr>

<tr>

<td align="right"></td>

<td align="center"><input type="submit" name="submit" value="Send Request" /></td>

</tr>

</table>

</form>

EOD;

}

 

?>

Link to comment
Share on other sites

1. no need for ob_start; remove it.

2. Your brackets are mis-matched.

 

} //else submitted

 

You already closed that if here:

 

    $errstr[] = "Please enter a valid email address - click the back button to avoid re-entering the data";

  }

} // <--- closed the if.

 

The result of this is that showForm(); is executed every time regardless of whether the form was submitted or not. That's probably your 'headers already sent' error.

 

3. Make sure there is no empty space before the first <?

Link to comment
Share on other sites

Hi BlueSkyIs,

 

Thanks for your help also...took out the ob_start and tried to implement your suggestions...I could see where you referred to the 'valid email address' line but couldn't see two } closes under that. I do see two under the if(!$valid) but if I remove that close I get a parse error.

 

I have tidied up the code a bit, could you look one more time and type in the right code for me in your answer? (please : ) )

 

should I leave this line: header ('Location:' . $_SERVER['DOCUMENT_ROOT'] . '/thank_you.php'); exactly as typed or do I put in my actual domain name there?

 

Thanks in advance to any help and thanks to all who have tried so far.

 

 

<?php

 

$to = "info@mywebsite.com";

 

if (!isset($_POST['submit'])) {

 

showForm();

 

} else { //form submitted

 

$error = 0;

 

if(empty($_POST['name'])) {

$error = 1;

$errstr[] = "Please enter a name - click the back button to avoid re-entering the data";

}

 

if(!preg_match("/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/", $_POST['email'])) {

$error = 1;

$errstr[] = "Please enter a valid email address - click the back button to avoid re-entering the data";

}

 

if (empty($_POST['telephone'])) {

$error = 1;

$errstr[] = "Please enter a telephone number - click the back button to avoid re-entering the data";

}

 

if(empty($_POST['imagetext'])) {

$error = 1;

$errstr[] = "Please validate the image code - click the back button to avoid re-entering the data";

} else {

include "securimage.php";

$img = new securimage();

$valid = $img->check($_POST['imagetext']);

 

if(!$valid) {

$error = 1;

$errstr[] = "The code you entered was incorrect - click the back button to avoid re-entering the data";

}

}

 

if ($error == 1) {

echo "<center>\n<font style=\"color: #FF0000\">\n";

foreach($errstr as $err) {

echo "<li> " . $err . "</li>\n";

}

echo "</font>\n</center>\n<br />\n\n";

 

showForm();

 

} else {

@mail($to, "Client Contact Request" . $_POST['subject'],

"Dear ," . "\n\nName: " . $_POST['name'] . "\n\nEmail: " . $_POST['email'] . " \n\nTelephone: " . $_POST['telephone'] .

"\n\nsent the following message.\n\nReason for email: " . $_POST['reason'] . "\n\nNotes: " . ($_POST['message']), "From: " . $_POST['email']);

//echo "\n\nThanks for contacting us.Return to the Home Page";

header ('Location:' . $_SERVER['DOCUMENT_ROOT'] . '/thank_you.php');

exit();

}

} //else submitted

 

function showForm()

{

$_POST['message'] = @htmlspecialchars(@$_POST['message']);

 

echo <<<EOD

 

<form method="post">

<table>

<tr>

<td><h2>Contact Request Form</h2></td>

</tr>

<td><input type="submit" name="submit" value="Send Request" /></td>

</tr>

</table>

</form>

EOD;

}

 

?>

Link to comment
Share on other sites

I think I've found a work around...at least it works but I'm not sure until I look at my google analytics whether it can track the 'Thank You' page display as a 'conversion' because of the  meta refresh redirect!

 

anyway thanks to everyone's help and if there is a simpler fix please let me know.

 

Basically what I did was in the place of the echo "\n\n\Thanks for Contacting us." I put an 'include' instead;

 

include("thankyou2.php");

 

Then on the 'thankyou2.php' I included the meta refresh command redirecting to my 'Thank You' page and voila!

 

Here is all the code for anyone who wants to accomplish the same thing:

 

'contactus.php'

 

<?

$page_title="";

include("header.php");

include("leftnav.php");           ?>

 

<?

include("contactmeform.php");

?>

 

We are located on the NW Corner. Please feel free to visit anytime Monday through Friday, 9 - 5pm.

 

</td>

 

<?

include("rightnav.php");

include("footer.php");

?>

 

contactmeform.php

 

<?php

 

$to = "info@mywebsite.com";

 

if (!isset($_POST['submit'])) {

 

showForm();

 

} else { //form submitted

 

$error = 0;

 

if(!preg_match("/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/", $_POST['email'])) {

$error = 1;

$errstr[] = "Please enter a valid email address";

}

 

if(empty($_POST['imagetext'])) {

$error = 1;

$errstr[] = "Please validate the image code";

} else {

include "securimage.php";

$img = new securimage();

$valid = $img->check($_POST['imagetext']);

 

if(!$valid) {

$error = 1;

$errstr[] = "The code you entered was incorrect - please try again.";

}

}

if ($error == 1) {

echo "<center>\n<font style=\"color: #FF0000\">\n";

foreach($errstr as $err) {

echo "<li> " . $err . "</li>\n";

}

echo "</font>\n</center>\n<br />\n\n";

 

showForm();

 

} else {

@mail($to, "Client Contact Request" . $_POST['subject'],

"Dear ," . "\n\nName: " . $_POST['name'] . "\n\nEmail: " . $_POST['email'] . " \n\nTelephone: " . $_POST['telephone'] . "\n\nsent the following message.\n\nReason for email: " . $_POST['reason'] . "\n\nNotes: " . ($_POST['message']), "From: " . $_POST['email']);

include("thankyou2.php");

}

} //else submitted

 

function showForm()

{

$_POST['message'] = @htmlspecialchars(@$_POST['message']);

 

echo <<<EOD

 

<form method="post">

<table class="main" border="1" bordercolor="#bfdfef" cellpadding="3" cellspacing="1" width="300" align="center">

<tr>

<td align="center" colspan="2"><h2>Contact Request Form</h2></td>

</tr>

<tr>

<td id="main" align="right">Name:</td>

<td align="left"><input type="text" name="name" value="" /></td>

</tr>

<tr>

<td id="main" align="right">Email Address:</td>

<td align="left"><input type="text" name="email" value="" /></td>

</tr>

<tr>

<td id="main" align="right">Telephone Number:</td>

<td align="left"><input type="text" name="telephone" value="" /></td>

</tr>

<tr>

<td id="main" align="right">Contact Reason:</td>

<td align="left"><select name="reason">

<option value="Question" selected>Quick Question</option>

<option value="Sales Enquiry">Sales Enquiry</option>

<option value="Call Back">Call Back Request</option>

<option value="Other">Other</option>

</select>

</td>

</tr>

<tr>

<td id="main" align="right">Notes:</td>

<td align="left"><textarea name="message" rows="8" cols="40"></textarea></td>

</tr>

<tr>

<td id="main" align="right">Security Code:</td>

<td align="center" colspan="2"><img src="securimage_show.php" alt=""></td>

</tr>

<tr>

<td id="main" align="right">Please enter the security code from above:</td>

<td align="center"><input type="text" name="imagetext" /></td>

</tr>

<tr>

<td align="right"></td>

<td align="center"><input type="submit" name="submit" value="Send Request" /></td>

</tr>

</table>

</form>

EOD;

}

 

?>

 

thankyou2.php

 

<meta http-equiv="refresh" content="0;url=http://www.mywebsite.com/thankyou.php" />

 

Please wait while the form submits....thank you.

 

thankyou.php

 

<?

$page_title="Thank you for submitting your request.";

$page_keywords="";

$page_description="";

include("header.php");

include("leftnav.php");           ?>

 

Thank you for contacting us and we will respond as soon as we can.

 

Please continue browsing our site or <a href="/index.php">click here</a> to return to the home page.

 

</td>

 

<?

include("rightnav.php");

include("footer.php");

?>

 

and that's all there is to it!  : )

 

don't forget to remove the secureimage references and the secureimage error check for it to work ok for you.

Link to comment
Share on other sites

The above does work but I was including too many steps...I moved the meta refresh statement into the form itself, in place of the 'include' part and it works great!

 

Just like Adrock suggested many moons ago but I couldn't get it to work! (had the quotes in all the wrong places)

 

the bit of code now looks like this:

 

} else {

@mail($to, "Client Contact Request" . $_POST['subject'],

"Dear ," . "\n\nName: " . $_POST['name'] . "\n\nEmail: " . $_POST['email'] . " \n\nTelephone: " . $_POST['telephone'] . "\n\nsent the following message.\n\nReason for email: " . $_POST['reason'] . "\n\nNotes: " . ($_POST['message']), "From: " . $_POST['email']);

echo "<meta http-equiv=refresh content=0;url=http://www.yourwebsite.com/thankyou.php>";

}

} //else submitted

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.