Matic
Matic

Reputation: 39

post-redirect-GET, GET? How?

Since I asked my question (Previous question) in a way no doubt most users think "dude this is tl;dr" let me put it more simple. I want to use post-redirect-get pattern to avoid user refreshing the site and resubmiting the data etc... I understand that in order to do so I have to redirect the user from html form, through php processing script and back to a new site (or original html form site) that displays the processed data.

Now my question. How do I GET my processed data back from my php? I don't understand the GET part... Currently I don't know how to show php generated data in a nice html display (view) page without include 'site.html';. This example isn't what I am looking for either: Simple Post-Redirect-Get code example. Code in the below example just redirects me to a current page.

Upvotes: 0

Views: 3701

Answers (3)

Joe Walters
Joe Walters

Reputation: 11

I hope this will help because it has taken me quite a while to get it as well. I tested my understanding like this. I have two php pages, the first page (prg1.php) sends the form to the database, action set to the second one (prg2.php). prg2.php checks the POST data, updates the database and issues a redirect to prg1.php with anything I need to pass back as GET variables. prg2.php looks like this

<?php
  if (isset($_POST['gameid'])){
    // process the data, update the database
    $gameid = htmlspecialchars($_POST['gameid']);
    $playerid = htmlspecialchars($_POST['playerid']);
    $message = htmlspecialchars($_POST['message']);



    //redirect, after updating the database
    $getinfo = '?gameid=' . $gameid . '&playerid=' . $playerid;

    header("Location: prg1.php" . $getinfo );
    exit();
  }
?>

Upvotes: 1

gifnoc-gkp
gifnoc-gkp

Reputation: 1516

You could try something like this:

/****************************************/
/*          Fetch my data.              */
/****************************************/
$mydata = $_GET["data"];

/****************************************/
/* Has to be sent before anything else! */
/****************************************/
header( 'Location: http://www.yoursite.com/index.php?data='.$mydata );

Upvotes: 0

Quentin
Quentin

Reputation: 944568

It depends on context, but for example:

Given: invoice-form.html, invoice-processing.php and current-invoices.php:

  1. User fills in data on invoice-form
  2. User submits form which has action="invoice-processing.php"
  3. Browser POSTs data to invoice-processing
  4. invoice-processing takes the data from the form and puts it in a database
  5. invoice-processing outputs 302 status and a Location header
  6. Browser goes to current-invoices
  7. current-invoices fetches a list of invoices (including the most recently submitted one) from the database and sends them to the browser as an HTML document

Upvotes: 3

Related Questions