Can I use $_POST without using a form?

Discussion in 'PHP' started by Jessemn, Oct 19, 2008.

  1. #1
    I wish to pass a variable between pages using $_POST (not because of security, I just dont want it to be part of the url).

    Is there a way I can do this without using a <form>?

    For the moment, I've given up and am just using $_GET, but it makes the url a little ugly.
     
    Jessemn, Oct 19, 2008 IP
  2. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #2
    <form method="POST">
     
    Kyosys, Oct 19, 2008 IP
  3. joebert

    joebert Well-Known Member

    Messages:
    2,150
    Likes Received:
    88
    Best Answers:
    0
    Trophy Points:
    145
    #3
    You could probably rig something up with AJAX to keep things off of the URL, but you'ld technically still be using a <form> even though it would be a script-generated <form>. Probably more trouble than it's worth really.
     
    joebert, Oct 19, 2008 IP
  4. JAY6390

    JAY6390 Peon

    Messages:
    918
    Likes Received:
    31
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Unless you are changing values on the actual page, why not just use sessions?
     
    JAY6390, Oct 19, 2008 IP
  5. Jessemn

    Jessemn Peon

    Messages:
    263
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #5
    I guess because I'm still rather new to PHP (and programming in general) and dont know very much about sessions... I guess this is a perfect time to learn though. Thanks for the help.
     
    Jessemn, Oct 20, 2008 IP
  6. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #6
    in a nutshell, it's just placing session_start(); on top of your pages and then using $_SESSION
     
    Kyosys, Oct 20, 2008 IP
  7. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #7
    make your variable a global variable

    $_SESSION(), $GLOBALS, etc..
     
    bartolay13, Oct 20, 2008 IP
  8. 1368633

    1368633 Banned

    Messages:
    305
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    130
    #8
    cURL
    <?php
    $url = "url here";
    $data = "field1=1&field2=2";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
    $return = curl_exec($ch);
    curl_close($ch);
    ?>
    Code (markup):
     
    1368633, Oct 20, 2008 IP
  9. EricBruggema

    EricBruggema Well-Known Member

    Messages:
    1,740
    Likes Received:
    28
    Best Answers:
    13
    Trophy Points:
    175
    #9
    Or use get method for submitting forms!
     
    EricBruggema, Oct 20, 2008 IP
  10. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #10
    @1368633: Yeah.. that's not going to help much with what they're asking. Submitting some POST data to some URL through the back-end won't help them keep variables for the current user as they browse pages.

    @EricBruggema: The OP stated that they do not want to use forms and that they want to avoid using GET because it looks ugly. Why would they want to combine both methods they're trying to avoid!

    As JAY6390 and Kyosys said, go with session variables. Very easy. Just make sure you call the session_start() function before ANY data gets output, otherwise you'll get a "Headers already sent" parse error. Then when you want to store a value that you can easily grab the next time they load a page (if the session hasn't expired), you'd just do $_SESSION['variable_name'] = "value"; and can access it with the same method. It's basically like using setcookie() and accessing it with $_COOKIE[''] since it technically IS setting a cookie on their side (just to say what their session ID is; the actual values are stored server-side so no worries about any important data being peeked through by your users unlike with using actual cookies!).

    Good luck.
     
    zerxer, Oct 22, 2008 IP
  11. elias_sorensen

    elias_sorensen Well-Known Member

    Messages:
    852
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    110
    #11
    Why not just use a session, and clear it after you have used it?
     
    elias_sorensen, Oct 23, 2008 IP
  12. salahsoftware

    salahsoftware Peon

    Messages:
    63
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    I will help you in simple manner:

    start the session by-
    session_start(); //this line should be the first line of the code

    pass the values to a session variable-
    $_SESSION['login']="usertest";

    now use the session variable on another page-
    $x=$_SESSION['login']; //remember to start the session before this line.

    Regards
    Shishir Gupta
    Salah Software
     
    salahsoftware, Oct 23, 2008 IP