【发布时间】:2019-09-20 13:07:47
【问题描述】:
我正在尝试为我的网站制作联系表单,但无法获取 HTML 表单中的数据以发布到任何地方?
我做了一些研究并了解到,为了使表单能够“发布”,表单必须具有 type="submit" 和有效名称的输入或按钮标签,而表单必须具有方法属性,例如 method="POST"。
我还测试了 _POST 数组是否正在使用以下方法收集任何数据:
print_r($_POST); 数组始终为空。下面我包含了一些制作表单的代码,当您单击提交时,表单数据会被发布并整齐地显示。 此代码不是我的,该代码已从 http://www.learningaboutelectronics.com/Articles/How-to-check-if-the-submit-button-is-clicked-in-PHP.php 测试并运行,我将其包含在此处以证明由于某种原因运行和测试的代码仍会出现错误。
我正在使用 Wampserver 通过 localhost 运行 PHP。
维护者/升级到 2.5 由 Herve Leclerc Otomatic 升级到 3 (wampserver@otomatic.net) Jojaba 主页的多种样式 Inno Setup 安装程序:http://www.jrsoftware.org/isinfo.php 论坛 Wampserver:http://forum.wampserver.com/index.php 使用的版本: Apache 2.4.37 端口 80- PHP 7.2.1 MySQL 5.7.24 端口 3306 MariaDB 10.3.12 端口 3307 用于 CLI(命令行界面)的 PHP 5.6.40
这是经过测试和工作的代码
<form action="" method="POST">
<label>Enter Your Name Please:</label>
<input type="text"name=" name_entered" value='<?php echo $name; ?>'/>
<br><br>
<input type="submit" name="submitbutton" value="Submit"/>
</form>
<?php
$name= $_POST['name_entered'];
$submitbutton= $_POST['submitbutton'];
if ($submitbutton){
if (!empty($name)) {
echo 'The name you entered is ' . $name;
}
else {
echo 'You did not enter a name. Please enter a name into this form field.';
}
}
?>
和我的输出:
Enter Your Name Please:
( ! ) Notice: Undefined variable: name in C:\wamp64\www\Practice\index.php on line 3
Call Stack
#
Time 1
Memory 0.0001
Function 403584
Location {main}( )
...\index.php:0
'/>
The name you entered is 'example name'
我的问题是只有在我的系统上运行时才会发生此错误,如果是这种情况,可以采取哪些措施来解决此问题。这也是编码 POSTable 表单的正确方法吗?任何反馈都表示赞赏,谢谢。
【问题讨论】: