如何使用php在提交表单的同一页面中显示从数据库返回的结果

时间:2014-09-13 10:16:37

标签: php

您好,我制作了这个简单的代码,将数据库中的信息显示在表格中,表格代码如下: http://pastebin.com/yyzcjshn

show.php代码为:

       <html>
<head>
<title>Show Result</title>

<?php
$connection = mysql_connect("localhost","root","");
// Check connection
if(!$connection){
die("Database connection failed: " . mysql_error());
}
//select database to use
$db_select = mysql_select_db("sells",$connection);
if(!$db_select){
die("Database selection failed: " . mysql_error());
}


$D1 = $_POST['D1'];

//show info
if($D1 != 'Show All'){
$result = mysql_query("SELECT * FROM clients WHERE Status='$D1'", $connection);
}
else $result = mysql_query("SELECT * FROM clients", $connection);
    if(!$result){
die("Database query failed: " . mysql_error());
}
echo "<table border='1'>
<tr>
<th>Order ID</th>
<th>Client Name</th>
<th>URL</th>
<th>Quantity</th>
<th>Price[$]</th>
<th>Status</th>
</tr>";
while($row = mysql_fetch_array($result)){
    echo "<tr>";
    echo "<td>" . $row["Order_ID"]."</td>";
    echo "<td>" . $row["ClientName"]."</td>";
    echo "<td><a href=" . $row['Url'] . " target=_blank >" . $row['Url'] . "</a></td>";
    echo "<td>" . $row["Quantity"]."</td>";
    echo "<td>" . $row["Price"]."</td>";
    echo "<td>" . $row["Status"]."</td>";
    echo "</tr>";
        }
        echo "</table>";



mysql_close($connection);
?>
</head>
</html>

如何使用php(不带ajax)在提交表单的同一页面显示结果?

1 个答案:

答案 0 :(得分:0)

简单回答:你不能。

在评论中,您说人们使用<?php echo $_SERVER['PHP_SELF']; ?>,但这不一样。 $_SERVER['PHP_SELF']将回显当前脚本文件名。使用它时,您不必手动在表单中编写action标记。

AJAX是在不刷新整个页面的情况下更新页面所需的方法。您可以使用(现代)AJAX方法或重定向方法。因为您不想在这里使用AJAX,所以是代码的重定向方法示例:

您的form.php。请注意,我将您的下拉列表替换为链接。您可以使用链接轻松approuch。您还可以保留下拉列表并使用Javascript / JQuery创建正确的重定向,包括GET参数:

<html>

    <head>
        <title>Show Info In Table</title>
    </head>

    <body>

      ....
      <a href="/form.php?D1=Show All">Show all</a>
      ....
      <a href="/form.php?D1=Finished">Finished</a>
      ....


      <?=include('show.php');?>
    </body>

</html>

show.php,请注意$_POST已替换为$_GET

<?php
$connection = mysql_connect("localhost","root","");
// Check connection
if(!$connection){
die("Database connection failed: " . mysql_error());
}
//select database to use
$db_select = mysql_select_db("sells",$connection);
if(!$db_select){
die("Database selection failed: " . mysql_error());
}


$D1 = $_GET['D1'];

//show info
if($D1 != 'Show All'){
$result = mysql_query("SELECT * FROM clients WHERE Status='$D1'", $connection);
}
else $result = mysql_query("SELECT * FROM clients", $connection);
    if(!$result){
die("Database query failed: " . mysql_error());
}
echo "<table border='1'>
<tr>
<th>Order ID</th>
<th>Client Name</th>
<th>URL</th>
<th>Quantity</th>
<th>Price[$]</th>
<th>Status</th>
</tr>";
while($row = mysql_fetch_array($result)){
    echo "<tr>";
    echo "<td>" . $row["Order_ID"]."</td>";
    echo "<td>" . $row["ClientName"]."</td>";
    echo "<td><a href=" . $row['Url'] . " target=_blank >" . $row['Url'] . "</a></td>";
    echo "<td>" . $row["Quantity"]."</td>";
    echo "<td>" . $row["Price"]."</td>";
    echo "<td>" . $row["Status"]."</td>";
    echo "</tr>";
        }
        echo "</table>";



mysql_close($connection);
?>
祝你好运。