0

我想问是否有人可以帮助我进行搜索查询和显示结果。

这是代码...

<?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>

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

如果有人可以帮助我,那就太好了。先感谢您

附言

好吧,我不知道它实际上是如何工作的,但我在想,有没有一种方法可以将结果添加到表单下方的空 div 或类似的东西中?我尝试了上面的选项,但它没有帮助。

4

3 回答 3

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();
}
?>
于 2013-09-26T10:48:20.497 回答
0

你有两个选择,

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

  2. 将 Search Algorithm 和 PHP Code 放在表单同一页面的顶部,然后使用or来检查它是否已成功提交,保存结果和内容以便稍后在其他地方打印。isset() $_GET$_POST

于 2013-09-26T10:48:59.920 回答
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 ?>
于 2013-09-26T10:49:11.557 回答