我試圖讓Autocomplete與Oracle數據庫一起工作。自動完成PHP JQuery Oracle
我發現了測試代碼,並試圖讓它與Oracle一起工作,但它不起作用,我不知道問題是在未觸及的搜索表單中還是在backend-search.php
(原始代碼是仍然在代碼評論#)
如果我在文本框中鍵入沒有任何反應。
數據庫連接在另一個表單上進行測試,我將數據獲取到表中。
如果有人能給我一個提示,我將不勝感激。
搜索form.php的
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>PHP Live MySQL Database Search</title>
<style type="text/css">
body{
font-family: Arail, sans-serif;
}
/* Formatting search box */
.search-box{
width: 300px;
position: relative;
display: inline-block;
font-size: 14px;
}
.search-box input[type="text"]{
height: 32px;
padding: 5px 10px;
border: 1px solid #CCCCCC;
font-size: 14px;
}
.result{
position: absolute;
z-index: 999;
top: 100%;
left: 0;
}
.search-box input[type="text"], .result{
width: 100%;
box-sizing: border-box;
}
/* Formatting result items */
.result p{
margin: 0;
padding: 7px 10px;
border: 1px solid #CCCCCC;
border-top: none;
cursor: pointer;
}
.result p:hover{
background: #f2f2f2;
}
</style>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.search-box input[type="text"]').on("keyup input", function(){
/* Get input value on change */
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".result");
if(inputVal.length){
$.get("backend-search.php", {term: inputVal}).done(function(data){
// Display the returned data in browser
resultDropdown.html(data);
});
} else{
resultDropdown.empty();
}
});
// Set search input value on click of result item
$(document).on("click", ".result p", function(){
$(this).parents(".search-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>
</head>
<body>
<div class="search-box">
<input type="text" autocomplete="off" placeholder="Stationsname..." />
<div class="result"></div>
</div>
</body>
</html>
後端-search.php中
<?php
error_reporting(E_ALL);
#$link = mysqli_connect("localhost", "root", "", "demo");
$db = '(DESCRIPTION =(ADDRESS =(PROTOCOL = TCP)(Host = xxxxxx)(Port = 1521))(CONNECT_DATA = (SERVICE_NAME = xxxxx)))';
$dbuser = 'xxxxx';
$pass = 'xxxxx';
$conn = oci_connect($dbuser, $pass, $db);
if(isset($_REQUEST['term'])){
// Prepare a select statement
#$sql = 'SELECT Standortname FROM DATABASE WHERE Standortname LIKE :s';
#if($stmt = mysqli_prepare($conn, $sql)){
if($stmt = oci_parse($conn, "SELECT Standortname FROM Database WHERE Standortname LIKE :s")){
// Bind variables to the prepared statement as parameters
#mysqli_stmt_bind_param($stmt, "s", $param_term);
oci_bind_by_name($stmt , ':s' , $param_term, -1); #$ph_name
// Set parameters
$param_term = $_REQUEST['term'] . '%';
// Attempt to execute the prepared statement
#if(mysqli_stmt_execute($stmt)){
if(oci_execute($stmt)){
#$result = mysqli_stmt_get_result($stmt);
$result = oci_result($stmt);
// Check number of rows in the result set
#if(mysqli_num_rows($result) > 0){
if(oci_num_rows($result) > 0){
// Fetch result rows as an associative array
#while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
while($row = oci_fetch_array($result, OCI_BOTH)){
echo "<p>" . $row["name"] . "</p>";
}
} else{
echo "<p>No matches found</p>";
}
} else{
echo "ERROR: Could not able to execute"; #$sql. " . mysqli_error($conn);
}
}
// Close statement
#mysqli_stmt_close($stmt);
oci_free_statement($stmt);
// close connection
#mysqli_close($conn);
oci_close($conn);
?>
Hy Tim,謝謝您的輸入。我真的不熟悉這個東西,但在你的幫助下,我能夠得到它的工作。非常感謝 – Fox83