Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP $_POST and id only, no name

Tags:

php

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.

If I declare the field like:

<input type="text" name="testField" />

I get the expected output.

like image 894
mattdwen Avatar asked Jun 14 '11 23:06

mattdwen


People also ask

What is $post in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables. The example below shows a form with an input field and a submit button.

Is $_ POST an array?

The PHP built-in variable $_POST is also an array and it holds the data that we provide along with the post method.

How to use_ POST in PHP?

The $_POST variable is an array of variable names and values sent by the HTTP POST method. The $_POST variable is used to collect values from a form with method="post". Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

Why POST is used in PHP?

POST is used to send data to a server to create/update a resource.


2 Answers

Standard behavior. Always use name within form.

like image 121
dynamic Avatar answered Sep 20 '22 07:09

dynamic


From the HTML 4.01 specification, §17.2, "Controls":

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.

like image 34
Ignacio Vazquez-Abrams Avatar answered Sep 20 '22 07:09

Ignacio Vazquez-Abrams