delta37 Posted May 16, 2008 Share Posted May 16, 2008 i've been trying to find solution to this for days now but can't seem to find one. anyways, here's the problem... from a input form, i named it into an array "<input type='text' name='contact_no[]'>" for some weird reason, every time i try to validate it in PHP, the $contact_no will always have a default value of "Array" even though the user did not fill up the input text field. i tried using this... if($contact_no == null) or if(is_null($contact_no)) but i always get a default value when i echo it, it displays the word "Array" can anyone help me here on how to validate if the array is really "null"? any help much appreciated, thanks! Quote Link to comment https://forums.phpfreaks.com/topic/105877-validating-form-input-name-array/ Share on other sites More sharing options...
cooldude832 Posted May 16, 2008 Share Posted May 16, 2008 well the obvious answer is because you forced it to be an array by using "[]" on it. Php handles post values as if they were arrays when you apply "[]" to them ie #array <input type="text" name="var1[]" /> <input type="text" name="var1[]" /> <input type="text" name="var1[]" /> #variable only <input type="text" name="var1" /> <input type="text" name="var2" /> <input type="text" name="var3" /> <input type="text" name="var4" /> so that [] should be there only if you intended it to be Quote Link to comment https://forums.phpfreaks.com/topic/105877-validating-form-input-name-array/#findComment-542606 Share on other sites More sharing options...
delta37 Posted May 17, 2008 Author Share Posted May 17, 2008 thanks for the reply. apparently I need to make it into an array because the user can enter as many contact_no as he/she wishes i'm just trying to make a fail-safe here if incase a user clicks on submit without entering any values in the contact_no[] apparently, even though there is not value, when i validate it in PHP, there's like a default value (i think it's a string) "Array" in it. Quote Link to comment https://forums.phpfreaks.com/topic/105877-validating-form-input-name-array/#findComment-543489 Share on other sites More sharing options...
cooldude832 Posted May 17, 2008 Share Posted May 17, 2008 well I still don't think you know what an array is because if you only have 1 input then the array nature of it is pointless. Either way the better solution is if(!empty($var)) Quote Link to comment https://forums.phpfreaks.com/topic/105877-validating-form-input-name-array/#findComment-543518 Share on other sites More sharing options...
delta37 Posted May 17, 2008 Author Share Posted May 17, 2008 but if the user decides to put more inputs then an array is needed right? i actually made a javascript code that will allow user to add more input forms if they wish, that's why i made it into an array to store that multiple entries. i will try this out later see if it works. thanks! Quote Link to comment https://forums.phpfreaks.com/topic/105877-validating-form-input-name-array/#findComment-543674 Share on other sites More sharing options...
sasa Posted May 18, 2008 Share Posted May 18, 2008 <?php function my_array_check($in){ foreach ($in as $a) if (trim($a)) return true; return false; } //$_POST['contact_no'] = array('123','',' '); if (my_array_check($_POST['contact_no'])) echo 'some value'; else echo 'nothing'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/105877-validating-form-input-name-array/#findComment-543994 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.