显示无字符输入的查询

时间:2013-02-12 14:15:13

标签: php jquery mysql javascript-events

我遇到了“google like”搜索引擎的问题。 当页面加载时,它希望输入框中的字符在div内显示结果。 如何在不在输入框中键入任何字符的情况下显示所有数据库内容? 以下是三个文件。

的index.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>

的search.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);
                    });
});

提前谢谢大家。

1 个答案:

答案 0 :(得分:1)

它将在加载时起作用:

$(document).ready(function(){
                        var search_query=$(this).val();
                        $.post('search.php', {search_query: search_query},function(searchq){
                        $('#search_query').html(searchq);
                    });