ajax在php中调用并验证电子邮件

时间:2016-07-19 14:45:11

标签: javascript php jquery html ajax

我正在尝试将表单操作“转换”为ajax调用。 我的表格:

 <!-- <form method="POST" onSubmit="return doSubmitLogic()" action="action.scripts.php" >
<input type="hidden" name="actiune" value="login" />
<div>
    <label> Email </label>
    <input type="email" name = "email" id="email" /><span id="emailErr"></span >
</div>
<div>
    <label> Password </label>
    <input type="password" name = "password" id="password" /> <span  id="passErr"></span >
</div>

<div>
    <input id ="submitBtn" type="submit" name="button" value="Send"/>
</div>

我试图用我的ajax做什么:

$(document).ready(function(){
$("#submitBtn").click(function(e){

    e.preventDefault();  

    $.ajax({
        type: "POST",
        url: "functions.php",
        data: {
               Email: $("#email").val(),
               Password: $("#password").val(),
              },

        success: function(result){   
            alert(result);
        },
        error: function (error){
            alert("Error");
        }

   });
 });
 });

此外,我的一段代码“functions.php”由许多人组成,检查操作是否具有特定值,如果是,则执行某些操作。所以:

  if ($_POST['actiune']==="login") {
    $email = $_POST['email'];
    $password = $_POST['password'];
    $encript_pass = md5($password);

    $query = "select * from user where email='$email' and password='$encript_pass'"; 
    $result = mysql_query($query) or die ("Error in query: $query " . mysql_error()); 
    $row = mysql_fetch_array($result); 



    if (!filter_var($email, FILTER_VALIDATE_EMAIL) === false) {
        echo("$email is a valid email address");
    } else {
        echo("$email is not a valid email address");
    }   
 }

当我运行它时,它会给我一个警告说错误。任何消化?

2 个答案:

答案 0 :(得分:2)

只需用此替换您的代码,请注意电子邮件和电子邮件中的“活动”。密码

    $(document).ready(function(){
$("#submitBtn").click(function(e){

    e.preventDefault();  

    $.ajax({
        type: "POST",
        url: "functions.php",
        data: {
               Email: $("#email").val(),
               Password: $("#password").val(),
               actiune: $('input[name="actiune"]').val()
              },

        success: function(result){   
            alert(result);
        },
        error: function (error){
            alert("Error");
        }

   });
 });
 });

答案 1 :(得分:1)

您也需要将actiune与您的数据一起传递。

 data: {
      email: $("#email").val(),
      password: $("#password").val(),
      actiune: 'login'
 },

我还删除了您传递的字段中的大写字母,因为您需要passwordemail而不是PasswordEmail