gpt4 book ai didi

php - 基于 Ajax 的搜索 PHP

转载 作者:行者123 更新时间:2023-11-29 12:25:25 25 4
gpt4 key购买 nike

我正在尝试在 PHP 中创建基于 AJAX 的搜索。到目前为止我编写的代码似乎还不能工作。任何建议都会有很大帮助。提前致谢。这是我的代码。

index.php

<head>
<script src = "http://code.jquery.com/jquery-1.9.1.js"></script>
</head>

<body>

<input type="text" name="text" id="text" autocomplete="off" onkeyup="showHint(this.value)"/>


<div id="inner"></div>
<script type="text/javascript">

function showHint(str) {
if(str.length == 0) {
document.getElementById('inner').innerHTML = "search";
return;
}

if(window.XMLHttpRequest) {
xmlhttp = XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

xmlhttp.onreadystatechange = function() {
if(xmlhttpreadystate == 4 && xmlhttp.status == 200) {
document.getElementById('inner').innerHTML = xmlhttp.responseText;
}
}

xmlhttp.open("REQUEST", "search.php?text"+str, true);
xmlhttp.send();

}

</script>

</body>

</html>

搜索.php

<?php

$host = 'localhost';
$user = 'root';
$password= 'root';
$db = 'demo';

@$conn = mysql_connect($host, $user, $password) or die(mysql_error());
mysql_select_db($db, $conn);

/*if($result) {
echo "success";
} else { echo "fail"; }
*/



$text = $_REQUEST['text'];
$text = preg_replace('#[^a-z0-9]#i', '', $text);

$query = "SELECT * FROM users where first_name LIKE '%$text%' OR last_name LIKE '%$text%'";

$action = mysql_query($query);
$result = mysql_num_rows($action);

while($res = mysql_fetch_array($action)) {
$output .= $res['first_name']. ' '.$res['last_name'];
echo $output;
}


?>

最佳答案

您错过了index.php 脚本中的等号(在文本之后)。

xmlhttp.open("REQUEST", "search.php?text="+str, true);

关于php - 基于 Ajax 的搜索 PHP,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28404462/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com