Search code examples
phphtmlformsposthttp-post

Passing data between two PHP pages


I have two pages and the first one 'order.php' is for the user to input data then the second one 'saveOrder.php' is for the save order information in the database. I have completed the order saving process by using POST method to submit form data. But I need to know how can I resend data back to 'order.php' in case saving failed (database error or user input data validation failed). Also order details should not be displayed in the url.


Solution

  • You can use sessions in order.php and 'saveOrder.php' to handle errors between them.

    saveOrder.php

    <?php
      
    session_start();
      
    $_SESSION["error"] = "an error occured";  
    ?>
    

    order.php

     <?php
          
        session_start();
          
        if(isset($_SESSION["error"]) && $_SESSION["error"] != ''){
            echo 'Error : '. $_SESSION["error"];
            unset($_SESSION["error"]);
        }
        ?>
    

    reference : https://www.php.net/manual/en/book.session.php

    https://www.geeksforgeeks.org/php-sessions/