Check out Scrivo

Do you want to try out Scrivo? Then here's a demo for you that does not just get your feet wet but lets you plunge right in.

Contact us

For more information, please contact us. We're happy to help you out!

Next Nov 3 Previous

Double posting?

Suppose we have the following Web form:

<form action="report.php" method="post">
        Value 1: <input type="text" name="value" />
        Value 2: <input type="text" name="value" />
        <button type="submit">Send</button>
</form>

And the following script to process the data:

<?php

echo $_POST["value"] . " " . $_POST["value"];

?>

What will it display in your browser if you fill in the values "double" and "post"?

A: "double post"
B: "Array Array"
C: "double double"
D: "post post"
E: An error

Answer

To answer this question it is best to look at the actual data send to the server. The complete request might look like:

POST /report.php HTTP/1.1
Host: www.scrivo.org
Connection: keep-alive
Content-Length: 23
User-Agent: The browser
Content-Type: application/x-www-form-urlencoded
Referer: http://www.scrivo.org/double_post.html
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8

value=double&value:post

So both values are sent to the server. But when PHP starts filling the $_POST superglobal it will first make an entry named "value" for the string "double" and directly after that it will use the same key to store "post". So $_POST["value"] will be overwritten by "post". And echoing this value twice will result in "post post" being printed. So answer D is correct.

You can use the same name for multiple input elements, but you'll have to include array brackets in the element names (e.g. name="value[]"). If you'd done that the entry set in the $_POST array will be an array with the values (and the correct answer would have been B).