PHP $_POST 只有 id,没有名字
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6351204/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP $_POST and id only, no name
提问by mattdwen
Can someone please explain this to me?
有人可以向我解释一下吗?
I have the following code:
我有以下代码:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" id="testField" />
<input type="submit">
</form>
<br /><br />
<pre>
<?php print_r($_POST); ?>
</pre>
This works fine on my main dev box, and on the server. However, I'm having to work remotely on my laptop at the moment. I've installed the exact same WAMPServer 2.1a build as on my dev setup, and the $_POST array is empty.
这在我的主开发箱和服务器上运行良好。但是,我目前必须在笔记本电脑上远程工作。我已经安装了与我的开发设置完全相同的 WAMPServer 2.1a 版本,并且 $_POST 数组是空的。
If I declare the field like:
如果我声明该字段如下:
<input type="text" name="testField" />
I get the expected output.
我得到了预期的输出。
回答by Ignacio Vazquez-Abrams
From the HTML 4.01 specification, §17.2, "Controls":
来自 HTML 4.01 规范第 17.2 节“控件”:
A control's "control name" is given by its name attribute.
...
When a form is submitted for processing, some controls have their name paired with their current value and these pairs are submitted with the form.
控件的“控件名称”由其名称属性给出。
...
提交表单进行处理时,某些控件的名称与其当前值成对,并且这些对与表单一起提交。
"id" does not matter.
“id”无关紧要。
回答by dynamic
Standard behavior. Always use name
within form.
标准行为。始终name
在表格内使用。
回答by abelvf
You never can pass a value to another page using the id, the only attribute that php can read via methos POST is using the name of the object.
您永远不能使用 id 将值传递给另一个页面,php 可以通过方法 POST 读取的唯一属性是使用对象的名称。
回答by user2095034
That suggest you create a textfield that you populate with the id from the drop down list whenever changed. When you post the form the textbox will show the previous id of the drop down list. You can set the textfield to hidden
这建议您创建一个文本字段,在更改时使用下拉列表中的 id 填充该文本字段。当您发布表单时,文本框将显示下拉列表的前一个 id。您可以将文本字段设置为隐藏