所以我试图对PHP脚本进行异步调用但由于某种原因存在错误:超出最大调用堆栈大小使得我的网站滞后很多。在我的代码中必须有一个循环,但我无法真正找到它。我正在寻找过去3天仍然没有。如果有人能发现任何东西我真的会非常棒。提前谢谢!
PHP代码:
<?php
require_once 'li.php'; //File with the constants
$Username = $_POST['Username'];
$query = "SELECT Username
FROM user_info
WHERE Username = ?
LIMIT 1";
if($stmt = $conn->prepare($query)){ //Prepares the statement!
$stmt->bind_param('s', $Username);
$stmt->execute(); //Executes it!
if($stmt->fetch()){ //Checks if the query returns anything!
$stmt->close(); //If yes then closes the prepared statement!
$error = "Username taken";
echo $error;
}
}
?>
AJAX / JS代码:
$(document).ready(function(){
$('#Username').on("keyup", function(){
$.ajax({
type: 'POST',
url: 'NameValidation.php', //Your required php page
data: { Username: Username }, //pass your required data here
async: true,
}).done(function(response){
if(response != ""){
$('#UsernameLabel').html("Username Taken!");
}
});
});
});
如果你不理解我想用这个代码做什么,让我解释一下。我希望每次用户名输入更改为搜索用户名是否已存在于数据库中,并通过更改Label文本提醒用户。
PS:不要担心SQL注入安全性我将在以后修复此问题时添加它! ^ - ^
答案 0 :(得分:0)
如果查询没有返回任何内容,则表示您没有关闭连接,请尝试将代码更改为:
if($stmt->fetch()){ //Checks if the query returns anything!
$error = "Username taken";
echo $error;
}
$stmt->close(); //close connection in any case