PHP+Ajax检测数据行数

index.php

用户名:

    <p><button id="regbtn" onclick="chkveri(),ajaxfunction()">注册</button></p>
    </section>
  <span id="s31"></span>   <span id="s32"></span>
    <script src="js/ajax.js"></script>
      <script src="js/j.js"></script>
    <?php
    // put your code here
    ?>
</body>

js/ajax.js

var xmlHttp;
/* 设置 */
function $(id){
return document.getElementById(id);
}

function ajaxfunction(){
if(window.ActiveXObject){//usually for old IE
xmlHttp=new ActiveXObject(“Microsoft.XMLHTTP”);
}
if(window.XMLHttpRequest){//for new IE and other browers, such as Chrome
xmlHttp=new XMLHttpRequest();
}
xmlHttp.onreadystatechange=readyStateChangHandle;
var username=$(‘username’).value;
url=“chkuser.php”;//;the page applying Ajax
url+="?username="+username;
xmlHttp.open(“GET”,url,true);
xmlHttp.send();
}
function readyStateChangHandle(){
if(xmlHttp.readyState=4&&xmlHttp.status=200){
xmlDoc=xmlHttp.responseText;
var s3=document.getElementById(“s31”);
s3.innerHTML= xmlDoc;
}
}

chkuser.php

<?php include 'includes/conn.php'; if(isset($_GET['username'])){ $username=$_GET['username']; $sql="select id from table where name='$username'"; $r= mysqli_query($mysqli, $sql); $num= mysqli_num_rows($r); echo $num; } else{ echo "Error"; } ?>

includes/conn.php

<?php DEFINE('DB_HOST','localhost'); DEFINE('DB_USER','root'); DEFINE('DB_PASSWORD','root'); DEFINE('DB_NAME','db); $mysqli=new MySQLi(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME); if($mysqli->connect_error){ echo $mysqli->connect_error; unset($mysqli); } else{ $mysqli->set_charset('utf8'); }

猜你喜欢

转载自blog.csdn.net/weixin_42467319/article/details/87893199