Search code examples
phpisset

How to set a parameter in PHP?


In index.php page, I have this:

<input type="hidden" name="parent" value="<?php echo GROUP_ROOT_ID; ?>">

GROUP_ROOT_ID is a macro, equaling to -1.

If in xyz.php file, we check if the parameter parent has been set or not using the below code:

if (isset($_GET['parent'])) {
  //do something
}

Why I cannot see parent has been set in xyz.php file? How can I make it be set?

Thanks all!


Solution

  • First you have to change $_GET('parent') to $_GET['parent'] second thing, to set the $_GET['parent'], the url of the page should be like this

    http://example.com/index.php?parent=123
    http://example.com/xyz.php?parent=123
    

    to use a global variable accessible from different pages, you can do it with $_SESSION

    // in index.php
    
    session_start();
    $_SESSION['parent'] = "value";
    
    // in xyz.php
    
    session_start();
    if(isset($_SESSION['parent']))
    { /* ... code ... */ }