How do you pass variables from one page to another and delete specific variables?

Discussion in 'PHP' started by Imozeb, Feb 21, 2010.

  1. #1
    I have 2 forms on my page and I want only the variables from the form that was submited to be passed on. I have to store the variables in $var format. What I want to do is to delete the variables from the form that was not submited and pass the variables from the form that was submited to the next page.
    So...

    How do you pass specific variables from one page to another and/or delete specific variables?

    Thanks in advance!

    ~imozeb :)
     
    Imozeb, Feb 21, 2010 IP
  2. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #2
    To delete a variable use unset(), or you can overwrite a variable by recalling it except making it do, something else.

    To pass it on to another page use $_REQUEST (or $_POST or $_GET, depending on the form type).
     
    danx10, Feb 21, 2010 IP
  3. susen2008

    susen2008 Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    You can pass variable from one page to another by,
    1. SESSION variable;
    2. by cookie;
    3. storing variable in database (use when security is absolute priority).
     
    susen2008, Feb 21, 2010 IP
  4. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Okay. I tried to use the PHP session command and it gave me an error "session does not exist so cant be destroyed". Why? I programmed it like this.

    page1.php:

    $username = "Mr World";
    session_start();
    $_SESSION['username'] = $username;
    //then redirect to page2.php


    page2.php:

    if isset($_SESSION['username'])
    {
    $username = $_SESSION['username'];
    }
    session_destroy();
    echo $username;
     
    Imozeb, Feb 21, 2010 IP
  5. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #5
    have to restart the session on page2 that will get rid of the error you listed.

    page2.php:
    session_start();
    if isset($_SESSION['username'])
    {
    $username = $_SESSION['username'];
    }
    session_destroy();
    echo $username;

    http://www.tizag.com/phpT/phpsessions.php
     
    shallowink, Feb 21, 2010 IP
  6. Imozeb

    Imozeb Peon

    Messages:
    666
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    It worked! Thanks guys!
     
    Imozeb, Feb 21, 2010 IP
  7. susen2008

    susen2008 Guest

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You can use cookie too. It is simple and useful.
    page1.php
    setcookie('username',$username);
    PHP:
    page2.php
    if isset($_COOKIE['username']){$username = $_Cookie['username'];}
    PHP:
     
    susen2008, Feb 23, 2010 IP