单个PHP文件中的多个AJAX调用

时间:2017-07-06 22:02:56

标签: php jquery ajax web

我正在尝试通过AJAX验证用户名和电子邮件,并将数据发送到单个PHP文件...我的代码是

HTML code - >

* {
  box-sizing: border-box
}

body {
  font-family: Verdana, sans-serif;
  margin: 0;
  padding: 0;
}

.imageSlide-container {
  position: relative;
  margin: auto;
}

.imageSlide {
  position: absolute;
  width: 100%;
  height: auto;
  opacity: 0;
  z-index: 1;
  -webkit-transition: opacity 1.0s;
  -moz-transition: opacity 1.0s;
  -o-transition: opacity 1.0s;
  transition: opacity 1.0s;
}

.showing {
  position: static;
  opacity: 1;
  z-index: 2;
  transform: translateZ(0);
}

.prev,
.next {
  position: absolute;
  top: 50%;
  z-index: 3;
  width: auto;
  cursor: pointer;
  margin-top: -22px;
  padding: 15px;
  color: red;
  font-size: 18px;
  font-weight: bold;
  transition: 0.6s ease;
  border-radius: 0 3px 3px 0;
}

.next {
  right: 0;
  border-radius: 3px 0 0 3px;
}

.prev:hover,
.next:hover {
  background-color: rgba(0, 0, 0, 0.8)
}

@media only screen and (max-width: 300px) {
  .prev,
  .next,
  .text {
    font-size: 11px
  }
}

JavaScript代码 - >

<div class="imageSlide-container">
  <img class="imageSlide showing" src="https://www.w3schools.com/howto/img_nature_wide.jpg">
  <img class="imageSlide" src="https://www.w3schools.com/howto/img_fjords_wide.jpg">
  <img class="imageSlide" src="https://www.w3schools.com/howto/img_mountains_wide.jpg">
  <img class="imageSlide" src="https://www.w3schools.com/howto/img_fjords_wide.jpg">

  <span class="prev" onclick="plusSlide-button(-1)">&#10094;</span>
  <span class="next" onclick="plusSlide-button(1)">&#10095;</span>
</div>
<a>test text</a>
<a>test text</a>
<a>test text</a>

PHP代码 - &gt;

<input type="text" name="user_name" id="user_name" onBlur="userChk()" class="form-control" placeholder="Username" />
    <input type="email" name="email" id="email" onBlur="emailchk()" class="form-control" placeholder="Email">

我不知道哪里是个蠢蛋...我是网络发展的新手。

1 个答案:

答案 0 :(得分:1)

您需要添加isset()否则会抛出未定义的索引错误。因为假设当ajax调用名称检查时将调用电子邮件变量不会设置所以它将抛出错误未定义的索引电子邮件。

if($_POST['name'])不会检查未定义的变量。

include('../components/connectdb.php');
if(isset($_POST['name'])) {
    $user_name     = strip_tags($_POST['name']);
        $uselect_sql= "SELECT user_name FROM users WHERE user_name='{$user_name}'";

       $uselect_query = mysqli_query($conn, $uselect_sql);

      $urow = mysqli_num_rows($uselect_query);

      if($urow > 0){
      echo "<div class='alert alert-danger alert-dismissable fade in'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a>User Name Already Exist</div>";
    }
    else
    {
      echo "";
    }
    }


    else if(isset($_POST['email'])){
      $email = ($_POST['email']);
       $eselect_sql= "SELECT email FROM users WHERE email='{$email}'";

       $eselect_query = mysqli_query($conn, $eselect_sql);

      $erow = mysqli_num_rows($eselect_query);
      if($erow > 0){
      echo "<div class='alert alert-danger alert-dismissable fade in'><a href='#' class='close' data-dismiss='alert' aria-label='close'>&times;</a><strong>Warning!</strong>Email Already Exist</div>";
    }
    else
    {
      echo "";
    }
    }