0

I have this (note that the text box is an array)

echo "<td><input style='width:60px' type='text' name='allocas[]' id='vtext' class='sc_two' size='80' maxlength='5'></td>

then in a validation code I have the following;

if(isset($_POST['Save']))
{
    if($_POST['allocas']=='')
    {
    echo "<table border='1'><tr><td>mikies</td></tr></table>";
    echo "Empty field";} 
    else 
    {
    echo "<table border='1'><tr><td>mikies</td></tr></table>";
    echo "Saved!";}
}

My issue is ($_POST['allocas']=='' doesn't seems to be doing the job as it doesn't validate and irrespective whether I have a value in allocas or not it goes to 'else' validation and prints Saved!.

If I change the text box from an array to a standalone text by changing the name to name='allocas' instead of name='allocas[]' then it works. So the issue is my validation of array values isn't correct and struggling to find an alternative approach. can you help?

Note : All these are not hard coded and dynamically populated. (live inside an echo tag)

Thanks

5
  • Just out of curiosity: why would you make a text field an array? I've never done that so may be it has its use and it would be nice to learn about this. Commented Jul 23, 2013 at 17:06
  • Assume I get to pick a value from a drop down which carries series of numbers from a database and upon selection, you got to print xxx amount of text boxes in the UI Commented Jul 23, 2013 at 17:14
  • Thanks but i still fail to see how picking a value from a dropdown (probably a <select>) transform into turning a text box into an array. I mean just a good old loop could do that. Anyways, if you ever write an article about this (advantanges/disadvantanges) please consider updating this question! Commented Jul 23, 2013 at 17:23
  • Mine is actually a more complicated story. But I get your point. For me that option however doesn't work. I tried. :) Commented Jul 23, 2013 at 17:34
  • Well, at least thanks for opening my eyes to any new possibility! :-) Commented Jul 23, 2013 at 17:37

6 Answers 6

1

Using allocas[] as the field name forces PHP to treat that as a potentially multi-valued field, and it will create an array. This array will be created regardless of how many actual fields come in with that name.

When you do

$_POST['allocas'] == ''

you're actually comparing an ARRAY to a string. Since that comparison makes no sense, PHP will type-cast the array to the raw string Array (literally the letters A, r, r, etc...), which evaluates to false.

You need to count how many values are in the array, then check those individual values for "emptiness", e.g.

if (count($_POST['allocas']) > 0) {
   ... got some values
   foreach($_POST['allocas'] as $value) {
      ... test $value
   }
}
Sign up to request clarification or add additional context in comments.

Comments

0

change

name='allocas[]' 

to

name='allocas' 

in your form/input.

Comments

0

If your input field is an "array" you can't check it afterwards like a string.

if(isset($_POST['Save']))
{
    if(count($_POST['allocas']) == 0)
    {
        echo "<table border='1'><tr><td>mikies</td></tr></table>";
        echo "Empty field";
    } else 
    {
        echo "<table border='1'><tr><td>mikies</td></tr></table>";
        echo "Saved!";
    }
}

Comments

0

If you want to try whether the value is empty try follwing

if (empty($_POST['allocas']))

Comments

0

To check if there is a value for something in PHP, you can use the empty function. I would also recommend checking that the value is indeed an array (you never know what people will do to your form)

if(isset($_POST['allocas']) && is_array($_POST['allocas']) && !empty($_POST['allocas'])) {
    //Yay! Something is here
}

References

is_array

empty

Comments

0

You should iterate over each of the POST values and validate each:

// Sanity check
if(is_array($_POST['allocas'])) {
  foreach($_POST['allocas'] as $oneTextFieldsValue) {
    // Validate $oneTextFieldsValue...
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.