使用PHP变量填充输入字段

时间:2017-07-03 18:28:38

标签: php

我正在用php创建一个披萨订购网站。现在我想要回显通过表单中的URL传递的变量。我知道如何检索这些数据:<?php echo $_GET["numpizzas"]; ?>。但我不知道将它添加到我的html表单字段的正确方法。非常感谢任何帮助

<?php

echo 
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="<?php echo "hi"; ?>" >
//Money field does not populate with number, I just see <?php echo $_GET[ 
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';

?>
<?php echo $_GET["numpizzas"]; ?>

我也尝试将整数存储在变量$howmanypizzas = $_GET["numpizzas"]; ?>中,但它仍然不会显示为字段值。

3 个答案:

答案 0 :(得分:1)

<?php echo $_GET["numpizzas"]; ?>不仅会检索数据。 echo也将其输出到html响应(屏幕)

由于您已经使用ECHO传递了html,因此可以执行以下操作:

<?php

echo 
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="'.$_GET["numpizzas"].'" >
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';

?>

说明:echo是一个接收字符串并将其输出到html的函数。 因此,使用连接运算符.,您可以将$_GET["numpizzas"]变量作为字符串注入到html中,并将其传递给echo函数,然后将其输出到浏览器。 解决它的另一种方法是只调用你需要处理逻辑的PHP,就像@pavithra回答一样,它也有效。

答案 1 :(得分:1)

你已经回应并试图回应内心。您需要将变量与您回显的字符串连接起来,请参阅PHP Strings

echo 
'<form action="pizza.php" method="post">
<h1>Thanks for Ordering. Please submit your delivery info.</h1>
<label>Name:</label> <input type="text" name="name">
<label>Address:</label> <input type="text" name="address">
<label>Phone:</label> <input type="text" name="phone">
<label>Money: </label><input type="text" name="money" value="' . $_GET["numpizzas"] . '">
<label>Feedback:</label> <input type="text" name="feedback">
<input type="submit" value="Submit">
</form>';

您可能还会考虑Heredoc语法。

答案 2 :(得分:0)

<input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />

但是我不明白为什么你会得到这个作为一个get variable.hope这个工作

<form action="pizza.php" method="post">
   <h1>Thanks for Ordering. Please submit your delivery info.</h1>
   <label>Name:</label> <input type="text" name="name">
   <label>Address:</label> <input type="text" name="address">
   <label>Phone:</label> <input type="text" name="phone">
   <label>Money: </label> <input type="text" name="money" value="<?php echo $_GET["numpizzas"]; ?>" />

   <label>Feedback:</label> <input type="text" name="feedback">
   <input type="submit" value="Submit">
</form>
相关问题