如何在表单下方显示搜索结果

时间:2013-09-26 10:44:06

标签: php html

我想问一下是否有人可以帮我查询搜索查询并显示结果。

这是代码......

<?php
// Change the fields below as per the requirements
$db_host="localhost";
$db_username="dasi";
$db_password="**************";
$db_name="dasi";
$db_tb_name="test";
$db_tb_atr_price="price_vat";
$db_tb_atr_cur="currency";

//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen

mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");

$query=mysql_real_escape_string($_GET['query']);

$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE code like '%".$query."%'");

echo "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_array($query_for_result))
{
    echo "<table border=1>";
    echo "<tr><td>" . $data_fetch['code'] . "</td><td>" . $data_fetch['price_vat'] . "</td><td>" .  $data_fetch['currency'] . "</td></tr>";
    echo "</table>";
}
echo "</ol>";



mysql_close();
?>

在内容中我添加了表单...

<form action="search.php" method="post">
<label>Search For: </label><input name="query" type="text" /><input name="submit" type="submit" value="Start Search" />
&nbsp;</form>

所以......一切都正常工作......我得到了结果,一切都很好。问题是: 我希望结果显示在表单本身下方,而不是显示在新页面中。

如果有人能帮助我那会很棒。提前谢谢

P.S。

嗯,我不知道它是如何工作的,但正在思考,是不是有一种方法可以将结果添加到窗体下面的空div或类似的东西?我尝试了上面的选项,但它有所帮助。

3 个答案:

答案 0 :(得分:0)

只需在 form 中嵌入 search.php 代码,然后检查 isset($submit) ,你很高兴。

<?php
?>
<form action="" method="post">
    <label>Search For: </label><input name="query" type="text" /><input name="submit" type="submit" value="Start Search" />
    &nbsp;</form>
<?php

if(isset($submit))
{
// Change the fields below as per the requirements
$db_host="localhost";
$db_username="dasi";
$db_password="**************";
$db_name="dasi";
$db_tb_name="test";
$db_tb_atr_price="price_vat";
$db_tb_atr_cur="currency";

//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen

mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");

$query=mysql_real_escape_string($_POST['query']);

$query_for_result=mysql_query("SELECT * FROM $db_tb_name WHERE code like '%".$query."%'");

echo "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_array($query_for_result))
{
    echo "<table border=1>";
    echo "<tr><td>" . $data_fetch['code'] . "</td><td>" . $data_fetch['price_vat'] . "</td><td>" .  $data_fetch['currency'] . "</td></tr>";
    echo "</table>";
}
echo "</ol>";



mysql_close();
}
?>

答案 1 :(得分:0)

你有两个选择,

  1. 使用AJAX实际获取结果以“收集”PHP生成的内容并使用JavaScript将其附加到页面上的某个位置。

  2. 将搜索算法和PHP代码放在表单同一页面的顶部,然后使用isset() $_GET$_POST检查是否已成功提交,保存结果&amp;内容稍后将在别处打印。

答案 2 :(得分:0)

将表格保存到变量中:

$table = "<h2>Search Results</h2><ol>";
while($data_fetch=mysql_fetch_array($query_for_result))
{
    $table .= "<table border=1>";
    $table .= "<tr><td>" . $data_fetch['code'] . "</td><td>" . $data_fetch['price_vat'] . "</td><td>" .  $data_fetch['currency'] . "</td></tr>";
    $table .= "</table>";
}
$table .= "</ol>";

然后将其打印在模板中:

<form action="search.php" method="post">
<label>Search For: </label><input name="query" type="text" /><input name="submit" type="submit" value="Start Search" />
&nbsp;</form>
<?php echo $table ?>