Question

I have a PHP script dat generates lines where you put information in. The user can set the amount of lines he wants.

When a field is not filled in, the field becomes green (works). When everything is filled in, the rows of information get send to the DB.

Problem: If I have 2 rows and the first row is filled in correctly and the second row isn't, the first line get send to the database because I programmed per row do this.

Code:

for($x=0;$x<$i;$x++){
  if (
    empty($_POST["datum".$x]) ||
    empty($_POST["campus_id".$x]) ||
    empty($_POST["lesuur".$x]) ||
    empty($_POST["klasgroep".$x]) ||
    empty($_POST["leerkracht".$x]) ||
    empty($_POST["lokaal".$x]) ||
    empty($_POST["vervanger".$x]) ||
    empty($_POST["vervanglokaal".$x])||
    empty($_POST["opmerking".$x])
    )
  {
    echo "";
  } else {

    processForm($x);
    $_SESSION['opgeslagen']=1;
    header('Refresh: 0;url=invoer.php');
  }
}

What I want to do is run my for loop, when everything in every row is filled in, THEN do the processform with the parameter in it but I can't make it work...

Any help would be appreciated, thanks!

Was it helpful?

Solution

As was mentioned before, simply use one loop to check everything, then another to process everything if the checks were all successful.

$failure = false;
for($x=0;$x<$i;$x++){
  if (
    empty($_POST["datum".$x]) ||
    empty($_POST["campus_id".$x]) ||
    empty($_POST["lesuur".$x]) ||
    empty($_POST["klasgroep".$x]) ||
    empty($_POST["leerkracht".$x]) ||
    empty($_POST["lokaal".$x]) ||
    empty($_POST["vervanger".$x]) ||
    empty($_POST["vervanglokaal".$x])||
    empty($_POST["opmerking".$x])
    )
  {
    $failure = true;
  }
}

if(!$failure) {
  for($x=0;$x<$i;$x++){
    processForm($x);
  }
  $_SESSION['opgeslagen']=1;
  header('Refresh: 0;url=invoer.php');
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top