multi-dimensional array post from form

后端 未结 3 1962
[愿得一人]
[愿得一人] 2020-12-03 05:58

I want to know how i can post a multi-dimensional array?

Basically i want to select a user and selected user will have email and name to sent to post.

So sel

相关标签:
3条回答
  • 2020-12-03 06:20

    You can name your form elements like this:

    <input name="users[1][name]" />
    <input name="users[1][email]" />
    <input name="users[2][name]" />
    <input name="users[2][email]" />
    ...
    

    You get the idea...

    0 讨论(0)
  • 2020-12-03 06:20

    Here's another way: serialize the array, post and unserialize (encrypting optional).

    And here's an example that worked for me:

    "send.php":

    <input type="hidden" name="var_array" value="<?php echo base64_encode(serialize($var_array)); ?>">
    

    "receive.php":

    if (isset($_POST['var_array'])) $var_array = unserialize(base64_decode($_POST['var_array']));
    

    With that you can just use $var_array as if it were shared between the two files / sessions. Of course there need to be a <form> in this send.php, but you could also send it on an <a> as a query string.

    This method has a big advantage when working with multi-dimensional arrays.

    0 讨论(0)
  • 2020-12-03 06:21

    Well, you are going to have to do some looping somewhere. If you name each form element with an index (as Franz suggests), you do the looping on the PHP side.

    If you want to use Javascript to do the looping, have your form onSubmit() create a JSON string to pass to the PHP. Then have the PHP retrieve it like so:

    json_decode($_POST['users'], true);
    

    The second argument tells it to make arrays instead of anonymous objects.

    0 讨论(0)
提交回复
热议问题