0

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.

Community
  • 1
  • 1
Matic
  • 39
  • 1
  • 6
  • Your question is still very vague. I assume you know how to do a MySQL query to fetch data? – Halcyon Aug 02 '13 at 13:04
  • I know how to manipulate data and work with databases, I have the basic php skills. My question is simply: PRG model. PHP. How does it work? How do I retrieve processed form data from php page using GET? – Matic Aug 02 '13 at 13:07
  • once you submitted your data to database,get the primary key of that particular row and pass it to display form through url and there you fetch the data for that key and display.- something like this? – Vipin Kumar KM Aug 02 '13 at 13:10
  • @ViPiN yes that would be a nice solution but I hear you can't pass a lot of data and sensitive data through url – Matic Aug 02 '13 at 13:34

3 Answers3

4

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
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • YES! This. But how do I manage to do that last 3 points: 5,6 and 7? and more importanly can I combine invoice-form.html and current-invoices.php in one page? Preferably html for easier graphic design later on – Matic Aug 02 '13 at 13:11
  • `header("Location: http://example.com/current-invoices.php");`, http://php.net/pdo , and of course there is no requirement that `include-form.html` can't be `current-invoices.php` too (but if it is plain HTML then you can't get any data from the data base) – Quentin Aug 02 '13 at 13:12
  • Yes, we are soo close to my question now, thanks for understanding me! Now you said "(but if it is plain HTML then you can't get any data from the data base)". What if I use html tags principle? Basicly to mix php in my html, will I be able to get data from php? And how can I manage that – Matic Aug 02 '13 at 13:20
  • PHP assumes you are going to be mixing it into HTML … but if you do that then it isn't "plain HTML" and you need to put it in a `.php` file not a `.html` (or configure your server to process `.html` files as PHP). – Quentin Aug 02 '13 at 13:21
  • I could mix it before when I included html file in my php code. But now if I want a proper PRG model I can't include html in my php script anymore. I thought there is a workaround for this because I really want my display pages in html for CSS design later – Matic Aug 02 '13 at 13:30
  • PRG isn't a model (it is a pattern) and nothing about it forbids putting HTML in a PHP document. (Good design practise would be to keep your data model code separate from your template code though, but that's MVC and related things, not PRG). – Quentin Aug 02 '13 at 13:32
  • Exactly Quenting I have a good MVC running but I don't know hot to add PRG pattern to it. Currently I have separated html and php files with the use of include function in my php. If I want to make PRG pattern I can't use include because that means that forms are running inside my php script which is completely opposite to a PRG pattern – Matic Aug 02 '13 at 13:41
  • That doesn't make sense. If you are using MVC then (using the steps in my original answer for this example): You have some URLs. The URLs are mapped on to controllers. The controller interacts with your database via the model, and then puts some data in a view. If you are using PRG then the first URL goes to a controller that sends an HTML view back. The second URL goes to a controller that puts data into the model and sends a Redirect view back. The third URL goes to a controller that gets data out of the model and sends an HTML view back. – Quentin Aug 02 '13 at 13:46
  • If you are implementing views by `include()`, then just don't put an `include()` in the controller that handles the second of the URLs (or put the redirect logic in a file and include *that* instead of one containing HTML). – Quentin Aug 02 '13 at 13:46
  • I think I got it! Tell me if I am correct. You are saying that I just need to take my `include()` out of my model php script which handles all the database work and into my second URL (php file) which will be my controller for displaying all of the data onto my html. – Matic Aug 02 '13 at 14:17
  • So basicly we have HTML(form) ---> PHP(all the actual code and then redirect to controller) ---> PHP (controller, code handles displaying of data, include here) ---> HTML(same form that we started from) – Matic Aug 02 '13 at 14:21
1

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();
  }
?>
0

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 );
gifnoc-gkp
  • 1,506
  • 1
  • 8
  • 17