0

HTML:

Owner: input type="text" name="owner[]" />
Category:
<select name="cat[]">
      <option value="clothes">Clothes</option>
      <option value="shoes">Shoes</option>
      <option value="accessories">Accessories</option>
</select>
Upload: <input type="file" name="image[]" />

whith function that clone the same fields when click on "+ button"

I count the POST field with:

$num = count($_FILES['image']['name']);

because i want to know how many times the end user clone the fields.

what i want is Make sure that the user has to fill all fields which he opend with "+ button" i cant check all the hidden fields i want to check just the field he opend.

so what can i do ?

i cant do like this:

$owner = $_POST['owner'][$i];
$cat = $_POST['cat'][$i];
$file = $_FILES['image'][$i];

if ($owner && $cat && $file)
   echo "bla bla bla";
else
   echo "fill all the fields!";

can anyone help me ?

thank you

1 Answer 1

3

There are some points which you need to make sure beforehand. Whenever you are using any input field's name attribute as "owner[]" or "cat[]" or "image[]", you will get an array then. But since, input file's property accessing capability is already 2D array by default, so now you will be able to access those properties as a 3D array.

When you have added a "[]" for the input file field's name attribute, you will now get the name of the 1st file as "$_FILES['image'][0]['name']", because array indices start with 0. As per your question, you can validate using the following way:-

<?php
$numOwners = count($_POST['owner']);
$numCats = count($_POST['cat']);
$numFiles = count($_FILES['image']);

// Check to see if the number of Fields for each (Owners, Categories & Files) are the same
if ($numFiles === $numCats && $numFiles === $numOwners) {
    $boolInconsistencyOwners = FALSE;
    $boolInconsistencyCats = FALSE;
    $boolInconsistencyFiles = FALSE;

    for ($i = 0; $i < $numFiles; $i++) {
        if (empty($_POST['owner'][$i])) {
            $boolInconsistencyOwners = TRUE;
            break;
        }

        if (empty($_POST['cat'][$i])) {
            $boolInconsistencyCats = TRUE;
            break;
        }

        if (!is_uploaded_file($_FILES['image'][$i]['tmp_name'])) {
            $boolInconsistencyFiles = TRUE;
            break;
        }
    }

    if ($boolInconsistencyOwners || $boolInconsistencyCats || $boolInconsistencyFiles) {
        echo "I knew that there will be some problems with users' mentality!";
        // Redirect with proper Error Messages
    }
    else {
        echo "Wow, Users have improved & have become quite obedient!";
        // Proceed with normal requirements
    }
}
else {
    echo "Something fishy is going on!";
}
?>

Hope it helps.

Sign up to request clarification or add additional context in comments.

3 Comments

I really appreciate the time you spent to write this code thank you thank you thank you so much for your interest, but unfortunately didn't help , In fact i wrote my own code before your answer and its work, Sorry for Wasting Your Time ....
@Swell - that's okay. Only thing which is required is that you are using the correct working code. Also I appreciate your humbleness. One more thing you can do is that either you can write the code as an answer & mark it as the correct one or mark my answer as the correct one. This is because other SO users will get to know that they do not need to provide any more answers to this question as it's already solved. Hope you understood my point. Thanks!
Thank you, SO users maybe they read our conversation and they will know that my problem has been soleved, thanks again.

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.