/    Sign up×
Community /Pin to ProfileBookmark

contact form error message array help

I’m in the middle of making this contact form and currently I’m trying to solidify the email verification. I currently have it so when the field is blank or an invalid email it shows an error message. If the email is fine, it will send me an email and also add it to a database I’ve created. So far, so good. The only issue I have is IF this email is already added to the database, I don’t want to add it again. So I’ve done this as well, and it works, the only issue is displaying the error message. The original error messages are in an array, and this last error message has to be in lines after the array so I’m not sure how to make the error message show (line 30). You’ll see on line 29 I just added echo error, this was to verify it worked. Any help would be appreciated.

code:

[code]
<?php
// if submitted
if (isset($_POST[‘submit’])) {
// gather POST data
$email = $_POST[’email’];
// make variables are empty
$error = array();
$msg = “”;
$error_msg = “”;
// validate the email entered email address
if (strlen(trim($email)) > 0) {
if (!eregi(“^[_a-z0-9-]+(.[_a-z0-9-]+)*@[a-z0-9-]+(.[a-z0-9-]+)*(.[a-z]{2,3})$”, $email)){
$error[’email’] = “enter a valid email”;
}
} else {
$error[’email’] = “enter your email”;
}
// if no errors with email address
if (empty($error)) {
// Open database connection
$conn = mysql_connect(‘localhost’, ‘DBUSERNAME’, ‘DBPASSWORD’);
mysql_select_db(‘DBNAME’);
// check if email is in the database already
$check_query = mysql_query(“SELECT * FROM emails WHERE email='” . $email . “‘”);
$rows = mysql_num_rows($check_query);
if ($rows > 0)
{
// display error if email is already in the database
// echo ‘ERROR’;
$error[’email’] = “Your email has already been added, try another”;
}
else {
// insert email address into database
$query = “INSERT INTO emails (email) VALUES (‘$email’)”;
mysql_query($query);
// Close connection
mysql_close($conn);
// there were no errors, display thank you message for successfully entering a valid email
$msg = “Success!n”;
// send email, display thank you message
// build the email
$to = ‘[email protected]’;
$subject = “contact form”;
$message = “$email”;
$headers = “From: $email”;
// send the mail using PHPs mail() function
ini_set(sendmail_from,’[email protected]’); // the INI lines are to force the From Address to be used
mail($to, $subject, $message, $headers);
ini_restore(sendmail_from);
}
}
else {
// there were errors, add the error class to the email input
$error_msg = “<p>”;
foreach($error as $field => $error) {
$style[$field] = ” error”;
$error_msg .= “$error”;
}
$error_msg .= “</p>n”;
}
}
// end check for submit button
?>
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN” “http://www.w3.org/TR/html4/strict.dtd”>
<html>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″>
<title>title</title>
</head>
<body>
<?php
// show error messages
if (isset($error_msg) && strlen(trim($error_msg)) > 0) {
echo “$error_msg”;
}
// show success message
if (isset($msg) && strlen(trim($msg)) > 0) {
echo “$msg”;
}
?>
<form method=”post” action=”<?php $_SERVER[‘PHP_SELF’]; ?>”>
<fieldset>
<label for=”email”>Email Address</label>
<input type=”text” name=”email” id=”email” value=”” class=”text<?php echo $style[’email’]; ?>”>
<input type=”submit” name=”submit” value=”Submit” class=”submit”>
</fieldset>
</form>
</body>
</html>
[/code]

Also, I was wondering if with this, do you think I’ll need to worry about spam protection since it’s pretty fool proof? or is it not? thanks again

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@StaceauthorOct 27.2008 — UPDATED CODE:

I now have the error message working if someone submits the same email address, but now the other form validations don't work! Any ideas? new code:

<i>
</i>&lt;?php
if (isset($_POST['submit']))
{
$today = date("l, F j, Y");
$pattern = "#^([a-z0-9-_]+)(.[a-z0-9-_]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*.[a-z]{2,4}$#i";
$email = $_POST['email'];
$error = array();
$msg = "";
$error_msg = "";
if (strlen(trim($email)) &gt; 0)
{
if (!preg_match($pattern, $email))
{
$error['email'] = "invalid e-mail address";
}
if ($email == '[email protected]')
{
$error['email'] = "wrong e-email address, try again";
}
} else
{
$error['email'] = "you didn't enter an e-mail address, try again";
}
include("connect.php");
$check_query = mysql_query("SELECT email FROM emails WHERE email='" . $email . "'");
$rows = mysql_num_rows($check_query);
if ($rows &gt; 0)
{
$error['email'] = "you've already entered your e-mail address";
}
else
{
$query = "INSERT INTO emails (email) VALUES ('$email')";
mysql_query($query);
mysql_close($connect);
}
if (empty($error))
{
$msg = "Success!";
$to = '[email protected]';
$subject = "new email";
$message = "$todayn$email";
$headers = "From: $email";
ini_set(sendmail_from,'[email protected]');
mail($to, $subject, $message, $headers);
ini_restore(sendmail_from);
}
else
{
$error_msg = "&lt;p&gt;";
foreach($error as $field =&gt; $error) {
$style[$field] = " error";
$error_msg .= "$error";
}
$error_msg .= "&lt;/p&gt;n";
}
}
?&gt;
&lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv="content-type" content="text/html; charset=utf-8"&gt;
&lt;title&gt;title&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;?php
// show error messages
if (isset($error_msg) &amp;&amp; strlen(trim($error_msg)) &gt; 0) {
echo "$error_msg";
}
// show success message
if (isset($msg) &amp;&amp; strlen(trim($msg)) &gt; 0) {
echo "$msg";
}
?&gt;
&lt;form method="post" action="&lt;?php $_SERVER['PHP_SELF']; ?&gt;"&gt;
&lt;fieldset&gt;
&lt;label for="email"&gt;Email Address&lt;/label&gt;
&lt;input type="text" name="email" id="email" value="&lt;?php if (isset($_POST['email'])) { echo $_POST['email']; } else { echo '[email protected]'; } ?&gt;" onfocus="this.select()" class="text&lt;?php echo $style['email']; ?&gt;"&gt;
&lt;input type="submit" name="submit" value="Submit" class="submit"&gt;
&lt;/fieldset&gt;
&lt;/form&gt; <br/>
&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@ayveghOct 27.2008 — I'm not sure what you're issue is, but I've cleaned up the code a bit; what seems to be the problem?
[code=php]<?php
if (isset($_POST['submit']))
{
$today = date("l, F j, Y");
$pattern = "#^([a-z0-9-_]+)(.[a-z0-9-_]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*.[a-z]{2,4}$#i";
$email = $_POST['email'];
$error = array();
$msg = "";
$error_msg = "";

if (strlen(trim($email)) > 0)
{
if (!preg_match($pattern, $email))
{
$error['email'] = "Invalid e-mail address.";
}
if ($email == '[email protected]')
{
$error['email'] = "Invalid e-mail address.";
}
}
else
{
$error['email'] = "No e-mail address was entered.";
}

include("connect.php");

$check_query = mysql_query("SELECT email FROM emails WHERE email='" . $email . "'");
$rows = mysql_num_rows($check_query);

if ($rows > 0)
{
$error['email'] = "The address you entered has already been used.";
}
else
{
$query = "INSERT INTO emails (email) VALUES ('$email')";

mysql_query($query);
mysql_close($connect);
}

if (empty($error))
{
$msg = "Success!";
$to = '[email protected]';
$subject = "new email";
$message = "$todayn$email";
$headers = "From: $email";

ini_set(sendmail_from,'[email protected]');
mail($to, $subject, $message, $headers);
ini_restore(sendmail_from);
}
else
{
$error_msg = "<p>";

foreach ($error as $field => $error)
{
$style[$field] = " error";
$error_msg .= "$error";
}

$error_msg .= "</p>n";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>title</title>
</head>
<body>
<?php
// show error messages
if (isset($error_msg) && strlen(trim($error_msg)) > 0)
{
echo "$error_msg";
}

// show success message
if (isset($msg) && strlen(trim($msg)) > 0)
{
echo "$msg";
}
?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<fieldset>
<label for="email">Email Address</label>
<input type="text" name="email" id="email" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } else { echo '[email protected]'; } ?>" onfocus="this.select()" class="text<?php echo $style['email']; ?>">
<input type="submit" name="submit" value="Submit" class="submit">
</fieldset>
</form>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@StaceauthorOct 27.2008 — Thanks for helping me clean up the code, but it's still not working properly. I'll explain:

When someone leaves the form blank, instead of getting an error message it gives the success method and inserts a blank field into the database.

Likewise, everything that's entered gets entered into the database, when only successfully valid emails should.

Here's the revised code:

[code=php]
<?php
if (isset($_POST['submit']))
{
$today = date("l, F j, Y");
$pattern = "#^([a-z0-9-_]+)(.[a-z0-9-_]+)*@([a-z0-9-]+)(.[a-z0-9-]+)*.[a-z]{2,4}$#i";
$email = $_POST['email'];
$error = array();
$msg = "";
$error_msg = "";
if (strlen(trim($email)) > 0)
{
if (!preg_match($pattern, $email))
{
$error['email'] = "invalid email address, try again";
}
if ($email == '[email protected]')
{
$error['email'] = "wrong address, try again";
}
}
else
{
$error['email'] = "you didn't enter an address, try again";
}
include("connect.php");
$check_query = mysql_query("SELECT email FROM emails WHERE email='" . $email . "'");
$rows = mysql_num_rows($check_query);
if ($rows > 0)
{
$error['email'] = "you've already signed up";
}
else
{
$query = "INSERT INTO emails (email) VALUES ('$email')";
mysql_query($query);
mysql_close($connect);
}
if (empty($error))
{
$msg = "Sucess!n";
$to = '[email protected]';
$subject = "new e-mail";
$message = "$todayn$email";
$headers = "From: $email";
ini_set(sendmail_from,'[email protected]');
mail($to, $subject, $message, $headers);
ini_restore(sendmail_from);
}
else
{
$error_msg = "<p>";
foreach($error as $field => $error) {
$style[$field] = " error";
$error_msg .= "$error";
}
$error_msg .= "</p>n";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>title</title>
</head>
<body>
<?php
// show error messages
if (isset($error_msg) && strlen(trim($error_msg)) > 0) {
echo "$error_msg";
}
// show success message
if (isset($msg) && strlen(trim($msg)) > 0) {
echo "$msg";
}
?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<fieldset>
<label for="email">Email Address</label>
<input type="text" name="email" id="email" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } else { echo '[email protected]'; } ?>" onfocus="this.select()" class="text<?php echo $style['email']; ?>">
<input type="submit" name="submit" value="Submit" class="submit">
</fieldset>
</form>

</body>
</html>
[/code]
Copy linkTweet thisAlerts:
@StaceauthorOct 27.2008 — edited code now working with one small error ([email protected] gets added to the database) but I'm not going to fret over it..

code for whoever needs it:
[code=php]<?php
include("connect.php");

if (isset($_POST['submit'])){
$today = date("l, F j, Y");
$pattern = "#^[a-z0-9]+([_\.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\.[a-z]{2,}$#i";
$email = mysql_real_escape_string($_POST['email']);
$error = array();
$msg = "";
$error_msg = "";

if (strlen(trim($email)) > 0) {

if (!preg_match($pattern, $email)){
$error['email'] = "invalid e-mail address";
}else{
$check_query = mysql_query("SELECT email FROM emails WHERE email='" . $email . "'");
$rows = mysql_num_rows($check_query);

if ($rows > 0){
$error['email'] = "you've already entered your e-mail address";

}else{
$query = "INSERT INTO emails (email) VALUES ('$email')";
mysql_query($query);
mysql_close($connect);
}
}

if ($email == '[email protected]'){
$error['email'] = "wrong e-email address, try again";
}

} else{
$error['email'] = "you didn't enter an e-mail address, try again";
}



if (empty($error)){
$msg = "Success!";
$to = '[email protected]';
$subject = "new email";
$message = "$todayn$email";
$headers = "From: $email";
ini_set(sendmail_from,'[email protected]');
mail($to, $subject, $message, $headers);
ini_restore(sendmail_from);

}else{
$error_msg = "<p>";
foreach($error as $field => $error) {
$style[$field] = " error";
$error_msg .= "$error";
}

$error_msg .= "</p>n";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>title</title>
</head>
<body>
<?php
// show error messages
if (isset($error_msg) && strlen(trim($error_msg)) > 0) {
echo "$error_msg";
}
// show success message
if (isset($msg) && strlen(trim($msg)) > 0) {
echo "$msg";
}
?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<fieldset>
<label for="email">Email Address</label>
<input type="text" name="email" id="email" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } else { echo '[email protected]'; } ?>" onfocus="this.select()" class="text<?php echo $style['email']; ?>">
<input type="submit" name="submit" value="Submit" class="submit">
</fieldset>
</form>

</body>
</html>[/code]
Copy linkTweet thisAlerts:
@ayveghOct 27.2008 — I'm glad to hear you got it working. Let me know if you need help with anything else. ?

ayvegh

EDIT:

Try this. It should block "[email protected]" from being marked as valid:

[code=php]<?php
include("connect.php");

if (isset($_POST['submit']))
{
$today = date("l, F j, Y");
$pattern = "#^[a-z0-9]+([_\.-][a-z0-9]+)*@([a-z0-9]+([.-][a-z0-9]+)*)+\.[a-z]{2,}$#i";
$email = mysql_real_escape_string($_POST['email']);
$error = array();
$msg = "";
$error_msg = "";

if (strlen(trim($email)) > 0)
{
if (!preg_match($pattern, $email) OR $email == '[email protected]')
{
$error['email'] = "invalid e-mail address";
}
else
{
$check_query = mysql_query("SELECT email FROM emails WHERE email='" . $email . "'");
$rows = mysql_num_rows($check_query);

if ($rows > 0)
{
$error['email'] = "you've already entered your e-mail address";
}
else
{
$query = "INSERT INTO emails (email) VALUES ('$email')";

mysql_query($query);
mysql_close($connect);
}
}
}
else
{
$error['email'] = "you didn't enter an e-mail address, try again";
}

if (empty($error))
{
$msg = "Success!";
$to = '[email protected]';
$subject = "new email";
$message = "$todayn$email";
$headers = "From: $email";

ini_set(sendmail_from,'[email protected]');
mail($to, $subject, $message, $headers);
ini_restore(sendmail_from);
}
else
{
$error_msg = "<p>";

foreach($error as $field => $error)
{
$style[$field] = " error";
$error_msg .= "$error";
}

$error_msg .= "</p>n";
}
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>title</title>
</head>
<body>
<?php
// show error messages
if (isset($error_msg) && strlen(trim($error_msg)) > 0)
{
echo "$error_msg";
}

// show success message
if (isset($msg) && strlen(trim($msg)) > 0)
{
echo "$msg";
}
?>
<form method="post" action="<?php $_SERVER['PHP_SELF']; ?>">
<fieldset>
<label for="email">Email Address</label>
<input type="text" name="email" id="email" value="<?php if (isset($_POST['email'])) { echo $_POST['email']; } else { echo '[email protected]'; } ?>" onfocus="this.select()" class="text<?php echo $style['email']; ?>">
<input type="submit" name="submit" value="Submit" class="submit">
</fieldset>
</form>

</body>
</html>[/code]
Copy linkTweet thisAlerts:
@StaceauthorOct 27.2008 — I'm glad to hear you got it working. Let me know if you need help with anything else. ?

ayvegh[/QUOTE]


Thanks, actually there is one thing. At first I thought it wouldn't be a big concern but it is.

When a user leaves in the value of the input: '[email protected]' and hits submit, the message should be 'wrong address, try again' - and that does happen, but it still adds the name to database. So then if another user does the same thing, they get the error message: 'email already added, try again' - is there a way to prevent the '[email protected]' by going into the database?

Thanks for all your help.
Copy linkTweet thisAlerts:
@ayveghOct 28.2008 — Check again - I was editing my previous post while you were responding. ?
Copy linkTweet thisAlerts:
@StaceauthorOct 28.2008 — PERFECT!! Thank you so much ??
×

Success!

Help @Stace 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.17,
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: @nearjob,
tipped: article
amount: 1000 SATS,

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

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