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'); }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值