我为我们工作的数据库设置了快速搜索功能。基本上你搜索一个电子邮件地址,你会根据电子邮件获得点击。
目前它工作正常,我可以写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
&amp; custom.js
全部编码在utf-8
中,我的数据库为utf8_general_ci
。
谁能明白为什么这不起作用?我是JavaScript的新手,并且感觉JS
中存在问题。
答案 0 :(得分:2)
$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
将除A-Z
,a-z
和0-9
之外的所有字符替换为空格。
将 @ 符号添加到排除的字符:
$search_string = preg_replace("/[^A-Za-z0-9@]/", " ", $_POST['query']);
答案 1 :(得分:2)
你的正则表达式可能有问题。您正在剥离包含@的所有非字母字符。您可以尝试以下操作,将@从正则表达式中删除。
$search_string = preg_replace("/[^A-Za-z0-9@]/", " ", $_POST['query']);