passing variables values using POST

Discussion in 'PHP' started by sudhakararaog, Mar 16, 2008.

  1. #1
    i am presently passing the value of a variable to a php file using GET after data has been inserted to the database.

    ex=

    $firstname = $_POST["firstname"];

    if(!$sqlconnection)
    {
    echo "error message";
    }
    else
    {
    header("Location: thankyou.php?firstnameis=$firstname");
    }

    how can i send the value of $firstname using POST instead of GET as the value is presently appearing in the address bar. ideally i would like using POST.

    NOTE = please suggest techniques that does NOT involve javascript in order to pass $firstname using POST method, as i have done the entire validations using php and i have assumed that user has javascript turned off.

    please advice.

    thanks.
     
    sudhakararaog, Mar 16, 2008 IP
  2. live-cms_com

    live-cms_com Notable Member

    Messages:
    3,128
    Likes Received:
    112
    Best Answers:
    0
    Trophy Points:
    205
    Digital Goods:
    1
    #2
    You can have this -

    <form action="thankyou.php" method="post">
    <input type="hidden" name="firstnameis" value="$firstname" />
    </form>

    I'm not sure if there's a better method.
     
    live-cms_com, Mar 16, 2008 IP
  3. highborn

    highborn Active Member

    Messages:
    184
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    55
    #3
    one of many ways, Use php session
     
    highborn, Mar 16, 2008 IP
  4. powerspike

    powerspike Peon

    Messages:
    312
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    using php sessions


    
    session_start();
    
    if(!$sqlconnection)
    {
    echo "error message";
    }
    else
    {
    $_SESSION['firstname'] = $_POST['firstname'];
    header("Location: thankyou.php");
    }
    
    Code (markup):
    on thankyou.php
    
    print $_SESSION['firstname'];
    
    Code (markup):
    $_SESSION data is stored on the server side.
     
    powerspike, Mar 16, 2008 IP
  5. mytshans

    mytshans Peon

    Messages:
    153
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I think using session is better way...
     
    mytshans, Mar 16, 2008 IP
  6. sudhakararaog

    sudhakararaog Member

    Messages:
    73
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    41
    #6
    thanks for letting me know. i have used session and it works without adding the variable name and value in the url.

    just wanted to confirm what i am doing is correct. i have 2 files a) form.php which gathers information entered in the form and b) thankyou.php which extracts the value of firstname store in form.php as $fname and displays this $fname and a thankyou message.

    code in form.php
    ==============================================================================
    session_start(); // AS THE VERY FIRST LINE FOLLOWED BY
    session_register("firstnameis");

    $fname = $_POST["fname"];
    $_SESSION['firstnameis'] = $fname; // SOMEWHERE IN BETWEEN THE CODE
    ==============================================================================

    code in thankyou.php
    ==============================================================================
    session_start(); // AS THE VERY FIRST LINE

    Dear <?php echo $_SESSION['firstnameis']; ?> thank you for registering etc...
    ==============================================================================

    1. do i need to destroy or unregister the session variable in thankyou.php and if so can i write that unregister statement after displaying <?php echo $_SESSION['firstnameis']; ?>


    please advice.

    thanks.
     
    sudhakararaog, Mar 17, 2008 IP
  7. Ikki

    Ikki Peon

    Messages:
    474
    Likes Received:
    34
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Well, if you're not going to use that variable anymore then you can empty your $_SESSION to prevent abuse. Try this:
    
    unset($_SESSION);
    $_SESSION = array();
    
    PHP:
    That will do. You would place this snippet right after this: <?php echo $_SESSION['firstnameis']; ?>
     
    Ikki, Mar 17, 2008 IP
  8. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #8
    if you were only going to use that variable of first name once..why did you bother making it a SESSION at all?

    just use method="post" instead of method="get"
    in your <form>

    POST hides it , GET puts it into the address bar as a string

    youre going way out of your way with SESSIONS and session unsets() etc

    and why dont you just $_POST all that information to the same page ..so that you dont have to create a new page. $_POST will always post to the next page called. Whether its the same page, or another page.
    The only time it could have problems is when youre dealing with HTTPS and security that strips all incoming or outgoing data depending on its set up.

    but for what youre doing , just use a <form method="post" action="same-page-your-form-is-on.php">

    but dont name it "same-page-your-form-is-on.php
    Just name it with your form page's name.
     
    ezprint2008, Mar 18, 2008 IP