Viewed   80 times

If you have a form containing text inputs with duplicate name attributes, and the form is posted, will you still be able to obtain the values of all fields from the $_POST array in PHP?

 Answers

4

No. Only the last input element will be available.

If you want multiple inputs with the same name use name="foo[]" for the input name attribute. $_POST will then contain an array for foo with all values from the input elements.

<form method="post">
    <input name="a[]" value="foo"/>
    <input name="a[]" value="bar"/>
    <input name="a[]" value="baz"/>
    <input type="submit" />
</form>

See the HTML reference at Sitepoint.

The reason why $_POST will only contain the last value if you don't use [] is because PHP will basically just explode and foreach over the raw query string to populate $_POST. When it encounters a name/value pair that already exists, it will overwrite the previous one.

However, you can still access the raw query string like this:

$rawQueryString = file_get_contents('php://input'))

Assuming you have a form like this:

<form method="post">
    <input type="hidden" name="a" value="foo"/>
    <input type="hidden" name="a" value="bar"/>
    <input type="hidden" name="a" value="baz"/>
    <input type="submit" />
</form>

the $rawQueryString will then contain a=foo&a=bar&a=baz.

You can then use your own logic to parse this into an array. A naive approach would be

$post = array();
foreach (explode('&', file_get_contents('php://input')) as $keyValuePair) {
    list($key, $value) = explode('=', $keyValuePair);
    $post[$key][] = $value;
}

which would then give you an array of arrays for each name in the query string.

Saturday, August 6, 2022
5

you would need 2 forms on your page, one form that posts back to index.php and a second form that posts to paypal

after the first form posts back to index.php, echo javascript in the body tag to submit the paypal form when it loads

    <?php
   if(isset($_POST['mydatafield'])){

        do database stuff

        $LOAD = 'document.paypal.submit();';
    }
    ?>
    <body onload="<?php echo $LOAD ?>">
    <form name="paypal" action="paypal.com?yadayada">
    paypal fields
    </form>
    <form name="myform" action="index.php">
    your form stuff
    submit button
    </form>
Sunday, September 18, 2022
2

Your post data is JSON so use json_decode to turn it into an array containing anobject and access the object_id property

$rawPostData = file_get_contents('php://input');
$json = json_decode($rawPostData);
$json = $json[0];
$all = date("F j, Y, g:i a") . " " . $json->object_id. "rn";
file_put_contents("Activity.log", $all, FILE_APPEND);
Monday, October 3, 2022
 
pope
 
5

I fixed this error by adding the name="fieldName" ngDefaultControl attributes to the element that carries the [(ngModel)] attribute.

Friday, August 5, 2022
4

Can you just use the full field name?

acroFields.SetField("form1[0].#subform[0].TextField2[0]", Value);
Wednesday, November 23, 2022
 
paul_b.
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :