2009-11-18 90 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保持不变。

*不,我没有测试过这个。