Viewed   88 times

I am having a very hard time understanding the exact process of "post/redirect/get".

I have combed through this site and the web for several hours and cannot find anything other than "here's the concept".

How to understand the post/redirect/get pattern?



As you may know from your research, POST-redirect-GET looks like this:

  • The client gets a page with a form.
  • The form POSTs to the server.
  • The server performs the action, and then redirects to another page.
  • The client follows the redirect.

For example, say we have this structure of the website:

  • /posts (shows a list of posts and a link to "add post")
    • /<id> (view a particular post)
    • /create (if requested with the GET method, returns a form posting to itself; if it's a POST request, creates the post and redirects to the /<id> endpoint)

/posts itself isn't really relevant to this particular pattern, so I'll leave it out.

/posts/<id> might be implemented like this:

  • Find the post with that ID in the database.
  • Render a template with the content of that post.

/posts/create might be implemented like this:

  • If the request is a GET request:
    • Show an empty form with the target set to itself and the method set to POST.
  • If the request is a POST request:
    • Validate the fields.
    • If there are invalid fields, show the form again with errors indicated.
    • Otherwise, if all fields are valid:
      • Add the post to the database.
      • Redirect to /posts/<id> (where <id> is returned from the call to the database)
Sunday, September 18, 2022

If the users are being redirected, then there should never be a form re-submission.

Redirect them to submitted.php and use the following:

if(isset($_SESSION['error'])){ echo'the error';}
else if(isset($_SESSION['success'])){ echo'your petition was successfully signed!');}
else{ echo 'unknown error.'; }
Wednesday, December 7, 2022

xs' would be bound to the string "ello".

xs would be bound to the string "hello".

The @ pattern lets you give a name to a variable while also matching its structure and possibly giving name to the components.

Friday, October 7, 2022

I believe there was an error.

First condition is fairly easy to understand. When LCP length == pattern length, it's done. When your pattern is even smaller than or equal to the smallest one, then only choice is the smallest one.

The second condition is wrong. We can prove it by contradiction. r < P || Wr <= a... means r >= P && Wr > a... If r >= P, then how can we have Lw = N(not found), since we already have r length common prefix?

Thursday, November 10, 2022

The pattern is to GET a blank form, modify the contents of the form, then POST that to the server, which then sends a redirect to another page which is a GET, perhaps to a page saying Form submitted successfully.. (Get->)Post->Redirect->Get

The first action is not really POST. That's the end result of completing a form and submitting it. The guide is more about what to do after that POST, as if you do not do a redirect, then the user is left on a page saying Form submitted successfully where they could just hit F5 and do another POST. With that redirect however, they're on that results page via a safe GET which will not result in a double post.

As for the implementation, you should have each be its own action on the server side. This is inline with the MVC / RESTful implementation.

  • GET /url?action=new -> Call new_form() method to render a new form
  • POST /url?action=create -> Call create_form() method to save and redirect to /url?action=show&id=1234
  • GET /url?action=show&id=1234 -> Call show_form() method to display the result
  • POST /url?action=save&id=1234 -> Call save_form() method to save and redirect

You could use 3 actions here instead if you wanted to have the 2nd action call save. Most REST/CRUD conventions use the 4, but the choice is yours. The benefits are the same as going the REST/MVC route in the first place.

See these resources as well:

  • RESTful web services
  • This covers typical conventions for RESTful controllers. It covers rails, but still applies to PHP as well if you're wanting to go the REST route.
Monday, December 26, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :