ChenXiu Posted June 11, 2021 Share Posted June 11, 2021 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." Quote Link to comment https://forums.phpfreaks.com/topic/312895-prg-post-redirect-get-behavior/ Share on other sites More sharing options...
kicken Posted June 11, 2021 Share Posted June 11, 2021 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. You load a form using GET After filling out the form you submit it using POST 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. Quote Link to comment https://forums.phpfreaks.com/topic/312895-prg-post-redirect-get-behavior/#findComment-1587165 Share on other sites More sharing options...
ChenXiu Posted June 17, 2021 Author Share Posted June 17, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/312895-prg-post-redirect-get-behavior/#findComment-1587286 Share on other sites More sharing options...
Psycho Posted June 18, 2021 Share Posted June 18, 2021 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. User navigates to a CREATE_INVOICE page 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. User submits the form using POST The receiving page identifies that this is a new invoice and add the applicable header records to the DB and generates an invoice ID The receiving page then performs a redirect to the CREATE_INVOICE page with a GET value for the invoice ID 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. The user will have two options. 1) Enter data into the fields for a new line item or 2) Complete the invoice 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 Quote Link to comment https://forums.phpfreaks.com/topic/312895-prg-post-redirect-get-behavior/#findComment-1587336 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.