Jump to content

PRG "Post-Redirect-Get" behavior


ChenXiu

Recommended Posts

I understand PRG ("post redirect get") as follows:

"index.php" welcomes visitor.
Visitor adds data, clicks submit button.
A new "Post page" appears and a Chart is displayed.
Visitor continues adding data to this "post.php" page via the "Add New Data" button
(... as visitor adds data, "post.php" continues posting to itself, and displays updated Chart. )
When visitor is finished, visitor clicks the "Finished" button.

if(isset($_POST["finished"])) {
// insert all data into mySQL for upcoming INVOICE;
header("location:thank-you.php",true,303);
exit;
}

The "thank-you.php" page retrieves and displayes the mySQL data as an invoice.

QUESTIONS:
1.) Anything I could do better?
2.) When visitor is on the "thank-you.php" page, reloading (F5) simply reloads the page without resubmitting data.
This is good, right? (I understand that's the purpose of PRG, avoiding multiple invoices and resubmittal of data)
3.) BUT....if visitor clicks the Browser Back Button, browser displays "Document Expired: document no longer available."
Is this supposed to happen? Is this normal behavior?

Thank you, as always, I appreciate the help and the "pushes in the right direction."

 

Link to comment
Share on other sites

1 hour ago, ChenXiu said:

3.) BUT....if visitor clicks the Browser Back Button, browser displays "Document Expired: document no longer available."
Is this supposed to happen? Is this normal behavior?

It is if you're doing multiple posts like you are describing, but that's not how this is typically supposed to work.  The typical sequence only involves one POST.

  1. You load a form using GET
  2. After filling out the form you submit it using POST
  3. The server saves the data from the POST and issues a redirect to another page.

The basic idea is that your POST request returns a redirect, not a page to be displayed.  If your POST request returns a page to display then trying to refresh that page requires re-sending the POST request, data and all.

Link to comment
Share on other sites

On 6/11/2021 at 12:15 PM, kicken said:

It is if you're doing multiple posts like you are describing, but that's not how this is typically supposed to work.

Thank you, I appreciate your answer. For fun, I made it so each post turned into a get using $_SESSION["post"] = $_POST, and then $_POST = $_SESSION["post"], and indeed I could "go back" but it was disconcerting how every page was basically a redirect. I decided to let it be like it is. If the visitor feels like they have to hit the back button, it really means I need to do a better job with placement, positioning, etc., to keep visitor moving forward.

Link to comment
Share on other sites

So, as I understand it, the user is adding multiple line items for an invoice and THEN you redirect at the end. Why? Here is what I suggest you do.

 

  1. User navigates to a CREATE_INVOICE page
  2. On first entry there will be no GET value for the invoice ID, so display whatever initial instructions/content to be displayed to the user. E.g. it could provide a form for vendor name/address, PO number, etc.
  3. User submits the form using POST
  4. The receiving page identifies that this is a new invoice and add the applicable header records to the DB and generates an invoice ID
  5. The receiving page then performs a redirect to the CREATE_INVOICE page with a GET value for the invoice ID
  6. The CREATE_INVOICE page detects the invoice ID and displays the current invoice data (including any line items already added), The page also displays a form to add a new line item.
  7. The user will have two options. 1) Enter data into the fields for a new line item or 2) Complete the invoice
  8. If the user enters a new line item, it sends the form data (using POST) to the processing page which adds the new line item and then performs a redirect using the invoice ID as a GET value. This takes the logic back to step 6 and will repeat indefinitely until the user decides to complete the invoice
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.