Home » Php » php – Same page processing

php – Same page processing

Posted by: admin July 12, 2020 Leave a comment

Questions:

How can process a form on the same page vs using a separate process page. Right now for signups, comment submissions, etc I use a second page that verifies data and then submits and routes back to home.php. How can I make it so that on submit, the page itself verifies rather than using a second page.

How to&Answers:

You can tell the form to submit to the PHP’s self, then check the $_POST variables for form processing. This method is very good for error checking as you can set an error and then have the form reload with any information the user’s previously submitted still in tact (i.e. they don’t lose their submission).

When the “submit” button is clicked, it will POST the information to the same page, running the PHP code at the top. If an error occurs (based on your checks), the form will reload for the user with the errors displayed and any information the user supplied still in the fields. If an error doesn’t occur, you will display a confirmation page instead of the form.

<?php
//Form submitted
if(isset($_POST['submit'])) {
  //Error checking
  if(!$_POST['yourname']) {
    $error['yourname'] = "<p>Please supply your name.</p>\n";
  }
  if(!$_POST['address']) {
    $error['address'] = "<p>Please supply your address.</p>\n";
  }

  //No errors, process
  if(!is_array($error)) {
    //Process your form

    //Display confirmation page
    echo "<p>Thank you for your submission.</p>\n";

    //Require or include any page footer you might have
    //here as well so the style of your page isn't broken.
    //Then exit the script.
    exit;
  }
}
?>

<form method="post" action="<?=$_SERVER['PHP_SELF']?>">
  <?=$error['yourname']?>
  <p><label for="yourname">Your Name:</label><input type="text" id="yourname" name="yourname" value="<?=($_POST['yourname'] ? htmlentities($_POST['yourname']) : '')?>" /></p>
  <?=$error['address']?>
  <p><label for="address">Your Address:</label><input type="text" id="address" name="address" value="<?=($_POST['address'] ? htmlentities($_POST['address']) : '')?>" /></p>
  <p><input type="submit" name="submit" value="Submit" /></p>
</form>

Answer:

The easiest construction is to detect whether the $_POST array is not empty

if(isset($_POST['myVarInTheForm'])) {
  // Process the form
}

// do the regular job

Answer:

you can check if it was POST request inside the page’s code and then check the data. If it was GET request – just show the form.

But please remember that is is a good practice to show successfull form submission results on a different page served through GET request, i.e. any successfull form POST should be answered with redirect to the success page.

Answer:

You could of course explore looking into AJAX requests, where you would make an asynchronous call to your handler script, and then update then update the sending page with a success message. This gives the impression of “Same page processing” i.e. The page doesn’t have to refresh.

It really depends on the effect you are trying to achieve however.

Answer:

@Michael Irigoyen: It works fine, but on first rn/load, it shows:

“Notice: Undefined variable: error in C:\xampp\htdocs\same_page.php on line 28”

How to handle this notice?

Got it now:
“Used isset, @ etc. to supress errors…”
“Works like a charm!!!”
“Now i’ll try it on my code…”

Answer:

I have saved a thank you message and refreshed using session variables.

if(!is_array($error)){
    $_SESSION['message'] = 'Thank You!';
    header('Location: yourpage.php');
    exit();
} 

and then use this in the top of the form:

if(isset($_SESSION['message'])){ 
    echo $_SESSION['message'];
    unset($_SESSION['message'];
} 

This should refresh the page and show the message and then if they refresh the page the session variable is empty so the thank you won’t show. This is called a flash message.