【问题标题】:How to use AJAX to perform a MYSQLI query (prepared statement)如何使用 AJAX 执行 MYSQLI 查询(准备语句)
【发布时间】:2014-06-16 02:12:21
【问题描述】:

所以我在 PHP 中有这个(准备好的语句)mysqli 查询(部分代码):

#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>\n", 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

我想通过 Javascript/AJAX 对 onblur 事件执行此操作:

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();
}

那么这可能吗?如何?我找到了一些教程,但它们涉及使用 jquery。没有jquery可以吗?

【问题讨论】:

  • 有问题的文件的名称是什么(请求应该去哪里)?还有$registerUserName从哪里获得价值?
  • 只需在 google 上搜索“ajax 教程”,将回调 url 替换为您的 php 脚本,发布您需要的用户名,然后就可以使用了。

标签: php jquery ajax mysqli


【解决方案1】:

我对你的代码做了一点改动。希望对您有所帮助:

#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>\n", 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

现在我们可以使用像“/ttt.php?registerUserName=user1”这样的 URL 来调用它 及功能:

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;
}

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

false 表示异步请求,req.send 将等待 php 脚本的响应。

【讨论】:

  • 非常感谢您的回复。非常感谢。我还没来得及试一试.. 会回到这篇文章。
  • 不确定您是否会看到这个。再次您好,感谢在旧版 Explorer 浏览器上改进的 try/catch。想知道您是如何取消 'req.onreadystatechange=function(){.. '。我认为您的意思是“假”意味着“同步”请求,是吗? 'true' 是异步的,需要 onreadystatechange 事件(?)
  • 是的,没错。 req.open(…,false) 等待回复。默认情况下,此参数为 true 并异步执行。如果它是异步执行的,那么另一个线程将调用onreadychange 事件。但是checkForUser函数到那时就已经完成了,无法返回任何结果。
  • 但这可以通过 onreadychange 异步工作,不是吗?此外,它是如何在您编码时同步工作的?在'req.send(null);'之后该功能必须完成执行,对吗?这不像函数停止并等到'if(req.status == 200)'完成..
  • 来自 w3schools:“使用 async=false.. 请记住,在服务器响应准备好之前,JavaScript 不会继续执行。如果服务器繁忙或缓慢,应用程序将挂起或停止。 "我想知道 - 方法如何阻止 Javascript 代码执行?如何阻止 Javascript 代码执行?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-12-11
  • 1970-01-01
  • 1970-01-01
  • 2012-03-26
相关资源
最近更新 更多