3

I hope this makes sense in relation to the title and what I'm trying to achieve, so here goes...

I have a form that displays between 1 to 30 fields to be entered - the number of fields is determined by the user at a previous stage (it will not always be the same amount).

If a user has 5 fields to fill out, they must all contain data - the same if they set 15 fields or 30 fields.

What I want to be able to do is loop through the POST variables in the form, make sure they are all set and either insert the data to the database, or display an error.

I was going to do 30 if statements with nested if statements:

if ($numberOfFields == 1){
    if (!$_POST["field1_text"]){$error = 1;}
};

if ($numberOfFields == 2){
    if (!$_POST["field1_text"]){$error = 1;}
    if (!$_POST["field2_text"]){$error = 1;}
};

But this seems a very long winded way and I was wondering if anyone had any suggestions or pointers.

I was wondering if something like this would work:

for ($q = 1; $q <= $numberOfFields; $q ++){
    if (!$_POST["field'".$q."'_text"]){
        $error = 1;
    }   
}

But I'm getting an error referencing the variable/field name using the $q. Should this be [$q] or something else?

I'm struggling to find any answers, but probably not asking the right question, but any help would be appreciated.

Thanks

1
  • In addition to concatenating $q in, you've added unnecessarily (harmful) single quotes. Also, I'd just use simple "double-quote interpolation" like so: if (!$_POST["field{$q}_text"]) {. Commented Jan 8, 2013 at 19:12

2 Answers 2

4

It could be done like this, using a foreach instead of a for:

if($_SERVER['REQUEST_METHOD'] == 'POST')
{
    $error = false;
    foreach($_POST as $key => $value)
    {
        if(strpos($key, 'field') === 0)
        {
            if($value == '')
            {
                $error = true;
                break;
            }
        }
    }

    if($error)
    {
        // not all fields have a value - show message
    }
}

It would be much easier if you used an input array on the form, instead of manually populating the input names with a number concatenated. Example:

<input type="text" name="field[]" />
<input type="text" name="field[]" />
<input type="text" name="field[]" />

On the PHP side, simply loop over them:

foreach($_POST['field'] as $field)
{
    if($field == '')
    {
        // error - doesn't have a value
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

Use this:

for ($q = 1; $q <= $numberOfFields; $q++){
    if (!$_POST["field".$q."_text"]){
        $error = 1;
    }   
}

In your own code you had weird extra '

2 Comments

2 hours of my life gone because of that - thanks for your speedy response - I thought I was on the right lines. Can you do the same to set variables? For example $var.$q = $_POST["field".$q."_text"]
@Paul For variables, I would also advise to assign them to an array like: $myvars = array(); $myvars[ $q ] = $_POST['field'.$q.'_text'];

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.