为什么此ajax在注册表中不会阅读php

时间:2018-07-17 10:49:00

标签: php jquery ajax

我对ajax注册表有疑问。我尝试了很多选择,但是找不到错误的地方。问题出在电子邮件上。如果存在电子邮件(check-email.php),则Ajax无法读取。其他一切正常。我用test@ok.com进行了测试。

table `users`

| id | email       |
|  1 | test@ok.com |

HTML

<script src="https://static.zrcdn.net/js/fw/jquery/1.7.2/jquery-1.7.2.min.js"></script>
<script>
  function checkStepOne() {
    jQuery("#owner_email").parents("li").find("label.error").hide();
    jQuery("#owner_password").parents("li").find("label.error").hide();
    jQuery("#owner_email").parents("li").find("span.check-1-r").removeClass('chkValid');
    jQuery("#owner_password").parents("li").find("span.check-2-r").removeClass('chkValid');

    var owner_email = jQuery("#owner_email").val();
    var owner_password = jQuery("#owner_password").val();

    var chkemail = 1;
    var chkpass = 1;
    var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

    $.ajax({
      type: "POST",
      url: "check-email.php",
      data: {
        owner_email,
      },
      success: function(responseEmail) {
        if (responseEmail == 0) {
          jQuery("#owner_email").parents("li").find("#e_exist").show();
          chkemail = 0;
        } else {
          chkemail = 1;
        }
      }
    });

    if (owner_email == "" || reg.test(owner_email) == false) {
      chkemail = 0;
    }

    if (chkemail == 1) {
      jQuery("#owner_email").parents("li").find("span.check-1-r").addClass('chkValid');
    } else {
      jQuery("#owner_email").parents("li").find("#e_avaib").show();
    }

    if (owner_password.length < 5 || owner_password == "") {
      chkpass = 0;
    }

    if (chkpass == 1) {
      jQuery("#owner_password").parents("li").find("span.check-2-r").addClass('chkValid');
    } else {
      jQuery("#owner_password").parents("li").find("label.error").show();
    }

    if (chkpass == 1 && chkemail == 1) {
      jQuery(".step-1").hide();
      jQuery(".step-2").addClass('chkValid').show();
    }
    return false;
  }
</script>


<form method="post" id="frmEmailSignup" onsubmit="return submitResform();">
  <ul>
    <li>
      <label class="side-label" for="owner[email]">Email</label>
      <input type="email" class="email required" id="owner_email" name="owner[email]" placeholder="Email" tabindex="0" onkeyup="return forceLower(this);" style="margin-bottom: 6px;"/>
      <label class="error" id="e_exist" style="margin-top: -9px;">Email exist.</label>
      <label class="error" id="e_avaib" style="margin-top: -9px;">Wrong email.</label>
      <span class="check-1-r"></span>
    </li>
    <li class="password-row">
      <label class="side-label" for="owner[password]">Password</label>
      <input type="password" class="password required" id="owner_password" name="owner[password]" placeholder="Password" tabindex="0"/>
      <label class="error">Wrong password.</label>
      <span class="check-2-r"></span>
    </li>
    <li class="full">
      <button type="button" class="button cta btn-disabled next next-one" tabindex="0" data-plan="register" onclick="checkStepOne();">Sign up</button>
    </li>
  </ul>
</form>

check-email.php

<?php
$email = $_POST['owner[email]'];
$query = mysql_query("SELECT email FROM users WHERE email = '$email'");
if (mysql_num_rows($query) == 1) {
  echo '0';
} else {
  echo '1';
}
?>

2 个答案:

答案 0 :(得分:1)

您应该将数据作为

传递给AJAX
data: {
    owner_email: owner_email
}

,在PHP中将其获取为

$_POST['owner_email']

答案 1 :(得分:0)

您在ajax中传递数据是错误的。你已经用过了。

数据:{owner_email}

应该是这样

数据:{email:owner_email}

在check-email.php文件中获得这样的电子邮件

$ email = $ _POST ['email'];