我想问一下是否有人可以帮我查询搜索查询并显示结果。
这是代码......
<?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" />
</form>
所以......一切都正常工作......我得到了结果,一切都很好。问题是: 我希望结果显示在表单本身下方,而不是显示在新页面中。
如果有人能帮助我那会很棒。提前谢谢
P.S。
嗯,我不知道它是如何工作的,但正在思考,是不是有一种方法可以将结果添加到窗体下面的空div或类似的东西?我尝试了上面的选项,但它有所帮助。
答案 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" />
</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)
你有两个选择,
使用AJAX
实际获取结果以“收集”PHP生成的内容并使用JavaScript将其附加到页面上的某个位置。
将搜索算法和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" />
</form>
<?php echo $table ?>