Home » Php » PHP $_POST and id only, no name

PHP $_POST and id only, no name

Posted by: admin July 12, 2020 Leave a comment

Questions:

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.

How to&Answers:

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.

Answer:

Standard behavior. Always use name within form.

Answer:

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.

Answer:

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