Hi All,
I’m trying to prevent duplicate entires in my DB for a newsletter submissions form. I’m successfully preventing the duplicate entries, but I haven’t been able to revise my jQuery to print the error. I’ve included the PHP just incase I’m missing something there… Any help would be appreciated!
jQuery:
$('#newsletterForm').validate({
submitHandler: function(form) {
jQuery(form).ajaxSubmit(function() {
$('#newsletterForm').html("<div id='success-message'></div>");
$('#success-message').html("<p><strong>Thank you.</strong> <br/>You've been added to our list and will hear from us soon!</p>");
});
}
});
PHP:
if(isset($_POST['submit'])) {
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
if(!isset($hasError)) {
$con = mysql_connect("xxx.xxx.xxx.xxx","user","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("myDB", $con);
$query = "SELECT COUNT(id) AS mycount FROM newsletter WHERE email = '$email'";
$result = mysql_query($query);
$row = mysql_fetch_array($result);
if($row['mycount'] == 0) {
$query = "INSERT INTO newsletter (email,dt) VALUES ('$email',NOW())";
mysql_query($query) or die ("Error writing to database");
} else {
echo "I'm sorry our database already contains that email address. Please use a new email address to continue.";
}
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
mysql_close($con);
}
}