Basic Post Redirect Get (PRG) design pattern in PHP

Post/Redirect/Get (PRG) is a web development design pattern that is used by the developers to prevent unwanted form resubmissions on their software. In software engineering, the term design pattern is an existing reusable solution to an occurring problem within a given condition in software design. It is a description for how to implement the very best solution of a certain problem in the software development.  

POST is one of the most common HTTP methods.

Some other notes on POST requests:

  • POST requests are never cached
  • POST requests do not remain in the browser history
  • POST requests cannot be bookmarked
  • POST requests have no restrictions on data length
Post/Redirect/Get Design pattern solution on succesful login

Since POST requests cannot be bookmarked by the browser, we need to implement the the post / redirect / get design pattern to prevent form submission duplicates. E.g. when a users submits a form and reloads the web pages, if not implementing PRG, the form will get resubmitted resulting in unwanted behaviour.

Below is a basic script in PHP of PRG.

//store current url into a variable  
$self = htmlspecialchars($_SERVER["PHP_SELF"]);  
//if form is submitted via POST method  
if ($_POST) {  
    //303 redirect to the same page  
    header('Location: ' . $self, true, 303);  
    //exit from the current script  
Please follow and like us:

Php price calculation using checkboxes

Hello, in this tutorial, i will show you how to calculate price in php webpage using html checkboxes.

Step 1: Create the html form

The first thing you need to do is to create the html document and make the form with the checkboxes.

<form action="index.php" method="get">
<input name="choice[]" type="checkbox" value="1" /> Chicken, Price: 8 Euros<br/>
<input name="choice[]" type="checkbox" value="2" /> Meat, Price: 10 Euros <br/>
<input name="choice[]" type="checkbox" value="3" /> Potatoes, Price: 2.50 Euros<br/>
<input name="choice[]" type="checkbox" value="4" /> Salad, Price: 5 Euros <br/>
<input type="submit" value="order" />

The above code gives the following output:

Chicken, Price: 8 Euros
Meat, Price: 10 Euros
Potatoes, Price: 2.50 Euros
Salad, Price: 5 Euros

Step 2: Create the php code

It is required to make a new index.php  file, since the form action is set to index.php. Index.php is the file we will write the below code.

        if(isset( $_GET["choice"] ))
        $food = $_GET["choice"];
        $c = count($food);
        $price = 0.0;
            if($food[$i] == 1){
                $price = $price + 8;
                echo "You have selected chicken<br>";
            if($food[$i] == 2){
                $price = $price + 10;
                echo "You have selected meat<br>";
            if($food[$i] == 3){
                $price = $price + 2.50;
                echo "You have selected potatoes<br>";
            if($food[$i] == 4){
                $price = $price + 5;
                echo "You have selected salad<br>";
        }//end for
        echo "<br>Final Price is:" .$price. "<br>";
        }//end isset
            echo 'Please choose something!';

A complete result of this tutorial is like below:

Chicken, Price: 8 Euros
Meat, Price: 10 Euros
Potatoes, Price: 2.50 Euros
Salad, Price: 5 Euros


You can watch the video tutorial on youtube.

Please follow and like us: