Same page processing
Asked Answered
E

6

7

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.

Exceptive answered 24/1, 2011 at 14:51 Comment(2)
why not just include that second page?Gladisgladney
its sort of messy work, dont' you think? It will look neat if its on the same page.Exceptive
D
14

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>
Democracy answered 24/1, 2011 at 15:3 Comment(9)
Thanks Michael. Just the thing i was looking for. However, instead of saying thank you for your submission, how can i refresh that page so that the page can load the new information. Can't do Ajax at this point, new to the programming game!Exceptive
If you process the form before fetching the data (As I suggested), the new data will be automatically taken into account.Straticulate
@Exceptive I don't understand your question. The method above is the exact model I use in 90% of my forms and is pretty much an extended example of what @NeqO has provided. What are you trying to accomplish in terms of "refreshing" the page?Democracy
@michael instead of printing a message how can i refresh the page for my comments page so that the recently submitted comment can show after refreshing.Exceptive
@Exceptive I'm sorry, I still doesn't understand what you're trying to accomplish. My answer solves the question you have posed. If you're trying to do something more, could you please rephrase or add to your question with your code so we can try to further assist you?Democracy
OK. You know how the code says "thanks for your submission" if the form is completed successfully. So how can i auto refresh the page besides just showing a thank you note. Sorry for the confusion.Exceptive
@Exceptive You could theoretically use a PHP header("Location: yourpage.php"); (if there's nothing being printed before this line takes place). This would skip the whole "Thank you" message and take them directly to another page. If you want to display the "Thank you" message briefly, then refresh/redirect, you could echo a <meta http-equiv="refresh" content="##" /> or <meta http-equiv="refresh" content="##;url=http://domain.com/yourpage.php" /> where ## is time in seconds before the refresh/redirect.Democracy
Change your action to action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" to be safeMaryrosemarys
I have saved a thank you message and refreshed using session variables and then use echo if(isset($_SESSION['message'])){ $_SESSION['message'];unset($_SESSION['message'];}Headmaster
S
7

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

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

// do the regular job
Straticulate answered 24/1, 2011 at 14:56 Comment(0)
P
2

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.

Psychomancy answered 24/1, 2011 at 14:56 Comment(2)
because when you show regular page in response to POST, the data will be resent on page refresh. Most browsers will warn user about that (and most users will be really confused with this warning), some, like Opera, will quietly resend all POST data. In any case, that would most probably be not the desired behaviour. That's why I recommend to redirect to the results page if the request went successdully. It is OK though to show validation results right after the POST, if there was anything wrong with the data and ask the user to fix its input.Psychomancy
:-) The only reason I asked is because when you are learning it is often helpful to understand why a particular comment is made.Coligny
C
1

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.

Coligny answered 24/1, 2011 at 15:2 Comment(2)
Thanks Andy. Tried implementing Ajax. Couldn't get my head aroudn it.Exceptive
There are loads of good resources online, a site i would recommend would be net.tutsplus.com - loads of video walkthroughs to help you along.Coligny
U
0

@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..."

Uchida answered 25/7, 2011 at 5:42 Comment(0)
H
0

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.

Headmaster answered 1/6, 2014 at 19:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.