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  
    exit;  
} 
Please follow and like us:

Διαγωνισμός στο Facebook για δωρεάν ιστοσελίδα

Όροι Διαγωνισμού

  1. Κάντε Like την σελίδα μας στο facebook εδώ: https://goo.gl/FrCxfo 
  2.  Γράψετε στα σχόλια του διαγωνισμού στο facebook την σωστή απάντηση
  3.  Συμπληρώστε την φόρμα εδώ και μπείτε στην κλήρωση όπου 2 διαγωνιζόμενοι θα κερδίσουν δωρεάν από μία επαγγελματική ιστοσελίδα (αξίας €450).

Η φιλοξενία της ιστοσελίδας €120/χρόνο δεν συμπεριλαμβάνεται.

Οι νικητές θα ανακοινωθούν την Κυριακή 16/09/2018 σε αυτή την σελίδα. Καλή επιτυχία!

 

Περισσότερα για εμάς θα βρείτε στο https://webdominar.xyz

 

Η σωστή απάντηση είναι 15.

Ο νικητής του διαγωνισμού ‘δωρεάν ιστοσελίδα’ που κέρδισε δωρεάν website (αξίας €450) είναι:

Loucas Neophytou

Παρακαλούμε όπως ο νικητής επικοινωνήσει μαζί μας για να παραλάβει το δώρο του.

Please follow and like us:

How to setup Angular 6 with Node.js and create the first web app

In this tutorial i will explain how to setup angular 6 with node.js and specifically to use the  Angular cli – a command line interface to scaffold and build angular apps using nodejs style (commonJs) modules. Firstly we need to install node.js. Node.js is a runtime environment that comes with npm (node package manager). Npm manages the different dependencies an Angular project has. The below article will explain step by step the whole procedure.

  • Download and install Node.js

The very first step, if you haven’t already done before, is to download and install node.js on your computer. You can download the latest version from node.js official website. Once you have installed it, you will be able to run npm commands directly from command prompt to  install Angular cli packages

Continue reading How to setup Angular 6 with Node.js and create the first web app

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" />
</form>

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.

<?php
        if(isset( $_GET["choice"] ))
        {
        $food = $_GET["choice"];
        $c = count($food);
        $price = 0.0;
        
         for($i=0;$i<$c;$i++){
            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
        else{
            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: