【发布时间】:2016-04-29 23:54:41
【问题描述】:
我已经完成了实时搜索部分。我想将搜索结果的值放入不同 php 文件的文本框中。
AJAX 代码:[在 index.php 中]
<script>
function search(string){
var xmlhttp;
if(window.XMLHttpRequest){
xmlhttp = new XMLHttpRequest();
}else{
xmlhttp = new ActiveXObject("XMLHTTP");
}
xmlhttp.onreadystatechange = function(){
if(xmlhttp.readyState == 4 && xmlhttp.status == 200){
document.getElementById("search_div").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "search.php?s="+string, true);
xmlhttp.send(null);
}
</script>
html文本类型:[in index.php]
<div class="input-field col s12">
<i class="material-icons prefix">account_circle</i>
<input id="name" type="text" name="name" class="validate" onkeyup="search(this.value)">
<label for="name">Doctor's Name</label>
<div id="search_div">
</div>
从数据库中检索数据:[in search.php]
<?php
ob_start();
require 'config.php';
$conn = new mysqli("localhost","root","","dcare");
if($conn->connect_errno)
{
die('Sorry! Connection was not Successful');
}
if(isset($_GET['s']) && $_GET['s'] != ''){
$s = $_GET['s'];
$sql = "SELECT * FROM doctor WHERE name LIKE '%$s%'";
$result = $conn->query($sql) or die($conn->error);
while($row = $result->fetch_assoc()){
$name = $row['name'];
$designation = $row['designation'];
echo "<div style='' id='searchtitle'>" . "<a style='font-family: verdana; text-decoration: none; color: black; href='$name'>" . $name . "</a>" . "</div>";
}
}
?>
【问题讨论】:
-
是否要永久更改 index.php 中的值?还是只是临时为当前用户的会话赋值?
-
对于当前用户会话。
-
所以根据你所说的,听起来你只是想在点击后更新同一页面上的值?会不会是正确的。还是页面要重载,重载后需要在表单中显示名称?
-
你是对的。该值需要更新。无需重新加载页面。
标签: javascript php html ajax