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
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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

<?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';
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.