Jump to content

validating form input name = array []


delta37

Recommended Posts

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! :)

Link to comment
https://forums.phpfreaks.com/topic/105877-validating-form-input-name-array/
Share on other sites

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

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.

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.