4

I have a table with multiple inputs in separate forms which would like each user has 15 from inputs to be passed. In this case I have around 80 users in the table and I have to post all the form inputs at once to the server site.

However, I received PHP warnings about receive more than 1000 input variables and suggest to increase the max cap for the input variables length. I only receive around 75 users form inputs in this case. The form inputs are passed through jQuery method .serialize()

I wonder how to calculate the input variables count in php... I've tried to use count($request->getParams()) and it returns 15 (outer layer keys count) which my returning variables is in this way:

name => array (
    [0] => john
    ....
    [79] => Serene
),
age => array (
    [0] => 21,
    ....
    [79] => 24,
),
....

In this case, is the variable count in 15 keys times with 80 key-value pair which is 1200 variables in total? Is this the way that PHP to calculate the input variables by calculate all the incoming form inputs?

I know how to count multidimensional array but I just wonder that the PHP whether it determine the input variables length by counting the outer layer keys count or all the keys in the input variables?

(p.s. the table has to post all user information form input due to the design of the code)

3
  • use foreach,since it is multidimensional
    – Deepak A
    Commented Apr 3, 2019 at 4:38
  • @Deepak do u mean that in PHP default way to calculate the input of the variables is to count every single form input?
    – gitguddoge
    Commented Apr 3, 2019 at 4:39
  • if count() returns the outer part of array then use foreach to calculate the count of child elements
    – Deepak A
    Commented Apr 3, 2019 at 4:45

4 Answers 4

5

If posible post data in json format by serializing the form. And you can parse json on server side to get all fields. So you can post data in single parameter only

Below is jquery exapmle to serialize form data to json

var data = $('#myForm').serializeArray();
$.post("page.php", data);

On php side can get assosiateve array in key value form

$data = json_decode($_POST['data'], true);
3
  • I thought about mentioning this, so good for you... I've used this "trick" before. Commented Apr 3, 2019 at 5:01
  • hmmmm... i tried this just now but the count of the input variables still remain same as .serialize() tho...
    – gitguddoge
    Commented Apr 3, 2019 at 6:34
  • Use serializeArray() it will convert to json . serialize() method convert the form data in query string. Commented Apr 3, 2019 at 6:40
3

You mean count($_POST, true) ?

count ( mixed $array_or_countable [, int $mode = COUNT_NORMAL ] )

mode

If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array.

I suppose this is more proper:

count($_POST, COUNT_RECURSIVE)

Special attention to recursively count the array

Of course by the time you can count them you may have already exceeded the limit, which is for HashDos (array hash table collision attacks).

I watched a good webinar by the Boss man "Rasmus Lerdorf" (the guy that invented PHP) a few years ago now on it ( on HashDos not counting arrays ... lol ).

*PS - if you didn't know count could take 2 arguments, I won't say how long it took me to figure that one out....

3
  • Although not the direct answer for the question but this one really helps!
    – gitguddoge
    Commented Apr 3, 2019 at 4:51
  • btw is this how php determine the incoming variables count? instead of the outer layer keys only?
    – gitguddoge
    Commented Apr 3, 2019 at 4:52
  • 1
    @gitguddoge - I am not sure they may even count the number of hash table inserts, which may be more then one for an array item that causes a collision. Basically PHP uses a fast hashing method for arrays (for obvious performance reasons) but you can design a request that causes multiple collisions (which require more inserts in the hash tables, somehow) and can crash the server. This is the Why of the limit. But this is at the "limit" (pun intended) of my knowledge of the subject. Commented Apr 3, 2019 at 5:03
2

You can do following thing:

Sharing our experience of multiple rows having number of fields.

For every user row, add a checkbox Edit, by default unchecked.

And all of row's fields disabled.

If you want to edit user row, then click the checkbox.

Form elements from that row will be enabled.

Its not likely that user will add data to 75 to 80 rows in a form.

Therefore, only those rows will be posted which are enabled by the user.

Disabled form elements do not submit.

And the count will be most probably less than 1000.

2
  • I hope I can do that too but too bad the table has to post all the user form inputs at once due to the design of it... nice try tho (upvoted!)
    – gitguddoge
    Commented Apr 3, 2019 at 4:43
  • Another approach would be: disable all the elements and enable them when user clicks on them. Make the CSS changes so that disabled elements should not be identified. This way, only edited fields will be submitted.
    – Pupil
    Commented Apr 3, 2019 at 4:46
2

try using this way hope it will help you

$count = 0;
foreach ($array as $type) {
    $count+= count($type);
}

check this

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.