duanmengmiezen8855 2014-04-29 22:38
浏览 60
已采纳

如何使用AJAX执行MYSQLI查询(预处理语句)

So I have this (prepared statement) mysqli query in PHP (partial code):

#New DB connection
@ $mysqli = new mysqli ('localhost', 'user', 'password', 'database');

#Check DB connection, print error upon failure
if (mysqli_connect_errno()) {
    printf("<p>Connect failed.</p> <p>Error: \"%s\"</p>
", mysqli_connect_error());
    exit();
} #if

#Check if userName already exists
$query = "SELECT * FROM users WHERE userName = ?";
$stmt = $mysqli->prepare($query);
$stmt->bind_param("s", $registerUserName);
$stmt->execute();
$stmt->store_result();
$num_results = $stmt->num_rows;
if ($num_results) {
    echo "<p>This user already exists. Please click your back button.</p>";
    exit;
}; #if

And I would like to execute this through Javascript/AJAX on an onblur event:

function checkForUser(str) {
  var xmlhttp;    
  if (str=="") {
    return;
  }
  if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
  }
  else{// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
  xmlhttp.onreadystatechange=function(){
    if (xmlhttp.readyState==4 && xmlhttp.status==200) {
      document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
    }
  }
  xmlhttp.open(???,true); // not sure what goes here or if I should even use this method
  xmlhttp.send();
}

So is this possible? and how? I found some tutorials but they involve using jquery. Can I do it without jquery?

  • 写回答

1条回答 默认 最新

  • duanraa1984 2014-04-29 23:35
    关注

    I've changed a little bit your code. Hope it will helpfull:

    #New DB connection
    @ $mysqli = new mysqli ('localhost', '…', '…', '…');
    
    #Check DB connection, print error upon failure
    if (mysqli_connect_errno()) {
        printf("<p>Connect failed.</p> <p>Error: \"%s\"</p>
    ", mysqli_connect_error());
        exit();
    } #if
    
    #Check if userName already exists
    $query = "SELECT * FROM wp_users WHERE user_login = ?";
    $stmt = $mysqli->prepare($query);
    $stmt->bind_param("s", $_GET[registerUserName]); 
    $stmt->execute();
    $stmt->store_result();
    $num_results = $stmt->num_rows;
    if ($num_results) {
        echo "<p>This user already exists. Please click your back button.</p>";
        exit;
    }; #if
    

    Now we can call it with URL like "/ttt.php?registerUserName=user1" And function:

    function checkForUser(uname)
    {
    var req;
    req = null;
    if (window.XMLHttpRequest) {
        try {
            req = new XMLHttpRequest();
        } catch (e){}
    } else if (window.ActiveXObject) {
        try {
            req = new ActiveXObject('Msxml2.XMLHTTP');
        } catch (e){
            try {
                req = new ActiveXObject('Microsoft.XMLHTTP');
            } catch (e){}
        }
    }
    
    if (req) {       
        req.open("GET", "/ttt.php?registerUserName="+encodeURIComponent(uname), false);
        //req.onreadystatechange = processReqChange;
        req.send(null);
        if(req.status == 200) {
          return req.responseText;
        }
    
    }
    return false;
    }
    

    in the

    req.open("GET", "/ttt.php?registerUserName="+encodeURIComponent(uname), false);
    

    false means async request and req.send will wait for response from php script.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料