php - Get input from HTML form as an Array and Check if there is input in the array or not -


i input values html form array this:

 <form method = "post" action = "akf.php">     enter values in array $a:<br><br>     <input type="text" name="a[one]" />     <input type="text" name="a[two]" />     <input type="text" name="a[three]" />     <input type="text" name="a[four]" />     <input type="text" name="a[five]" />     <br><br><input type="submit" value="find"><br><br>  </form> 

now input php html form , display this:

   $a=$_post['a'];    print_r($a); 

now without giving input in form (ie., empty form) if submit form,

enter image description here

it should return message "no values entered in array ".

note: want php return above message after submit form.

functions , syntax's used achieve output:

if (empty($a)) {     echo 'no values entered in array'; } else {     echo 'array'; } 

this doesn't work because: returns message "no values entered in array" when page loaded newly, when click submit button, php takes value input , returns message array.

$sum = array_sum($a); if($sum==0) {     echo " no values entered in array "; } else {     echo 'array'; } 

this syntax works when inputs numerical (numbers of type) . if enter strings in array through html forms , submit, still prints "no values entered in array" since array_sum($a); doesn't count strings.

it returns array because if don't enter empty strings submitted. if enter nothing array full of empty strings.

to check if there empty strings do:

if(!array_filter($a)) {     echo 'no values entered in array'; } else {     echo 'array'; } 

array_filter compare elements false , if false removed. if elements in array empty string returned array empty , therefore validated false.

see also: checking if array items empty php


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -