Change page using Meta after filling-out Form

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

lilnicky37
Forum Newbie
Posts: 10
Joined: Wed Apr 14, 2010 11:56 am

Change page using Meta after filling-out Form

Post by lilnicky37 »

Hi all,

I'm trying to create some code where a user fills in a form, and once they hit "submit", the page will change to either a confirmation, or an error page.
However, whenever "submit" is hit, nothing happens. The email is sent (or not sent) as it is supposed to, but the page will not change to the confirmation or error page.
I'm also not receiving any PHP related error messages.
Can anyone help me out?

Code: Select all

<?php

function isValidEmail($address) {
return preg_match("/^[a-z0-9._\-]+[@]([a-z0-9\-]+[.])+([a-z]{2,4})\$/i",
$address);
}

$EmailFrom = "Contact Form";
$EmailTo = "support@xxxxxxxxx.com";
$Subject = Trim(stripslashes($_POST['subject']));
$Name =Trim(stripslashes($_POST['name']));
$Email = Trim(stripslashes($_POST['email']));
$Message = Trim(stripslashes($_POST['message']));

$validationOK=true;
if (!$validationOK) {
  echo'<meta http-equiv="refresh" content="0;URL=http://www.xxxxxxxxx.com/error.htm"/>';
  exit;
}

if(trim($Name) == '' || trim($Email) == '' || trim($Message) == '') {
echo'<meta http-equiv="refresh" content="0;URL=http://www.xxxxxxxxx.com/error.htm"/>';
exit;
}

$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

if(!isValidEmail($Email)) {
echo'<meta http-equiv="refresh" content="0;URL=http://www.xxxxxxxxxx.com/error.htm"/>';
}
else {
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
}
if ($success){
  echo'<meta http-equiv="refresh" content="0;URL=http://www.xxxxxxxxx.com/error.htm"/>';
}
else{
  echo'<meta http-equiv="refresh" content="0;URL=http://www.xxxxxxxxxx.com/contactthanks.php"/>';
}
?>
I am very new to PHP, so my apologies if there are any obvious errors!
solid
Forum Commoner
Posts: 28
Joined: Wed Aug 12, 2009 11:56 am

Re: Change page using Meta after filling-out Form

Post by solid »

Change:

Code: Select all

if ($success){
  echo'<meta http-equiv="refresh" content="0;URL=http://www.xxxxxxxxx.com/error.htm"/>';
}
else{
  echo'<meta http-equiv="refresh" content="0;URL=http://www.xxxxxxxxxx.com/contactthanks.php"/>';
}
to:

Code: Select all

if ($success){
  header('Location: http://www.xxxxxxxxx.com/error.htm');
}
else{
  header('Location: http://www.xxxxxxxxx.com/contactthanks.php');
}
exit;
lilnicky37
Forum Newbie
Posts: 10
Joined: Wed Apr 14, 2010 11:56 am

Re: Change page using Meta after filling-out Form

Post by lilnicky37 »

I updated the code:

Code: Select all

<?php

function isValidEmail($address) {
return preg_match("/^[a-z0-9._\-]+[@]([a-z0-9\-]+[.])+([a-z]{2,4})\$/i",
$address);
}

$EmailFrom = "Contact Form";
$EmailTo = "support@warmglowbooks.com";
$Subject = Trim(stripslashes($_POST['subject']));
$Name =Trim(stripslashes($_POST['name']));
$Email = Trim(stripslashes($_POST['email']));
$Message = Trim(stripslashes($_POST['message']));

$validationOK=true;
if (!$validationOK) {
  header('Location: http://www.xxxxxxxxx.com/error.htm');
  exit;
}

if(trim($Name) == '' || trim($Email) == '' || trim($Message) == '') {
header('Location: http://www.xxxxxxxxxxx/error.htm');
exit;
}

$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

if(!isValidEmail($Email)) {
header('Location: http://www.xxxxxxxxxx.com/error.htm');
}
else {
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
}
if ($success){
  header('Location: http://www.xxxxxxxxxx.com/contactthanks.php');
}
else{
  header('Location: http://www.xxxxxxxxxx.com/error.htm');
}
?>
The email's are sending/not sending as they should, but it still will not navigate to the error.htm or contactthanks.php pages.
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Re: Change page using Meta after filling-out Form

Post by Technocrat »

Add exit or die after each of your header().

Also $var != '' isn't correct and actually produces a warning. It should be !empty($var).
lilnicky37
Forum Newbie
Posts: 10
Joined: Wed Apr 14, 2010 11:56 am

Re: Change page using Meta after filling-out Form

Post by lilnicky37 »

Thanks, I have updated the code as below:

Code: Select all

<?php

function isValidEmail($address) {
return preg_match("/^[a-z0-9._\-]+[@]([a-z0-9\-]+[.])+([a-z]{2,4})\$/i",
$address);
}

$EmailFrom = "Contact Form";
$EmailTo = "support@warmglowbooks.com";
$Subject = Trim(stripslashes($_POST['subject']));
$Name =Trim(stripslashes($_POST['name']));
$Email = Trim(stripslashes($_POST['email']));
$Message = Trim(stripslashes($_POST['message']));

$validationOK=true;
if (!$validationOK) {
  header('Location: http://www.xxxxxxxxxxxxxx.com/error.htm');
  exit;
}

if(!empty($Name) || !empty($Email)|| !empty($Message)) {
header('Location: http://www.xxxxxxxxxx.com/error.htm');
exit;
}

$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

if(!isValidEmail($Email)) {
header('Location: http://www.xxxxxxxxxx.com/error.htm');
exit;
}
else {
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
}
if ($success){
  header('Location: http://www.xxxxxxxxxx.com/contactthanks.php');
exit;
}
else{
  header('Location: http://www.xxxxxxxxx.com/error.htm');
exit;
}

?>
But the result is the same. Clicking "submit" has no effect on changing the page.
I've also included the form code from the actual webpage. Perhaps that will make a difference:

Code: Select all

<form action="contact.php" method="post" id="contactform">
<ol>
<li> <label for="name">Name <span class="red">*</span></label> <input id="name" name="name" class="text" /> </li>
<li> <label for="email">Your Email <span class="red">*</span></label> <input id="email" name="email" class="text" /> </li>
<li> <label for="company">Company</label>
<input id="company" name="company" class="text" /></li>
<li> <label for="subject">Subject <span class="red">*</span></label>
<input id="subject" name="subject" class="text" /></li>
<li> <label for="message">Message <span class="red">*</span></label> <textarea id="message" name="message" rows="6" cols="50"></textarea>
</li>
<li class="buttons"> <input name="imageField" id="imageField" src="images/send.gif" type="image" />
</li>
</ol>
</form>
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Re: Change page using Meta after filling-out Form

Post by Technocrat »

Close you need the if to be:

Code: Select all

if(empty($Name) || empty($Email)|| empty($Message)) {
Also:

Code: Select all

$validationOK=true;
if (!$validationOK) {
  header('Location: http://www.xxxxxxxxxxxxxx.com/error.htm');
  exit;
}
Make no sense in the code you have now. You are setting it to true then validating it?
lilnicky37
Forum Newbie
Posts: 10
Joined: Wed Apr 14, 2010 11:56 am

Re: Change page using Meta after filling-out Form

Post by lilnicky37 »

Ok, I updated the If (empty...) entry, and removed the validation() entry.

Any idea on why the page is not changing upon submission?
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Re: Change page using Meta after filling-out Form

Post by Technocrat »

I put all your code into a test file and I get sent to http://www.xxxxxxxxxx.com/contactthanks.php and I get an email sent to me. Is this not happening for you, or is this not the result you are expecting?
lilnicky37
Forum Newbie
Posts: 10
Joined: Wed Apr 14, 2010 11:56 am

Re: Change page using Meta after filling-out Form

Post by lilnicky37 »

That is what I want to happen.
When I fill in the form on my end (on the website), and I press submit, the form does not reset (meaning the text remains there), and I do not navigate to contactthanks.php. It appears as though nothing happens.
I do however receive the correct email, even though it doesn't look like the form submitted the data properly.
User avatar
Technocrat
Forum Contributor
Posts: 127
Joined: Thu Oct 20, 2005 7:01 pm

Re: Change page using Meta after filling-out Form

Post by Technocrat »

Is the HTML and the PHP in 2 separate files?
lilnicky37
Forum Newbie
Posts: 10
Joined: Wed Apr 14, 2010 11:56 am

Re: Change page using Meta after filling-out Form

Post by lilnicky37 »

Yes, the HTML/form is on one page, contact..html
And the PHP is in its own page, contact.php
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Change page using Meta after filling-out Form

Post by mikosiko »

lilnicky37 wrote:That is what I want to happen.
....
I do however receive the correct email, even though it doesn't look like the form submitted the data properly.
what exactly are the differences in the data?... could you post an example of what did you input in the form and what did you get in the email?
lilnicky37
Forum Newbie
Posts: 10
Joined: Wed Apr 14, 2010 11:56 am

Re: Change page using Meta after filling-out Form

Post by lilnicky37 »

Sorry, I don't think I'm making my issue clear.
The email aspect of this code works perfectly.
When the user clicks "submit" on the form, I receive the email like I am supposed.
However, the webpage is also supposed to navigate to contactthanks.php for the user, and this is not happening.
After the user hits "submit", the web page does nothing, and it looks as though the form was not submitted, even though it was.
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Change page using Meta after filling-out Form

Post by mikosiko »

therefore... this is the part of the code "failing" right?

Code: Select all

if ($success){
  header('Location: http://www.xxxxxxxxxx.com/contactthanks.php');
exit;
}
else{
  header('Location: http://www.xxxxxxxxx.com/error.htm');
exit;
}
did you try to debug that code using echo per example:

Code: Select all

if ($success){
  echo "... Success...";
  //header('Location: http://www.xxxxxxxxxx.com/contactthanks.php');
exit;
}
else{
   echo "... Fail!! .." . $success . " this was the value of success variable";
  //header('Location: http://www.xxxxxxxxx.com/error.htm');
exit;
}
also... post the code of your page "error.htm"
lilnicky37
Forum Newbie
Posts: 10
Joined: Wed Apr 14, 2010 11:56 am

Re: Change page using Meta after filling-out Form

Post by lilnicky37 »

Thanks for being patient with me, I'll admit that this is a friends code, and I've been assigned to fix it, so I may have overlooked something obvious.
I replay the if($success) bit that you suggested for debugging. Upon submission, I did receive the "...success..." text on the page, but it did not change to contactthanks.php like I have hoped.

So, below is my code at this point:
First, contact.html, the page on which the form is located:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>XXXXXXXX</title>

<link rel="shortcut icon" href="favicon.gif" />
<link rel="icon" href="/favicon.gif" type="image/gif" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="XXXXXXXXXXXXXXXXXX" />
<meta name="keywords" content="XXXXXXXXXXXXXXXXXXXXX" />
<link href="style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
// <![CDATA[
jQuery(document).ready(function(){
$('#contactform').submit(function(){ var action = $(this).attr('action');
$.post(action, { name: $('#name').val(),
email: $('#email').val(),
company: $('#company').val(),
subject: $('#subject').val(),
message: $('#message').val()
},
function(data){
$('#contactform #submit').attr('disabled','');
$('.response').remove();
$('#contactform').before('<p class="response">'+data+'</p>');
$('.response').slideDown();
if(data=='Message sent!') $('#contactform').slideUp();
}
); return false;
});
});
// ]]>
</script>
</head>
<body>
<div class="main">
<div class="header">
<div class="logo"><a href="index.html"><img src="images/logo.gif" alt="logo" border="0" height="140" width="334" /></a></div>
<div style="width: 550px; text-align: right;" class="simple_text">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; Search </div>
<div style="margin-left: 0px; width: 276px;" class="search">
<form style="margin-left: 0px; width: 261px;" name="search" method="post" action="search1.php">&nbsp;&nbsp;<input name="find" type="text" />
<select name="field"><option value="xxxx">XXXX</option><option value="xxxxx">xxxxx</option><option value="XXXX">xxxx</option></select>
<input name="searching" value="yes" type="hidden" /><input id="sort" name="sort" value="price" type="hidden" /><input id="direction" name="direction" value="DESC" type="hidden" /><input name="search" src="images/search.gif" class="button" type="image" /></form>
</div>
<div class="clr"></div>
<div class="header_text_bg2">
<div class="header_text2">
<h2><span>Contact </span> Us </h2>
<p>XXXXXXXXXXXXXX</p>
<div class="clr"></div>
</div>
<div class="clr"></div>
</div>
<div class="clr"></div>
<div class="menu_resize">
<div class="menu">
<div style="width: 512px;" class="menu">
<ul>
<li><a href="index.html"><span>Home</span></a></li>
<li><a href="portfolio.html"><span>About Us</span></a></li>
<li><a href="login.php"><span>Login</span></a></li>
<li><a href="register.php"><span>Register</span></a></li>
<li><a href="member.php"><span>Account</span></a></li>
<li><a href="contact.html" class="active"><span>Contact
us</span></a></li>
</ul>
</div>
</div>
<div class="Twitter"><img src="images/twitter.gif" alt="picture" height="20" width="20" />
<p> <a target="_blank" href="http://www.twitter.com/xxxxxxxxxxx">Follow us on Twitter</a></p>
</div>
<div class="clr"></div>
</div>
</div>
<div class="clr"></div>
<div class="body">
<div class="body_big">
<h2><span>Contact </span> Form</h2>
<p><strong>Tell Us What's On Your Mind</strong></p>
<div class="bg"></div>
<form action="contact.php" method="post" id="contactform">
<ol>
<li> <label for="name">Name <span class="red">*</span></label> <input id="name" name="name" class="text" /> </li>
<li> <label for="email">Your Email <span class="red">*</span></label> <input id="email" name="email" class="text" /> </li>
<li> <label for="company">Company</label>
<input id="company" name="company" class="text" /></li>
<li> <label for="subject">Subject <span class="red">*</span></label>
<input id="subject" name="subject" class="text" /></li>
<li> <label for="message">Message <span class="red">*</span></label> <textarea id="message" name="message" rows="6" cols="50"></textarea>
</li>
<li class="buttons"> <input name="imageField" id="imageField" src="images/send.gif" type="image" />
</li>
</ol>
</form>
</div>
<div class="body_small">
<h2><span>Get </span> in touch!</h2>
<p><strong>Tel:</strong> +XXX XXX XXXX<strong></strong><br />
<strong>Email:</strong>&nbsp;<a href="contact.html#">support@xxxxxxxxxx.com</a><br />
<strong>Address:</strong> XXXXXXXXXXX</p>
<p><a href="cart.php">View Cart</a>&nbsp;</p>
</div>
<div class="clr"></div>
</div>
<div class="footer">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="portfolio.html">Company Information</a></li>
<li><a href="member.php">User Page</a></li>
<li><a href="terms.php">Terms & Conditions</a></li>
<li><a href="privacy.php">Privacy Policy</a></li>
<li><a href="contact.html">Contact</a></li>
<li><a href="http://www.twitter.com/xxxxxxxxx"><img src="images/twitter_footer.gif" alt="picture" border="0" height="15" width="80" /></a></li>
</ul>
<p>Copyright © 2010&nbsp;XXXXXXXXXXXXX.
All Rights Reserved</p>
<div class="clr"></div>
</div>
<div class="clr"></div>
</div>
</body></html>
Next, the relevant PHP file, contact.php

Code: Select all

<?php

function isValidEmail($address) {
return preg_match("/^[a-z0-9._\-]+[@]([a-z0-9\-]+[.])+([a-z]{2,4})\$/i",
$address);
}

$EmailFrom = "Contact Form";
$EmailTo = "support@xxxxxxxxx.com";
$Subject = Trim(stripslashes($_POST['subject']));
$Name =Trim(stripslashes($_POST['name']));
$Email = Trim(stripslashes($_POST['email']));
$Message = Trim(stripslashes($_POST['message']));


if(empty($Name)||empty($Email)||empty($Message)) {
header('Location: http://www.xxxxxxxxx.com/error.htm');
exit;
}

$Body = "";
$Body .= "Name: ";
$Body .= $Name;
$Body .= "\n";
$Body .= "Email: ";
$Body .= $Email;
$Body .= "\n";
$Body .= "Message: ";
$Body .= $Message;
$Body .= "\n";

if(!isValidEmail($Email)) {
header('Location: http://www.xxxxxxxxxx.com/error.htm');
exit;
}
else {
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
}
if ($success){
  echo "... Success...";
  //header('Location: http://www.xxxxxxxxxx.com/contactthanks.php');
exit;
}
else{
   echo "... Fail!! .." . $success . " this was the value of success variable";
  //header('Location: http://www.xxxxxxxxx.com/error.htm');
exit;
}
?>
Lastly, error.htm:

Code: Select all

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head><title>XXXXXXXX</title>

<link rel="shortcut icon" href="favicon.gif" />
<link rel="icon" href="/favicon.gif" type="image/gif" />
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta name="description" content="xxxxxxxxxxxx" />
<meta name="keywords" content="xxxxxxxxxxxxxxx" />
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="main">
<div class="header">
<div class="logo"><a href="index.html"><img src="images/logo.gif" alt="logo" border="0" height="140" width="334" /></a></div>
<div style="width: 550px; text-align: right;" class="simple_text">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;
&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; Search </div>
<div style="margin-left: 0px; width: 276px;" class="search">
<form style="margin-left: 0px; width: 261px;" name="search" method="post" action="search1.php">&nbsp;&nbsp;<input name="find" type="text" />
<select name="field"><option value="xxxxxx">xxxxxxx</option><option value="xxxxxxx">xxxxxx</option><option value="xxxx">xxxx</option></select>
<input name="searching" value="yes" type="hidden" /><input id="sort" name="sort" value="price" type="hidden" /><input id="direction" name="direction" value="DESC" type="hidden" /><input name="search" src="images/search.gif" class="button" type="image" /></form>
</div>
<div class="clr"></div>
<div class="header_text_bg2">
<div class="header_text2">
<h2><span>Contact </span> Us </h2>
<p>xxxxxxxxxxxxxxxx</p>
<div class="clr"></div>
</div>
<div class="clr"></div>
</div>
<div class="clr"></div>
<div class="menu_resize">
<div class="menu">
<div style="width: 512px;" class="menu">
<ul>
<li><a href="index.html"><span>Home</span></a></li>
<li><a href="portfolio.html"><span>About Us</span></a></li>
<li><a href="login.php"><span>Login</span></a></li>
<li><a href="register.php"><span>Register</span></a></li>
<li><a href="member.php"><span>Account</span></a></li>
<li><a href="contact.html" class="active"><span>Contact
us</span></a></li>
</ul>
</div>
</div>
<div class="Twitter"><img src="images/twitter.gif" alt="picture" height="20" width="20" />
<p> <a target="_blank" href="http://www.twitter.com/xxxxxxxxx">Follow us on Twitter</a></p>
</div>
<div class="clr"></div>
</div>
</div>
<div class="clr"></div>
<div class="body">
<div class="body_big">
<h2><span>We're </span> Sorry</h2>
<p><strong>An Error Has Occurred. Please Try Again.</strong></p>
</div>
<div class="bg"></div>
<p><a href="http://www.xxxxxxxxxx.com/contact.html">Back to Contact Form</a></p>
</div>
<div class="body_small">
<h2><span>Get </span> in touch!</h2>
<p><strong>Tel:</strong> +xxx xxx xxxx<strong></strong><br />
<strong>Email:</strong>&nbsp;<a href="contact.html#">support@xxxxxxxxx.com</a><br />
<strong>Address:</strong> xxxxxxxxxxx</p>
<p><a href="cart.php">View Cart</a>&nbsp;</p>
</div>
<div class="clr"></div>
</div>
<div class="footer">
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="portfolio.html">Company Information</a></li>
<li><a href="member.php">User Page</a></li>
<li><a href="terms.php">Terms & Conditions</a></li>
<li><a href="privacy.php">Privacy Policy</a></li>
<li><a href="contact.html">Contact</a></li>
<li><a href="http://www.twitter.com/xxxxxxxxxx"><img src="images/twitter_footer.gif" alt="picture" border="0" height="15" width="80" /></a></li>
</ul>
<p>Copyright © 2010&nbsp;xxxxxxxxxxx
All Rights Reserved</p>
<div class="clr"></div>
</div>
<div class="clr"></div>
</div>
</body></html>
Post Reply