我已经为我们工作中的数据库设置了快速搜索功能。基本上你搜索一个电子邮件地址,你会得到基于电子邮件的点击。
目前它工作正常,例如我可以写anna
,并获得像anna@thedomain.com
. 如果我搜索thedomain.com
.
但是第二次我写“anna@”我得到“没有命中”。因此,当我在搜索字段中包含 @ 时,我只是没有得到任何点击。
该脚本是基于index.html
其中有一些js
附加到它,以及search.php
脚本。
以下是代码index.html
:
<!DOCTYPE HTML>
<html>
<head>
<!-- Meta -->
<meta charset='utf-8'>
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<title>Live search</title>
<meta name="description" content="">
<meta name="viewport" content="width=device-width">
<link rel="icon" href="favicon.ico" type="image/x-icon">
<!-- Load CSS -->
<link href="style/style.css" rel="stylesheet" type="text/css" />
<!-- Load Fonts -->
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=PT+Sans:regular,bold" type="text/css" />
<!-- Load jQuery library -->
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<!-- Load custom js -->
<script type="text/javascript" src="scripts/custom.js"></script>
</head>
<body>
<div id="main">
<!-- Title -->
<div class="icon"></div>
<h1 class="title">Search</h1>
<h5 class="title">Find relevant data</h5>
<!-- Input -->
<input type="text" id="search" value="E-Mailaddress" onfocus="this.value = ''; return true" onblur="this.value = 'E-Mailaddress'; return true" autocomplete="off">
<!-- Shows The result -->
<h4 id="results-text">Showing results for: <b id="search-string">Array</b> </h4>
<ul id="results"></ul>
</div>
</body>
</html>
search.php
脚本:
<?php
/************************************************
The Search PHP File
************************************************/
/************************************************
MySQL Connect
************************************************/
// Credentials
$dbhost = "localhost";
$dbname = "dbname";
$dbuser = "dbuser";
$dbpass = "dbpassword";
// Connection
global $tutorial_db;
$tutorial_db = new mysqli();
$tutorial_db->connect($dbhost, $dbuser, $dbpass, $dbname);
// Check Connection
if ($tutorial_db->connect_errno) {
printf("Connect failed: %s\n", $tutorial_db->connect_error);
exit();
}
/************************************************
Search Functionality
************************************************/
// Define Output HTML Formating
$html = '';
$html .= '<li class="result">';
$html .= '<a target="_blank" href="urlString">';
$html .= '<h3>nameString</h3>';
$html .= '<h4>functionString</h4>';
$html .= '</a>';
$html .= '</li>';
// Get Search
$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$search_string = $tutorial_db->real_escape_string($search_string);
// Check Length More Than One Character
if (strlen($search_string) >= 1 && $search_string !== ' ') {
// Build Query
$query = "SELECT * FROM table_name WHERE Emailaddress LIKE '%".$search_string."%'";
// Do Search
$result = $tutorial_db->query($query);
while($results = $result->fetch_array()) {
$result_array[] = $results;
}
// Check If We Have Results
if (isset($result_array)) {
foreach ($result_array as $result) {
// Just a normal output
$output = "<li>$result[Dataentry] / <a href='showmore.php?ticketid=$result[ticketref]'>Go to ticket</a> / <strong>$result[Emailaddress]</strong></li>";
// Output
echo $output;
}
}
else{
// Format No Results Output
$output = str_replace('urlString', 'javascript:void(0);', $html);
$output = str_replace('nameString', '<b>Ingen resultater...</b>', $output);
$output = str_replace('functionString', 'Sorry :(', $output);
// Output
echo($output);
}
}
/*
// Build Function List (Insert All Functions Into DB - From PHP)
// Compile Functions Array
$functions = get_defined_functions();
$functions = $functions['internal'];
// Loop, Format and Insert
foreach ($functions as $function) {
$function_name = str_replace("_", " ", $function);
$function_name = ucwords($function_name);
$query = '';
$query = 'INSERT INTO search SET id = "", function = "'.$function.'", name = "'.$function_name.'"';
$tutorial_db->query($query);
}
*/
?>
包含的custom.js
文件:
/* JS File */
// Start Ready
$(document).ready(function() {
// Icon Click Focus
$('div.icon').click(function(){
$('input#search').focus();
});
// Live Search
// On Search Submit and Get Results
function search() {
var query_value = $('input#search').val();
$('b#search-string').html(query_value);
if(query_value !== ''){
$.ajax({
type: "POST",
url: "search.php",
data: { query: query_value },
cache: false,
success: function(html){
$("ul#results").html(html);
}
});
}return false;
}
$("input#search").live("keyup", function(e) {
// Set Timeout
clearTimeout($.data(this, 'timer'));
// Set Search String
var search_string = $(this).val();
// Do Search
if (search_string == '') {
$("ul#results").fadeOut();
$('h4#results-text').fadeOut();
}else{
$("ul#results").fadeIn();
$('h4#results-text').fadeIn();
$(this).data('timer', setTimeout(search, 100));
};
});
});
index.html
, search.php
&custom.js
都是编码的utf-8
,我的数据库是utf8_general_ci
.
谁能明白为什么这行不通?我是 JavaScript 新手,感觉JS
.