Skip to content
Advertisement

Passing variables to another page with url – PHP

I want to execute some PHP code by clicking on a link and Passing variables to another page with url Pleas help me.

Such WordPress Site: https://www.armandl.com/?p=5123

Advertisement

Answer

You set $_SESSION['status'] to 0 and then set it to 1, so it will always be 1. Additionally, the link you click has nothing to do with the session vars.

Page 1:

Normally to get just one variable from one page to another via hyperlink you would add it to the URL as a query parameter and access it using $_GET:

<?php
    echo '<a href="page2.php?status=0">No</a>';

    echo '<a href="page2.php?status=1">Yes</a>';
?>

Page 2:

You also need to check if $_GET['status'] is set:

<?php
    if    (isset($_GET['status']) && $_GET['status'] == 0) {
        echo "No !";
    }
   elseif (isset($_GET['status']) && $_GET['status'] == 1) {
        echo "Yes !";
   }
   else {
        echo "NOT set !";
   }
?>

Now, if you want it to be accessible on other pages without passing in the URL, then set it as a session var:

    session_start();
    $_SESSION['status'] = $_GET['status'];
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement