PHP checking $_POST
I have some form fields that when a form is submitted creates开发者_C百科 an array within the $_POST, I needing to check the this array has atleast 4 keys, how can I check that? I have no idea
try:
<?php
if(count($_POST) >= 4):
//Do your stuff
else:
//Do your error stuff
endif;
If you want to check an array within $_POST as apose to $_POST itself use
count($_POST['name_of_key_to_array_you_want_to_count'])
First, to make your work easier, you should change input name into array version. Something like this should work:
<input type='text' name='data[]' value='' />
Then, PHP will do it's magic and all you have to do is:
echo count($_POST['data']);
This is because your data[]
form field is changed into array.
Use array_keys
and count
:
echo count(array_keys($_POST));
Or simply:
echo count($_POST);
because keys are same in number as items.
The count()
function returns the length of an array.
精彩评论