/    Sign up×
Community /Pin to ProfileBookmark

Php Contact Form – Displaying errors

Hello Everyone,
I have two php files that I use to create a contact form. One is my actual file that carries the html in it and the other is the functionality of the actual contact form.

I have the contact form error checked rather extensively with regular expressions, but I need help outputting my error messages to the proper place.

I would like to output the message “success!” or any of my error messages that are stored in the variable $error_message underneath my submit button in the first set of code I have provided you with. Currently when I press submit, I get taken to my php file and it displays my error messages or success messages in plain text. I would like to be able to press submit and then have the message displayed right underneath the submit button based upon if it there was an error or if it was a success.

My html code for the form is as follows:
NOTE: the html uses some javascript for a drop down box that is unnecessary to post here.

[CODE]<a id=”close” onclick=”toggle_visibility(‘contact’);”>Like this listing? &nbsp;&nbsp; <span style=”background:#1e4877;padding: 5px 10px;-moz-border-radius: 3px;border-radius: 3px;text-shadow: 1px 1px #08203b;”>Contact Me</span></a>
<div id=”contact” style=”display:none;”><span style=”background: #111;

filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=’#000′, endColorstr=’#333′);
background: -webkit-gradient(linear, left top, left bottom, from(#000), to(#333));
background: -moz-linear-gradient(top, #000, #333);color:white;font-family:arial;font-size:12px;position:fixed;
top:40px;
right:0px;padding-top:30px;padding-left:30px;padding-bottom:10px;”>
<form name=”contactform” method=”post” action=”contact_function.php”>
<table width=”450px” style=”font: 13px/1.8 “Helvetica Neue”, Helvetica, Arial, sans-serif;
color: #fff;”>
<tr>
<td valign=”top”>
<label for=”whole_name” style=”color: white;font-size:14px;line-height:2;” >Name *</label>
</td>
<td valign=”top”>
<input type=”text” style=”font: 13px ‘Helvetica Neue’, Helvetica, Arial !important;
color: #666;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
padding: 8px;
border: 1px solid #000;
background: white;” name=”whole_name” maxlength=”50″ size=”30″>
</td>
</tr>
<tr>
<td valign=”top”>
<label for=”email” style=”color: white;font-size:14px;line-height:2;”>Email Address *</label>
</td>
<td valign=”top”>
<input type=”text” style=”font: 13px ‘Helvetica Neue’, Helvetica, Arial !important;
color: #666;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
padding: 8px;
border: 1px solid #000;
background: white;” name=”email” maxlength=”80″ size=”30″>
</td>
</tr>
<tr>
<td valign=”top”>
<label for=”telephone” style=”color: white;font-size:14px;line-height:2;”>Phone Number</label>
</td>
<td valign=”top”>
<input type=”text” style=”font: 13px ‘Helvetica Neue’, Helvetica, Arial !important;
color: #666;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
padding: 8px;
border: 1px solid #000;
background: white;” name=”telephone” maxlength=”30″ size=”30″>
</td>
</tr>
<tr>
<td valign=”top”>
<label for=”message” style=”color: white;font-size:14px;line-height:2;” >Message *</label>
</td>
<td valign=”top”>
<textarea name=”message” style=”font: 13px ‘Helvetica Neue’, Helvetica, Arial !important;
color: #666;
-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;
padding: 8px;
border: 1px solid #000;
background: white;” maxlength=”1000″ cols=”30″ rows=”6″></textarea>
</td>
</tr>
<tr>
<td colspan=”2″ style=”text-align:center”>
<input type=”submit” style=”background:
#3494DE;
filter: progid:DXImageTransform.Microsoft.gradient(startColorstr=’#3494de’, endColorstr=’#1f75b8′);
background: -webkit-gradient(linear, left top, left bottom, from(
#3494DE), to(
#1F75B8));
background: -moz-linear-gradient(top,
#3494DE,
#1F75B8);
border: 1px solid
#01599D;
color:
white !important;
text-shadow: 1px 1px 0px
#1073C7);
box-shadow: 1px 1px 1px 0px
#1073C7);
-webkit-box-shadow: 1px 1px 1px 0px
rgba(0, 0, 0, 0.1);
-moz-box-shadow: 1px 1px 1px 0px rgba(0, 0, 0, 0.1);margin-right: 90px;padding:8px 12px;-webkit-border-radius: 2px;
-moz-border-radius: 2px;
border-radius: 2px;” value=”Submit”>
</td>
</tr>
</table>
</form>
</span></div>
</div>[/CODE]

My PHP file is as follows:

[CODE]<?php
include (“globalconfig.php”);
if(isset($_POST[’email’])) {

// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = “[email protected]”;
$email_subject = “Pro Package – Search Contact”;

function died($error) {
// your error code can go here
echo “<font style=’color: #222;font-family:arial, sans-serif;font-size:16px;’>There were some errors with the form you submitted.</font><br /><br />”;
echo $error.”<br />”;
echo “<font color=’black’>Please fix these.</font>”;
die();
}

// validation expected data exists
if(!isset($_POST[‘whole_name’]) ||
!isset($_POST[’email’]) ||
!isset($_POST[‘telephone’]) ||
!isset($_POST[‘message’])) {
died(‘There were some errors with the form you submitted.’);
}

$whole_name = $_POST[‘whole_name’]; // required
$email_from = $_POST[’email’]; // required
$telephone = $_POST[‘telephone’]; // required
$message = $_POST[‘message’]; // required

$error_message = “”;

//Name Error Checking
$name_exp = “/^([a-zA-Z’ ]+)$/”;
if((strlen($whole_name) < 2) || (!preg_match($name_exp,$whole_name))){
$error_message .= ‘<font style=”color: red;font-family:arial, sans-serif;font-size:13px;”>The name you entered does not appear to be valid.<font><br />’;
}
//Email error checking
$email_exp = “/^[A-Za-z0-9._&#37;-]+@[A-Za-z0-9.-]+.[A-Za-z]{2,4}$/”;
if(!preg_match($email_exp,$email_from)) {
$error_message .= ‘The email address you entered does not appear to be valid.<br />’;
}
//Phone Number Error Checking
$phone_exp = “/^(1?(-?d{3})-?)?(d{3})(-?d{4})$/”;
if(strlen($telephone) < 10 || !preg_match($phone_exp,$telephone)){
$error_message .= ‘The phone number you entered is invalid. <br />’;
}
//Comment Error Checking
if(strlen($message) < 2) {
$error_message .= ‘The comment you entered do not appear to be valid.<br />’;
}
//Spam Catcher
$spam_exp = “/(&#217;|&#218;|&#219;|&#220;|&#249;|&#250;|&#251;|&#252;|&#181;|&amp;#085;|&amp;#117;|&amp;#181;|&amp;micro;|&amp;#217;|&amp;#218;|&amp;#219;|&amp;#220;|&amp;Ugrave;|&amp;Uacute;|&amp;Ucirc;|&amp;Uuml;|&amp;#249;|&amp;#250;|&amp;#251;&amp;#252;|&amp;ugrave;|&amp;uacute;|&amp;ucirc;|&amp;uuml;)/”;
if(preg_match($spam_exp,$whole_name) || preg_match($spam_exp,$email_from) || preg_match($spam_exp,$telephone) || preg_match($spam_exp,$message)){
$error_message .= “Please do not spam.<br />”;
}

//Catching if there was an error, if so die.
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = “Contact form information is listed below.nn”;

function clean_string($string) {
$bad = array(“content-type”,”bcc:”,”to:”,”cc:”,”href”);
return str_replace($bad,””,$string);
}

$email_message .= “Name: “.clean_string($whole_name).”n”;
$email_message .= “Email: “.clean_string($email_from).”n”;
$email_message .= “Phone: “.clean_string($telephone).”n”;
$email_message .= “Message: “.clean_string($message).”n”;

$text_message = “$first_n $last_n has contacted you. Please call them @ $telephone.”;

// create email headers
$headers = ‘From: ‘.$email_from.”rn”.
‘Reply-To: ‘.$email_from.”rn” .
‘X-Mailer: PHP/’ . phpversion();
@mail($email_to, $email_subject, $email_message, $headers);
@mail($textemail, $email_subject, $text_message);
?>

<!– include your own success html here –>
SUCCESS!
<meta http-equiv=”REFRESH” content=”2;url=http://<?php echo $urlname;?>/search.php”>

<?php
}
?>
[/CODE]

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@holyhttpMay 24.2012 — You can either combines the 2 files into a single PHP file or includes - require_once(.....) - your form processing file in the contact.php file

You need to restructure your form processing code not to invoque

died('There were some errors with the form you submitted.');

but rather set

$errmsge='There were some errors with the form you submitted.';

All you need to check is if the $errmsge varaible is an empty string or not to decide whether to process the form info or not.

At the bottom of your form processing, if all goes well you can also set:

$errmsge='SUCCESS';


Now next to your submit button, just add:

<?php echo $errmsge; ?>
×

Success!

Help @amorte spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.2,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,
)...