How to pass values between the pages in PHP?

Realize: How to pass any value between pages in PHP? Here, is an instructional exercise about passing values starting with one page then onto the next utilizing PHP.

PHP is one of the most well-known dialects with regards to Back-end. Indeed, even the CMS monster WordPress utilizes PHP at its centre, so there’s nothing more to include how significant the language is.

In any case, regularly new designers think that it’s hard to pass variables along with the consequent pages. They may even go for neighbourhood Storage to make this work, however, these hacks are not required when you can do this effectively with the session the executives.

The session is an active period where guest’s information is put away and went to the following pages. We advise the PHP mediator to begin a session by characterizing session_start() toward the start of each PHP record we need a session to occur. At that point, we get to the session variables utilizing the $_SESSION[‘variablename‘] technique.

PHP code with HTML:

<?php session_start();
      //Put session start at the beginning of the file
?>

<!DOCTYPE html>
<html>
<head>
    <title>Session Example</title>
</head>
<body>

<?php if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $_SESSION['name'] = $_POST['name'];
        
        if($_SESSION['name']) {
            header('location: printName.php');
        }
    }
?>

    <form action="session.php" method="POST">
        <input type="text" name="Name">
        <input type="submit" value="submit">  
    </form>   


</body>
</html>

Right now, are taking a content contribution to the type of name and putting away it in the name session variable. Note, this is the how the session variables are characterized, $_SESSION[‘name‘]

Next, note that we have included session_start() toward the start of each PHP record. This will guarantee that we can securely get to the variable characterized in another page, by simply utilizing $_SESSION[‘name’].

<?php session_start();
      //Put session start at the beginning of the file
?>

<!DOCTYPE html>
<html>
<head>
    <title>Print Name</title>
</head>
<body>

<p>Your Name is: <?php echo $_SESSION['name']; ?></p>   


</body>
</html>

In printName.php record, reverberating the session name variable prints the name we have inputted from a client in another page.

In this way, this is the means by which you pass variables and values starting with one page then onto the next in PHP. Offer this with your companions/individual students so they don’t need to battle.

Leave a Comment

error: Alert: Content is protected!!