PHP不会从表单中回显名称

时间:2016-02-29 17:16:39

标签: php html forms

我的表格如下所示。

<form role="form-horizontal" method="post" action="pacific.php" style="width 70%">
<div class="input-group input-group-lg">
<span class="input-group-addon" id="sizing-addon1">Name:</span>
<input type="text" class="form-control" placeholder="Name" id="name" aria-describedby="sizing-addon1">
</div>
<button type="submit" class="btn btn-default">Next ></button>
</form>

它向name提交pacific.php。在pacific.php我有php代码首先从name获取输入,然后我有一个echo语句,如下所示。

<?php
$n = $_POST["name"];
echo "Hello, $n.";
?>

然而在页面上,所有回声都是&#34;你好,。&#34;。我希望能够回应&#34;你好,(人名)。&#34;

3 个答案:

答案 0 :(得分:1)

您忘记将名称添加到所需的输入

<form role="form-horizontal" method="post" action="pacific.php" style="width 70%">
<div class="input-group input-group-lg">
<span class="input-group-addon" id="sizing-addon1">Name:</span>
<input type="text" class="form-control" placeholder="Name" id="name" aria-describedby="sizing-addon1" name="name">
</div>
<button type="submit" class="btn btn-default">Next ></button>
</form>

答案 1 :(得分:0)

<input type="text" class="form-control" placeholder="Name" id="name" aria-describedby="sizing-addon1">

更改为

<input type="text" class="form-control" placeholder="Name" name="name" id="name" aria-describedby="sizing-addon1">

您需要使用name属性,而发布表单PHP请求输入元素的名称。

答案 2 :(得分:0)

您的代码必须采用这种方式:使用post方法捕获数据需要name="name"

<form role="form-horizontal" method="post" action="pacific.php" style="width 70%">
<div class="input-group input-group-lg">
<span class="input-group-addon" id="sizing-addon1">Name:</span>
<input type="text" class="form-control" placeholder="Name" name="name" id="name" aria-describedby="sizing-addon1">
</div>
<button type="submit" class="btn btn-default">Next ></button>
</form>


<?php
$n = $_POST["name"];
echo "Hello, $n.";
?>