【发布时间】:2018-04-17 04:38:10
【问题描述】:
我正在尝试使用 php ajax 和 oracle db 实现实时搜索。但是当我在搜索框中输入时,我没有得到结果。我的表格内容如下,
SQL> 从搜索中选择 *;
ID NAME
1 David Copperfield
2 Ricky Ponting
3 Cristiano Ronaldo
4 Lionel Messi
5 Shane Watson
请让我知道我必须在哪里更改我的代码。
代码:
1. db.php
<?php
// Create connection to Oracle
$conn = oci_connect("system", "******","//localhost/xe");
//Check connection
if (!$conn) {
$m = oci_error();
echo $m['message'], "\n";
exit;
}
else {
print "Connected to Oracle!";
}
?>
2。搜索.php
<!DOCTYPE html>
<html>
<head>
<title>Live Search using AJAX</title>
<!-- Including jQuery is required. -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<!-- Including our scripting file. -->
<script type="text/javascript" src="script.js"></script>
<!-- Including CSS file. -->
<link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
<!-- Search box. -->
<input type="text" id="search" placeholder="Search" />
<br>
<b>Ex: </b><i>David, Ricky, Ronaldo, Messi, Watson, Robot</i>
<br />
<!-- Suggestions will be displayed in below div. -->
<div id="display"></div>
</body>
</html>
3. ajax.php
<?php
//Including Database configuration file.
include "db.php";
//Getting value of "search" variable from "script.js".
if (isset($_POST['search'])) {
//Search box value assigning to $Name variable.
$Name = $_POST['search'];
//Search query.
$query='SELECT Name FROM search WHERE Name LIKE '%Name%'';
//Query execution
$stid = oci_parse($conn, $query);
$execquery = oci_execute($stid);
//Creating unordered list to display result.
echo '
<ul>
';
//Fetching result from database.
while ($Result = oci_fetch_array($stid)) {
?>
<!-- Creating unordered list items.
Calling javascript function named as "fill" found in "script.js" file.
By passing fetched result as parameter. -->
<li onclick='fill("<?php echo $Result['Name']; ?>")'>
<a>
<!-- Assigning searched result in "Search box" in "search.php" file. -->
<?php echo $Result['Name']; ?>
</li></a>
<!-- Below php code is just for closing parenthesis. Don't be confused. -->
<?php
}}
?>
</ul>
4. script.js
//Getting value from "ajax.php".
function fill(Value) {
//Assigning value to "search" div in "search.php" file.
$('#search').val(Value);
//Hiding "display" div in "search.php" file.
$('#display').hide();
}
$(document).ready(function() {
//On pressing a key on "Search box" in "search.php" file. This function will be called.
$("#search").keyup(function() {
//Assigning search box value to javascript variable named as "name".
var name = $('#search').val();
//Validating, if "name" is empty.
if (name == "") {
//Assigning empty value to "display" div in "search.php" file.
$("#display").html("");
}
//If name is not empty.
else {
//AJAX is called.
$.ajax({
//AJAX type is "Post".
type: "POST",
//Data will be sent to "ajax.php".
url: "ajax.php",
//Data, that will be sent to "ajax.php".
data: {
//Assigning value of "name" into "search" variable.
search: name
},
//If result found, this funtion will be called.
success: function(html) {
//Assigning result to "display" div in "search.php" file.
$("#display").html(html).show();
}
});
}
});
});
5.样式.css
a:hover {
cursor: pointer;
background-color: yellow;
}
谢谢。
【问题讨论】:
-
将
$query='SELECT Name FROM search WHERE Name LIKE '%Name%'';更改为$query= "SELECT Name FROM search WHERE Name LIKE '%$Name%'";。注意双引号的使用。加号$Name -
你好,我改了,但是没用。 $query="SELECT Name FROM search WHERE Name LIKE '%$Name%'";
-
您能在
$execquery = oci_execute($stid);之后添加var_dump($execquery);并告诉我们您得到了什么吗?谢谢 -
我在执行 ajax.php 时遇到错误。 ( ! ) 解析错误:语法错误,第 13 行 C:\wamp\www\ajax.php 中的意外 '$Name' (T_VARIABLE)
-
我不确定出了什么问题。数据库连接成功。