Forum Moderators: coopster

Message Too Old, No Replies

Email form wont go to thank you page

         

slimey

6:56 am on May 21, 2006 (gmt 0)

10+ Year Member



Hi all i for some reason cant get my email form to go to the thank you page afterit has been submitted and is validated.

Anyone shed some light on the problem?

<?php
// Create an empty array to hold the error messages.
$arrErrors = array();
//Only validate if the Submit button was clicked.
if (!empty($_POST['Submit'])) {
// Each time there's an error, add an error message to the error array
// using the field name as the key.
if ($_POST['name']=='')
$arrErrors['name'] = 'Please provide your name.';
if ($_POST['phone']=='')
$arrErrors['phone'] = 'Please provide your mobile number.';
if (!preg_match("/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/", $_POST['email']) ) {
$arrErrors['email'] = 'Please provide a valid email address.';}
if ($_POST['inquiry']=='')
$arrErrors['inquiry'] = 'Please type in your Shout Out.';
if (count($arrErrors) == 0) {
// If the error array is empty, there were no errors.
// Insert form processing here.



$email = $_POST['email'] ;
$name = stripslashes($_POST['name']) ;
$inquiry = stripslashes($_POST['inquiry']) ;
$phone = stripslashes($_POST['phone']) ;

$to="email@email.com.au";
$mailheaders = "MIME-Version: 1.0\n";
$mailheaders .= "Content-type: text/html; charset=iso-8859-1\n";
$mailheaders .= "From: $email" . "\r\n" ;
$mailheaders .= "Cc: ";
$message="

-snip-

";
mail( $to, "website - Shout out", $message, $mailheaders);
header( "Location: [website.com.au...] );




} else {
// The error array had something in it. There was an error.
// Start adding error text to an error string.
$strError = '<div class="formerror"><p><img src="images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><span class="list">Please check the following and try again:</span></p><ul class="list">';
// Get each error and add it to the error string
// as a list item.
foreach ($arrErrors as $error) {
$strError .= "<li>$error</li>";
}
$strError .= '</ul></div><p>';
}
}
?>
<?php echo $strError;?>

<form method="post" action="<?php echo $PHP_SELF;?>">
<fieldset >
<legend class="heading">Send us a Shout Out</legend>
<p<?php if (!empty($arrErrors['name'])) echo ' class="formerror"';?>>
<label for="name" class="content">Name:</label>
<br>
<input name="name" type="text" id="name" value="<?php echo $_POST['name']?>">
<?php if (!empty($arrErrors['name'])) echo '<img src="images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['name'].'</span>';?>
</p>
<p<?php if (!empty($arrErrors['phone'])) echo ' class="formerror"';?>>
<label for="phone" class="content">Mobile Phone:</label>
<br>
<input name="phone" type="text" id="phone" value="<?php echo $_POST['phone']?>">
<?php if (!empty($arrErrors['phone'])) echo '<img src="images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['phone'].'</span>';?>
</p>
<p<?php if (!empty($arrErrors['email'])) echo ' class="formerror"';?>>
<label for="email" class="content">Email:</label>
<br>
<input name="email" type="text" id="email" size="35" value="<?php echo $_POST['email']?>">
<?php if (!empty($arrErrors['email'])) echo '<img src="images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><br /><span class="errortext">'.$arrErrors['email'].'</span>';?>
</p>
<p<?php if (!empty($arrErrors['inquiry'])) echo ' class="formerror"';?>>
<label for="inquiry" class="content">Shout Out :</label>
<br>
<textarea name="inquiry" cols="28" rows="5" id="inquiry"><?php echo $_POST['inquiry']?></textarea>
<?php if (!empty($arrErrors['inquiry'])) echo '<img src="images/triangle_error.gif" width="16" height="16" hspace="5" alt=""><span class="errortext">'.$arrErrors['inquiry'].'</span>';?>
</p>
<p<?php if (!empty($arrErrors['inquiry'])) echo ' class="formerror"';?>>&nbsp;</p>

<input type="submit" name="Submit" value="Submit">

</fieldset>
</form>

slimey

7:15 am on May 21, 2006 (gmt 0)

10+ Year Member



Ok this is odd, for some reason when it is surrounded by the contact page html it doesnt work, but when it is just the php code it does.

I have the right file extentions and everything and it does send the email, it just doesnt go to the thankyou page it reloads the contact page again.

Whats happening here?

dreamcatcher

7:50 am on May 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi slimey,

I notice you are using the header function. This must not be used after you have already called HTML. So, I`m assuming this is the reason. Try putting the following at the top just after your opening tag:

error_reporting(E_ALL);

This will probably show the header error.

dc

slimey

8:03 am on May 21, 2006 (gmt 0)

10+ Year Member



i got these reports back

Notice: Undefined variable: strError in /var/virtual/web/w1425/html/contact.php on line 181

Notice: Undefined index: inquiry in /var/virtual/web/w1425/html/contact.php on line 219

those lines are as follows
- 181 - <?php echo $strError;?>

- 219 - <textarea name="inquiry" cols="28" rows="5" id="inquiry"><?php echo $_POST['inquiry']?></textarea>

So whats the solution

dreamcatcher

8:25 am on May 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The undefined variable is because you are not initialising the variable before its use:

$strError = '';

If you look at your source code you might see undefined indexes for the other fields too. You need to check that the variables are set by using isset() [uk.php.net].

<textarea name="inquiry" cols="28" rows="5" id="inquiry"><?php echo (isset($_POST['inquiry'])? $_POST['inquiry'] : '')?></textarea>

dc

slimey

8:28 am on May 21, 2006 (gmt 0)

10+ Year Member



i also got this too sorry,

Warning: Cannot modify header information - headers already sent by (output started at /var/virtual/web/w1425/html/contact.php:12) in /var/virtual/web/w1425/html/contact.php on line 163

line 163 is header( "Location: http://www.example.com.au/thank.htm" );

so what do i do about this problem

[edited by: jatar_k at 3:58 pm (utc) on May 21, 2006]
[edit reason] generalized domain [/edit]

dreamcatcher

9:35 am on May 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, please see my original post.

This must not be used after you have already called HTML

dc

slimey

9:53 am on May 21, 2006 (gmt 0)

10+ Year Member



Ok got you cant use it after i have called HTML, so how will i redirect someone with php after the form has been processed?

dreamcatcher

10:09 am on May 21, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you want to use the header function you`ll have to juggle your code around so that the PHP comes before any HTML. So, if you have include files in place, put the PHP processing before that.

Your other alternatives would be just to have a message saying 'Thanks'.

Good luck.

dc

slimey

10:15 am on May 21, 2006 (gmt 0)

10+ Year Member



Ahh ok cool very simple all fixed.