1

I have a simple checklist form with a number of input fields and counters to check them. Form consists of only few text fields and some radio buttons, whose value is set to either conforms or notConforms:

  • error counter ($errCounter) = counts errors like illegal input format and missing fields
  • non conformance counter ($notConforms) = checks if/how many input fields are set to notConforms. I am trying to alert the user and get their confirmation if any inputs are set to notConforms.

Two problems with the outcome of my code below:

  1. it makes two entries (duplicate) into database
  2. after database update, it does not header the user to the indicated page (inspectionbatch.php)

What is wrong with the following?

if ($_SERVER["REQUEST_METHOD"] == "POST") {
    if($errCounter == 0){ // provided there are no errors found during form validation
        // warn user if there is a non-conformance
        if($notConforms !== 0){ ?>
            <script>
                if(confirm("A not-conforms 'N/C' result has been recorded for one or more inspection criteria. If this is accurate, click OK to continue.")) {
                    <?php echo updateDatabase(); header("location: inspectionbatch.php");?>

                } else {
                    <?php echo updateDatabase(); header("location: inspectionbatch.php");?>
                }
            </script>
  <?php } else {
        updateDatabase(); header("location: inspectionbatch.php");
        }

    } else { // if errors are found during form validation, return how many errors were found
        echo "Error count: " . $errCounter;
    }

}

I also tried putting the header() function inside the updateDatabase() immediately after the syntax to update database. Database was updated fine but header() did not work...

AbraCadaver
  • 78,200
  • 7
  • 66
  • 87
Marko B
  • 13
  • 2
  • 1
    php executes on the server, javascript executes on the client. what you're trying to do is impossible - the js code will not run until LONG after php has completely finished and quit/shutdown. And beyond that, your header() calls are also useless. you cannot use header() after having sent output to the client. – Marc B May 05 '15 at 15:21
  • php is server-side. JS is client-side. Thus you can't use php once the page was sent to the user and the JS does it's magic. – DocRattie May 05 '15 at 15:21
  • @Marc B meet Marko B! – AbraCadaver May 05 '15 at 15:23

1 Answers1

1

This code doesn't work because PHP, a server-side technology, runs to completion before javascript, a client-side technology, even begins. All the PHP code will execute on your web server, and a response will be sent to the client and then all the javascript will run in the the web browser.

If you want to mix the 2, you'll have to imagine how the completely rendered dynamic result will look to a web browser.

Additionally, a call to the header() function cannot be made if any bytes have already been written to the HTTP body. From the docs:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

Asaph
  • 159,146
  • 25
  • 197
  • 199
  • I see. So just take out the notConforms check from PHP and run it in JS prior to allowing form to be submitted... – Marko B May 05 '15 at 15:35