我在使用“google like”搜索引擎时遇到问题。当页面加载时,它期望输入框中的一个字符在 div 内显示结果。
如何在输入框中不输入任何字符的情况下显示所有数据库内容?下面是三个文件:
索引.html
<html>
<head>
<title></title>
</head>
<body>
Caută analizele în baza noastră de date.<br>Taxa de urgență este de 35 Ron
<p align ="right">
<input type="text" id="go" class="field" name="go" onclick="this.value='';" onfocus="this.select()" onblur="this.value=!this.value?'Caută analiza dorită':this.value;" value="Caută analiza dorită"/>
<input type="submit" name="search" id="submit" value="Cauta" /><br/>
<div id="search_query"></div>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.7.2.min.js"></script>
<script type="text/javascript" src="js/instant.js"></script>
</p>
</body>
</html>
搜索.php
<?php
$conn = mysql_connect("localhost","user","pass");
mysql_select_db("db");
mysql_set_charset("UTF8", $conn);
if(isset($_POST['search_query'])){
$search_query = mysql_real_escape_string(htmlentities($_POST['search_query']));
$search_query_x = explode(",",$search_query);
foreach($search_query_x as $search_each){
@$x++;
if($x==1)
@$construct.="Den LIKE '%$search_each%'";
else
$construct.="AND Den LIKE '%s$search_each%'";
}
$construct ="SELECT * FROM biochimie1 WHERE $construct";
$run = mysql_query($construct) or die(mysql_error());
$foundnum = mysql_num_rows($run);
// Define $color=1
$color="1";
if ($foundnum==0){
echo "Ne pare rau dar nu există rezultate pentru <strong><font color='red'>$search_query</font></strong></br></br> Vă rugăm să verificați dacă ați scris corect";
}
else{
echo'<p align="center">';
echo "$foundnum \n";
echo ' rezultate găsite în urma căutării!</p>';
echo "<table id='table1' border='0'width='500'>
<tr>
<th width='150' align='center' valign='middle'>Denumire Analize</th>
<th width='50' align='center' valign='middle'>Preț</th>
<th width='100' align='center' valign='middle'>Zile de recoltare</th>
<th width='100' align='center' valign='middle'>Termen de eliberare a rezultatelor</th>
<th width='100' align='center' valign='middle'>Se poate lucra în regim de urgență</th>
</tr></table>";
while($runrows = mysql_fetch_assoc($run))
{
$NrCrt = $runrows ['NrCrt'];
$denanalize = $runrows ['Den'];
$pret = $runrows ['Pret'];
$rec = $runrows ['Rec'];
$rez = $runrows ['Rez'];
$obs = $runrows ['Obs'];
echo "<table border='0'>";
// If $color==1 table row color = #FFC600
if($color==1){
echo "
<tr bgcolor='#efefef'>
<td width='150' align='center' valign='middle'>$denanalize</td>
<td width='50' align='center' valign='middle'>$pret</td>
<td width='100' align='center' valign='middle'>$rec</td>
<td width='100' align='center' valign='middle'>$rez</td>
<td width='100' align='center' valign='middle'>$obs</td>
</tr>
</table>";
// Set $color==2, for switching to other color
$color="2";
}
// When $color not equal 1, use this table row color
else {
echo "<tr bgcolor='#eaeaea'>
<td width='150' align='center' valign='middle'>$denanalize</td>
<td width='50' align='center' valign='middle'>$pret</td>
<td width='100' align='center' valign='middle'>$rec</td>
<td width='100' align='center' valign='middle'>$rez</td>
<td width='100' align='center' valign='middle'>$obs</td>
</tr>";
// Set $color back to 1
$color="1";
}echo "
</table>";
}
}
}
else{
echo 'An ERROR has occured';
}
?>
和 Instant.js 文件
$('#go').keyup(function(){
var search_query=$(this).val();
$.post('search.php', {search_query: search_query},function(searchq){
$('#search_query').html(searchq);
});
});
谢谢大家。