我正在尝试让搜索引擎结果页面显示一条消息,说“如果没有输入密钥,则不会输入关键字,请再试一次”。但我一直忽略了下面脚本的问题,如果你没有输入密钥,脚本不允许你按回车,但我只有那里,因为我没有关于如何进行搜索的线索引擎结果页面显示一条消息“没有搜索关键词请再试一次”任何人?
搜索栏:
<form action="/search.php" method="GET">
<input class="term" type="text" id="term" name="term" required />
<input type="submit" class='submit' id="submit" value="search" disabled />
</form>
<script type="text/javascript">
document.getElementById('term').oninput = function() {
document.getElementById('submit').disabled = !this.value.trim();
}
</script>
显示搜索结果:
<?php
$db = mysqli_connect('localhost','root', '', 'searchengine');
if(!$db) {
die('sorry we are having some problbems');
}
$sql = mysqli_query(
$db,
sprintf(
"SELECT * FROM searchengine WHERE name LIKE '%s' LIMIT 0,20",
'%'. mysqli_real_escape_string($db,$_GET['term']) .'%'
)
);
while($ser = mysqli_fetch_array($sql)) {
echo "<a href='$ser[pageurl]'>$ser[img]</a>";
}
mysqli_close($db);
?>
答案 0 :(得分:0)
<?php
$db = mysqli_connect('localhost','root', '', 'searchengine');
if(!$db) {
die('sorry we are having some problbems');
}
// SET GETTER AS A VARIABLE
$searchTerm = mysqli_real_escape_string($db,$_GET['term']);
if ( empty($searchTerm))
{
echo("no key words searched please try again");
}
else
{
$sql = mysqli_query(
$db,
sprintf(
"SELECT * FROM searchengine WHERE name LIKE '%s' LIMIT 0,20",
'%'. $searchTerm .'%'
)
);
while($ser = mysqli_fetch_array($sql)) {
echo "<a href='$ser[pageurl]'>$ser[img]</a>";
}
}
mysqli_close($db);
?>
我认为,基于你的问题,这就是你想要的。
我希望有所帮助!
答案 1 :(得分:0)
您可以检查该术语是否为空,如果是,则退出并显示消息
<?php
if(empty($_GET['term']){
exit("no key words entered please try again");
}
$db = mysqli_connect('localhost','root', '', 'searchengine');
//rest of your code
答案 2 :(得分:0)
<div id="result" style="display:none">
<!-- Here is for result -->
</div>
<script type="text/javascript">
$.sendComment = function(){
var deger = $("form#search").serialize();
$.ajax({
url: "yourpost.php",
type: "POST",
data: result,
dataType: "json",
success: function(answer){
if(answer.error){
$("#result").html(answer.error).show();
}else{
$("#result").html(answer.ok).show();
}
}
});
}
</script>
yourpost.php
<?php
require "connect.php";
if(@$_SERVER["HTTP_REFERER"]==""){
$array["error"]='No post';
}else{
if($_POST){
@$name = strip_tags(mysql_real_escape_string($_POST['name']));
if(isempty($name)){
$array["error"] = "no post";
}else{
//SQL query..
$array["ok"] = "Search is success";
}
}
echo json_encode($array);
?>