2009-11-18 87 views
0

如何捕獲表單中提交的值並將它們顯示在使用PHP的表單字段中的頁面上?我已經創建瞭如下形式:PHP表單問題

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
    <title>Order Form</title> 
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> 
</head> 
<body> 

<form name="orderform" method="get" action="ProcessOrder.php"> 
<p>Name<br/> 
<input type="text" name="name" size="50" /><br/> 
<p>Address<br/> 
<input type="text" name="address" size="50" /><br/> 
<p>City<br/> 
<input type="text" name="city" size="50" /><br/> 
<p>Province<br/> 
<input type="text" name="province" size="2" /><br/> 
<p>Postal Code<br/> 
<input type="text" name="postalcode" size="6" /><br/> 
<p>Email<br/> 
<input type="reset"/> 
<input type="submit"/><p> 
</form> 
</body> 
</html> 

編輯:如果我想使用的PHP文件來獲取值並顯示出來的每個字段將這項工作?例如:

<?php 
    <input type="text" name="name" size="50" value="isset($_GET["name"]) ? htmlspecialchars($_GET["name"]) : ""; ?>" /><br/> 
?> 

回答

3

對於每一個領域,做這樣的事情:

<input type="text" name="name" size="50" value="<?php print isset($_GET["name"]) ? htmlspecialchars($_GET["name"]) : ""; ?>" /><br/> 
+1

您需要其他示例不具有的isset()和htmlspecialchars()。 – Dave 2009-11-18 04:18:46

0

修訂:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 
<html> 
<head> 
    <title>Order Form</title> 
    <meta http-equiv="content-type" content="text/html;charset=UTF-8" /> 
</head> 
<body> 

<form name="orderform" method="get" action="ProcessOrder.php"> 
<p>Name<br/> 
<input type="text" name="name" size="50" value="<?php echo(htmlspecialchars ($_GET['name'])); ?>/><br/> 
<p>Address<br/> 
<input type="text" name="address" size="50"value="<?php echo(htmlspecialchars ($_GET['address'])); ?> /><br/> 
<p>City<br/> 
<input type="text" name="city" size="50" value="<?php echo(htmlspecialchars ($_GET['city'])); ?>/><br/> 
<p>Province<br/> 
<input type="text" name="province" size="2" value="<?php echo(htmlspecialchars ($_GET['province'])); ?>/><br/> 
<p>Postal Code<br/> 
<input type="text" name="postalcode" size="6" value="<?php echo(htmlspecialchars ($_GET['postalcode'])); ?>/><br/> 
<p>Email<br/> 
<input type="reset"/> 
<input type="submit"/><p> 
</form> 
</body> 
</html> 

編輯:對不起你使用GET POST沒有實現。

+0

1)表單是GET,不是POST; 2)XSS vunerabilities存在; 3)會引起(注意,錯誤,警告?不知道哪一個)。 – MiffTheFox 2009-11-18 04:16:45

+0

不要忘記清理輸入。這也是不允許的文件上傳元素。 – AverageAdam 2009-11-18 04:19:35

+0

已經在您的評論之前注意到了GET!更新爲xss。只是試圖向他展示一個快速的例子,但同意應該成爲解決方案的一部分。 – jaywon 2009-11-18 04:22:30

0

PHP manual對如何與形式的PHP

<form action="action.php" method="post"> 
<p>Your name: <input type="text" name="name" /></p> 
<p>Your age: <input type="text" name="age" /></p> 
<p><input type="submit" /></p> 
</form> 

Hi <?php echo htmlspecialchars($_POST['name']); ?>. 
You are <?php echo (int)$_POST['age']; ?> years old. 

樣本輸出工作的好教程的腳本可能是:

嗨,喬。你22歲。

1

我通常對沒有框架的簡單表單/網站使用類似的東西。

$name  = isset($_GET['name'])  ? trim($_GET['name'])  : ""; 
$address = isset($_GET['address']) ? trim($_GET['address']) : ""; 
$city  = isset($_GET['city'])  ? trim($_GET['city'])  : ""; 
$province = isset($_GET['province']) ? trim($_GET['province']) : ""; 
$postalcode = isset($_GET['postalcode']) ? trim($_GET['postalcode']) : ""; 

... 

<p>Name<br/> 
<input type="text" name="name" size="50" value="<?= htmlspecialchars($name); ?>" /><br/> 
<p>Address<br/> 
<input type="text" name="address" size="50" value="<?= htmlspecialchars($address); ?>" /><br/> 
<p>City<br/> 
<input type="text" name="city" size="50" value="<?= htmlspecialchars($city); ?>" /><br/> 
<p>Province<br/> 
<input type="text" name="province" size="2" value="<?= htmlspecialchars($province); ?>" /><br/> 
<p>Postal Code<br/> 
<input type="text" name="postalcode" size="6" value="<?= htmlspecialchars($postalcode); ?>" /> 

這允許您在需要時設置默認值,或者您可以將它們留空。

或者你可以做以下*

$fields = array(
    'name'  => '', 
    'address' => '', 
    'city'  => '', 
    'provice' => 'Quebec', 
    'postalcode' => '' 
); 

foreach ($fields as $field => $default) { 
    $$field = isset($_GET[$field]) ? trim($_GET[$field]) : $default; 
} 

和HTML保持不變。

*不,我沒有測試過這個。