0

我希望能够设置以下内容:

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);
?>

谢谢!

4

4 回答 4

2

只需在添加之前检查数据库中的电子邮件。希望这可以帮助。

    <?php
    $first_name=mysql_real_escape_string($_POST['firstname']); 
    $last_name=mysql_real_escape_string($_POST['lastname']); 
    $email=mysql_real_escape_string($_POST['email']); 
    $sql = "SELECT * FROM email_list WHERE `email`='$email'";
    $res= @mysql_query($sql);
    if(@mysql_num_rows($res)>0)
    {
      echo "Email Already Exists" ;

    }
    else
{
 $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."; 
  }
    ?>
于 2012-12-04T10:21:46.400 回答
2
$sql="SELECT email FROM email_list WHERE email = '$email'";
$result = mysql_query($sql, $con) or die('Error: ' . mysql_error());
if (mysql_num_rows($result) > 0)
{
  // Error - Email already exists
   echo "Error: The email address already exists."; 
} else {
   $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);

我添加了一个检查以查看电子邮件地址是否已经存在,如果存在则输出错误。mysql错误也有错误输出。

如果您需要以某种方式格式化输出,请使用 JSON。但是以上内容应该可以帮助您入门。

于 2012-12-04T10:23:49.537 回答
1

只需在您的 php 文件末尾添加以下行

$value = mysql_insert_id() > 0;

echo json_encode( array('ok'=> $value ) );
于 2012-12-04T10:17:16.273 回答
1

jQuery

$('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=='0'){
             alert('required fields empty');
         }else if(response.ok=='1'){
               alert('email already exists');
         }
else if(response.ok=='2')
{
alert('thankyou for your input');
}
     }, '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']); 

    if(empty($first_name) || empty($last_name) || empty($email) ) {

        echo json_encode( array('ok'=> '0' ) );
        exit();
    }

    $sql="Select * from email_list where email='".$email."' ";
    $sqll=mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
    $data=mysql_fetch_array($sqll);
    if($data['email']) {

        echo json_encode( array('ok'=> '1' ) );
        exit();

    }

    $sql="INSERT INTO email_list (first_name,last_name,email) VALUES ('$first_name','$last_name','$email')"; 
mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
    $value = mysql_insert_id() > 0;
    if($value)
    echo json_encode( array('ok'=> '2' ) );
    mysql_close($con);
    exit();
    ?>
于 2012-12-04T10:26:42.350 回答