mail form error message display on same page as form

the mail() function

Moderators: macek, egami, gesf

rubi
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Jun 23, 2011 10:04 pm

mail form error message display on same page as form

Postby rubi » Thu Jun 23, 2011 10:15 pm

I am new to this.

goal 1: contact form that direct to separate thankyou.php file when submitted successfully.

goal 2: error message that displays on same page above field.

I have figured out how to get the thankyou page to work but it broke my error messages. If there is an error it still goes to the thankyou page.

If I go with the original code that does not direct to separate thankyou page, it displays the thankyou and error messages on a blank page with just the one line of text.


here is the original code that works to send the form but not for the above goals.

___________

code for mail.php
____________

<?php
if(!$_POST) exit;
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];

if(trim($name) == '') {
echo '<div class="contact_error">Please enter your name.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="contact_error">Please enter a valid email address.</div>';
exit();
} else if(trim($phone) == '') {
echo '<div class="contact_error">Please enter a valid contact number.</div>';
exit();
} else if(!is_numeric($phone)) {
echo '<div class="contact_error">Your phone number should only contain numbers.</div>';
exit();
} else if(!verifyEmail($email)) {
echo '<div class="contact_error">Your email address is invalid. Please enter a valid one. </div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="contact_error">Please enter a message.</div>';
exit();
}
if($error == '') {
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}

$address = "me@myemail.com";
$email_subject = 'New Message: From ' . $name;
$email_body = "From: $name \n\n";
$email_reply = "Email: $email \nPhone: $phone";
$email_content = "\n\nMessage: \r\n\n$comments\r\n\n";
$message = $email_body . $email_reply . $email_content;

if(mail($address, $email_subject, $message, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n")) {
echo "<fieldset>";
echo "<div id='email_sent'>";
echo "<h3>Thank You!</h3>";
echo "<p>We've received your email and will be in touch soon</p>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
}
function verifyEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
?>

___________________________________________________________________________

contact form code
__________________________

<form method="post" action="<?php bloginfo('url'); ?>/wp-content/themes/fullscreen_child/mail.php" name="contactform" id="contactform">
<fieldset>
<input name="name" type="text" id="name" size="30" value="" />
<label for=name accesskey=U>Name</label>
<input name="email" type="text" id="email" size="30" value="" />
<label for=email accesskey=E>Email</label>
<input name="phone" type="text" id="phone" size="30" value="" />
<label for=phone accesskey=P>Phone</label>
<textarea name="comments" cols="40" rows="15" id="comments"></textarea>
<input type="submit" class="submit" id="submit" value="" />
</fieldset>
</form>
_________________________________________________________________________________________

New code that directs to separate custom designed thankyou url but breaks error messaging

________________________________________

<?php
header("Location: http://www.url of thankyou page"); /* Redirect browser */
/* Make sure that code below does not get executed when we redirect. */
exit;


if(!$_POST) exit;
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$comments = $_POST['comments'];

if(trim($name) == '') {
echo '<div class="contact_error">Please enter your name.</div>';
exit();
} else if(trim($email) == '') {
echo '<div class="contact_error">Please enter a valid email address.</div>';
exit();
} else if(trim($phone) == '') {
echo '<div class="contact_error">Please enter a valid contact number.</div>';
exit();
} else if(!is_numeric($phone)) {
echo '<div class="contact_error">Your phone number should only contain numbers.</div>';
exit();
} else if(!verifyEmail($email)) {
echo '<div class="contact_error">Your email address is invalid. Please enter a valid one. </div>';
exit();
}
if(trim($comments) == '') {
echo '<div class="contact_error">Please enter a message.</div>';
exit();
}
if($error == '') {
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}

$address = "me@myemail.com";
$email_subject = 'New Message: From ' . $name;
$email_body = "From: $name \n\n";
$email_reply = "Email: $email \nPhone: $phone";
$email_content = "\n\nMessage: \r\n\n$comments\r\n\n";
$message = $email_body . $email_reply . $email_content;

if(mail($address, $email_subject, $message, "From: $email\r\nReply-To: $email\r\nReturn-Path: $email\r\n")) {
echo "<fieldset>";
echo "<div id='email_sent'>";
echo "Thank You!" . " -" . "<a href='http://www.url to go to home page' style='text-decoration:none;color:#ff0099;'> Return Home</a>";
echo "</div>";
echo "</fieldset>";
} else {
echo 'ERROR!';
}
}
function verifyEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+@((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
?>

I truly have no actual clue about php and have spent all day researching and trying to learn about it so any advice would be wonderful!

Return to “PHP coding => Mail”

Who is online

Users browsing this forum: No registered users and 1 guest