使用ajax检查数据库中是否有多个字段

时间:2017-03-07 04:34:31

标签: javascript php jquery ajax

如果字段在数据库中并且回显" OK"如何检查多个字段?如果没有那么"无效"

这是我的html代码

var claims = new[]
{
  new Claim("UserID", user.ID.ToString()),
  new Claim(ClaimTypes.Role, "pioneer")
};

var principal = new ClaimsPrincipal(
                  new ClaimsIdentity(
                    claims, CookieAuthenticationDefaults.AuthenticationScheme));

await HttpContext.Authentication.SignInAsync("Cookies", principal);

这是 checkdata.php

<html>
<head>
<script src="Bootstrap/js/jquery.min.js"></script>
<script src="Bootstrap/js/bootstrap.min.js"></script>
<script src="Bootstrap/js/npm.js"></script>
<script type="text/javascript">

function checkname()
{
 var name=document.getElementById( "UserName" ).value;

 if(name)
 {
 $.ajax({
  type: 'post',
  url: 'checkdata.php',
  data: {
  user_name:name,
 },
  success: function (response) {
   $( '#name_status' ).html(response);
   if(response=="OK")   
   {
    return true;    
   }
    else
   {
    return false;   
   }
  }   
  });
 }
 else
 {
  $( '#name_status' ).html("");
  return false;
 }
}

function checkemail()
{
 var email=document.getElementById( "UserEmail" ).value;

 if(email)
 {
  $.ajax({
  type: 'post',
  url: 'checkdata.php',
  data: {
  user_email:email,
  },
  success: function (response) {
   $( '#email_status' ).html(response);
   if(response=="OK")   
   {
    return true;    
   }
   else
   {
    return false;   
   }
  }
  });
 }
 else
 {
  $( '#email_status' ).html("");
  return false;
 }
}

 function checkall()
{
 var namehtml=document.getElementById("name_status").innerHTML;
 var emailhtml=document.getElementById("email_status").innerHTML;

 if((namehtml && emailhtml)=='OK')
 {
 return true;
 }
 else
 {
 return false;


}
}

</script>
</head>
<body>

<form method="POST" action="insertdata.php" onsubmit="return checkall();">
 <input type="text" name="username" id="UserName" onkeyup="checkname();">
 <span id="name_status"></span>
 <br>
 <input type="text" name="useremail" id="UserEmail" onkeyup="checkemail();">
 <span id="email_status"></span>
 <br>
 <input type="password" name="userpass" id="UserPassword">
 <br>
 <button type="submit" name="submit_form" value="Submit">Submit</button>
</form>

</body>
</html>

为了更清楚,我想要实现的是数据库中的名称和电子邮件的值等于输入的值。它只会显示一个&#34; OK&#34;。刚开始使用ajax。请帮忙。 TY

5 个答案:

答案 0 :(得分:3)

当您在用户输入时测试数据时,您应该单独检查用户名和电子邮件。为此你的ajax函数看起来没问题,但你的php代码应该被修改。

//Your database code

<?php
if(isset($_POST['user_name']) && isset($_POST['user_email'])){
 //Check if username and email both is exist

 $name=$_POST['user_name']; 
 $emailId=$_POST['user_email']; 

 $checkdata="SELECT name,loginid FROM user WHERE name='$name' AND loginid='$emailId';";

 $query=mysqli_query($conn, $checkdata);

 if(mysqli_num_rows($query) > 0)
 {
  echo "User Name And Email Already Exist";
 }
 else
 {
  echo "OK";
 }
 exit();
} else if(isset($_POST['user_name'])){
 //Check if username is exist
 $name=$_POST['user_name']; 

 $checkdata="SELECT name,loginid FROM user WHERE name='$name';";

 $query=mysqli_query($conn, $checkdata);

 if(mysqli_num_rows($query) > 0)
 {
  echo "User Name Already Exist";
 }
 else
 {
  echo "OK";
 }
 exit();
} else if(isset($_POST['user_email'])){
 //Check if email is exist
 $emailId=$_POST['user_email']; 
 $checkdata="SELECT name,loginid FROM user WHERE loginid='$emailId';";

 $query=mysqli_query($conn, $checkdata);

 if(mysqli_num_rows($query) > 0)
 {
  echo "User Email Already Exist";
 }
 else
 {
  echo "OK";
 }
 exit();
} else {
  echo 'Not valid test data provided';
}
?>

答案 1 :(得分:2)

如果您使用的查询是正确的,则在文件checkdata.php中使用return而不是echo

<?php
$servername = "localhost";
$username = "root";
$password = "louchin";
$dbname = "demo";


$conn = new mysqli($servername, $username, $password, $dbname);

if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
?>


<?php
if(isset($_POST['user_name']) && isset($_POST['user_email'])){
 $name=$_POST['user_name'];
 $emailId=$_POST['user_email']; 

 $checkdata="SELECT name,loginid FROM user WHERE name='$name' AND loginid='$emailId' ";

 $query=mysqli_query($conn, $checkdata);

 if(mysqli_num_rows($query) > 0)
 {
  return "User Name Already Exist";
  exit();
 }
 else
 {
  return "OK";
  exit();
 }     
}
?>

答案 2 :(得分:1)

更改您的表单和脚本,如下所示:仅在提交时一次检查两个字段

<script type="text/javascript">


 function checkall()
{
  var name=document.getElementById( "UserName" ).value;
  var email=document.getElementById( "UserEmail" ).value;
  if(name)
 {
  if(email)
  {
    $.ajax({
    type: 'post',
    url: 'checkdata.php',
    data: {
    user_email:email,user_name:name
    },
    success: function (response) {
     $( '#email_status' ).html(response);
     $( '#name_status' ).html(response);
     if(response=="OK")   
     {
      return true;    
     }
     else
     {
      return false;   
     }
    }
    });

  }
  else
  {
    $( '#email_status' ).html("");
    return false;

 }
 else
 {
  $( '#name_status' ).html("");
  return false;
 }


</script>
</head>
<body>

<form method="POST" action="insertdata.php" onsubmit="return checkall();">
 <input type="text" name="username" id="UserName" >
 <span id="name_status"></span>
 <br>
 <input type="text" name="useremail" id="UserEmail" >
 <span id="email_status"></span>
 <br>
 <input type="password" name="userpass" id="UserPassword">
 <br>
 <button type="submit" name="submit_form" value="Submit">Submit</button>
</form>

答案 3 :(得分:0)

我的声誉不足导致我把它放在答案而不是评论中。在checkdata.php中,您需要user_name和user_email。但是ajax调用包含user_name或user_email。你可以发送两个或分叉checkdata.php来单独检查它们。

答案 4 :(得分:0)

checkdata.php 中。

您可以像这样获得UsernameUserEmail的值

$name=$_POST['user_name']; 
$emailId=$_POST['user_email'];

你可以尝试这样的事情。

$name=$_REQUEST['username'];
$emailId=$_REQUEST['useremail'];