2014-09-13 33 views
0

你好返回的結果從數據庫中,我做了這個簡單的代碼來顯示從我的數據庫信息到一個表,表單代碼如下: http://pastebin.com/yyzcjshn我如何能展現在提交表單的同一頁上使用PHP

的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

你對「同一頁」的定義是什麼?你的意思是沒有刷新瀏覽器/按下提交按鈕時導航到新頁面? – EWit 2014-09-13 10:24:04

+0

是的,我不希望它在另一個頁面中顯示結果。 – WhiteOne 2014-09-13 10:26:47

+1

你的代碼易受[注入](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1)和'mysql_ *'函數[棄用](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/12860046#12860046)。 – 2014-09-13 10:31:58

回答

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

好運。

相關問題