1

I have a form that gets validated when submit. I make sure some fields are not empty, date fields have a valid format, etc. I have a block of check boxes with a text input next to it.

Option 1
<input type="checkbox" name="jobBonusId[]" value="1"  />
Option 2
<input type="checkbox" name="jobBonusId[]" value="2"  />
Option 3
<input type="checkbox" name="jobBonusId[]" value="3"  />

Amount 1
<input type="number" name="jobBonusAmount[]" step="0.01" value="0.00" />
Amount 2
<input type="number" name="jobBonusAmount[]" step="0.01" value="0.00" />
Amount 3
<input type="number" name="jobBonusAmount[]" step="0.01" value="0.00" />

So, if any of the jobBonusId[] checkbox gets checked it creates an array. Now, I want to validate the jobBonusAmount[] if its -parent- checkbox was checked and make sure is not empty or equals 0.00.

So far, I have the following code:

// Run the script
if (isset($_POST['addJobRecord']) && $_POST['addJobRecord']=='oTzm50xfm') {
   // Validate date format
   if (!validateDate($jobDateStart) || !empty($jobDateEnd) && !validateDate($jobDateEnd)) {
      // Show the form
      $displayContent = $displayForm;
   // Validate data dates
   } else if ($jobTimeIn>$jobTimeOut) {
      // Show the form
      $displayContent = $displayForm;
   // Validate bonus values
   } else if (!empty($jobBonusId) && is_array($jobBonusId)) {
      // At least one jobBonusId checkbox was checked
      // Make sure its child input is not empty

      ... HERE'S WHERE I'M STUCK ...

   } else {
      // Everything looks good
      // Add record to database
   }
}

Any ideas how to accomplish it?

Thank you much!

1
  • if you create the associative array on front end, send that array through a hidden field and validate jobBonusAmount for each jobBonusId that will be much easier. Commented Mar 8, 2020 at 7:44

1 Answer 1

1

You can use array key, and check child by jobBonusId key:

foreach ($jobBonusId as $key => $bonusId) {
    if (!empty($bonusId)) {
        if (!empty($jobBonusAmount[$key])) { // check a child
            // if child is filled
        } else {
            // not filled
        }
    }
}

It means, that $jobBonusId array is the same to $jobBonusAmount array

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

2 Comments

I already tried that, but don't know how to exit the parent else if (!empty($jobBonusId) && is_array($jobBonusId)). Look: // Start errors count $err = 0; // Loop trhu array values foreach ($jobBonusId as $id=>$val) { // Validate bonus amount if ($jobBonusAmount[$id]=='0.00') { // Add count $err++; } } // Validate errors found if (!empty($err)) { // Show the form $displayContent = $displayForm; }
@CarlosCharles if one of the cild if filled, use break. Also you can set flag (variable) before foreach, and in case at least one filled - set flag to true

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.