我希望能够设置以下内容:
1)如果邮件已经存在则返回错误 2)如果成功则返回错误 3)如果错误则返回错误
目前它可以工作,但允许您添加相同的电子邮件地址并发送成功的回复,但需要为现有电子邮件添加一个
$('form').submit(function(){
// check if passwords match; you might want to do more thorough validation
var hasError = false;
var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var emailaddressVal = $("#email").val();
if(emailaddressVal == '') {
$("#email").after('<span class="error">Please enter your email address.</span>');
hasError = true;
}
else if(!emailReg.test(emailaddressVal)) {
$("#email").after('<span class="error">Enter a valid email address.</span>');
hasError = true;
} else if(hasError == false) {
// make ajax post request and store the response in "response" variable
$.post('submit.php', $(this).serialize(), function(response){
// process response here (assume JSON object has boolean property "ok"
if(response.ok==true){
// sweet, it worked!
alert('OK!');
}else{
// handle error
alert('Ooops');
}
}, 'json');
}
// stop the form from being submitted
return false;
});
而php是:
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
$con = mysql_connect("localhost","root",""); //Replace with your actual MySQL DB Username and Password
if (!$con) { die('Could not connect: ' . mysql_error()); }
mysql_select_db("table", $con); //Replace with your MySQL DB Name
$first_name=mysql_real_escape_string($_POST['firstname']);
$last_name=mysql_real_escape_string($_POST['lastname']);
$email=mysql_real_escape_string($_POST['email']);
$sql="INSERT INTO email_list (first_name,last_name,email) VALUES ('$first_name','$last_name','$email')";
if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); }
echo "The form data was successfully added to your database.";
mysql_close($con);
?>
谢谢!