13

Is there any way to pass stuff from one page to another using the POST method without using forms.

Like in get you can just append a ? with whatever you want to send. Can you do something for post.

I also read one other thread which said to use sessions. But sessions are saved on the users computer as cookies, and cookies are unsecure.

So is there any other way to do it? Like describe something in the first page and then pass it to the second page.

Also it would be good if anyone could explain me how does the POST method work? And how does it pass data?

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Arjun Bajaj
  • 1,932
  • 7
  • 24
  • 41
  • 2
    you can use GET, or better you can make use of jQuery so you don't have to create a form. jQuery has a robust ajax functionality that can make it easier for you – Ibu Apr 22 '11 at 08:22
  • Do you mean to make the request from the user's browser, or from your script? – Pekka Apr 22 '11 at 08:23
  • 2
    Why are cookies insecure? It seems that sessions would be the right solution here. Only the session **ID** is stored in a cookie. The data is stored on the server. How POST works: http://en.wikipedia.org/wiki/HTTP_POST Basically, instead of appending the data to the URL, it is sent in the body of the request. You have to explain what you actually want to do so that we can help you. – Felix Kling Apr 22 '11 at 08:24
  • 1
    In short: no. But why don't you wan't to use POST? – Niklas Apr 22 '11 at 08:24
  • GET pass parameters via request url, POST pass parameters via request body (even after headers). You can't simply form POST request without forms. Use Jquery or pure javascript for that. Also session are NOT saved on users computer -only session id. – Emmerman Apr 22 '11 at 08:25
  • Please explain in more detail what you want to do :) – Markus Hedlund Apr 22 '11 at 08:25
  • @Ibrahim i can't use GET to pass data like passwords and some other confidential stuff. @Pekka i require both, but mainly my script – Arjun Bajaj Apr 22 '11 at 08:29
  • @ArjunBajaj: Ok, this gets more interesting. What passwords are we talking about? If you make a login, then obviously you would need a form. If you are just talking about data passing around pages, then sending the passwords to the client just to sent them to another page is potentially insecure. Using sessions is the only solution here, as you never expose confidential data directly. – Felix Kling Apr 22 '11 at 08:37
  • This is not accurate: "sessions are saved on the users computer as cookies, and cookies are unsecure". Session data is stored *on the server*. The cookie on the client merely contains a reference to the active session. In this regard, sessions are secure. – crishoj Apr 22 '11 at 08:27
  • sorry, i meant that its reference was saved and can be hijacked? right? but wouldn't it be good to not depend on the user to process data in your application? – Arjun Bajaj Apr 22 '11 at 08:31
  • @ArjunBajaj: Of course it can be hijacked in an open environment. But then every data you sent from and to the client can be hijacked as well. Think about it: If an attacker can sniff the cookie data out of the HTTP request, what prevents him from sniffing the POST data out of the HTTP request? – Felix Kling Apr 22 '11 at 08:33

7 Answers7

9

You should use jQuery to this. You can use its ajax() function. Visit the link below and read the full description of it with the functions list to help you out.

Here is a sample code for you:

<hmtl>
    <head>
        <script type="http://code.jquery.com/jquery-1.5.1.min.js"></script>
    </head>
    <body>
        <div class="my-fake-form">
            <input id="posting-value-1" type="text" />
            <a id="submit-form-link" href="#submit">Submit this Div!</a>
        </div>
    </body>
</html>

Ajax code:

function fake_form_submit ()
{

    var post = $('input#posting-value-1').val();

    $.ajax({
    'url': 'your-php-file.php',
    'type': 'POST',
    'dataType': 'json', 
    'data': {post: post},
    'success': function(data)
     {
         if(data.finish)
         {
            $("div.my-fake-form").attr("innerHTML","Form Submited!");
         }
         else
         {
            $("div.my-fake-form").attr("innerHTML","Form Not Submited!");   
         }
     },
     beforeSend: function()
       {
            $(document).ready(function () {
                $("div.my-fake-form").attr("innerHTML","Loading....");
            });
       },
        'error': function(data)
        {
          $(document).ready(function () {
            $("div.my-fake-form").attr("innerHTML","ERROR OCCURRED!");
          });
        }
      });
}

$(document).ready(function () {
    $('a#submit-form-link').click(function (e) {
       e.preventDefault();
       fake_form_submit();
    });
});

PHP:

<?php
$post = $_POST['post'];

//Do Something with the value!

//On Succes return json encode array!
echo json_encode(array("finish" => true));
?>

AJAX documentation
AJAX function documentation
AJAX tutorial

Al.G.
  • 4,327
  • 6
  • 31
  • 56
Jack Billy
  • 7,141
  • 6
  • 27
  • 38
5

Just use sessions.

The storage of sessions in a cookie is as indicated by the answers above limited to the ID of the session. Cookies can even be configured to be sent as HTTP-cookies only, if you're security-minded.

On the requesting page, set the session key-value pair you want. On the requested page request the previously set session key (check if it exists first). In you're php.ini you can choose to use session cookies as HTTP-only so you have more security.

Furthermore: posting data can be as simple as using AJAX or cURL as described above also.

Iwan Luijks
  • 486
  • 2
  • 3
1

Server side you can achieve this using cURL extension. Tutorial: here I'm not 100% sure you are looking for a server-side solution though?

Wesley van Opdorp
  • 14,888
  • 4
  • 41
  • 59
1

you can use Ajax requests to send POST requests to the server. or cURL on the server side.

Headshota
  • 21,021
  • 11
  • 61
  • 82
0

For send request in POST from a page, you can do with JQuery.post (in this case, you must use the library) or cURL (in php)

Carbos
  • 117
  • 8
0

One way is to use JavaScript to submit an invisible form. HTML:

<button id="my-button">Go to other page</button>
<form id="my-form" method="POST" action="/other-page" style="display: none">
  <input type="hidden" name="param1" value="value1">
  <input type="hidden" name="param2" value="value2">
</form>

JavaScript (jQuery):

$("#my-button").click(function(e) {
    $("#my-form").submit();
});

You could also use a link (<a> tag) instead of a button, etc.

This will make the browser go to the other page. If you want to stay on the current page and just post some data in the background, use the ajax functions in jQuery or similar libraries.

Lassi
  • 3,522
  • 3
  • 22
  • 34
0

You'll need some javascript if you want to POST from the browser and make it look like a link:

you can use submit() function . see http://www.javascript-coder.com/javascript-form/javascript-form-submit.phtml for example.

but if you want it to act like GET, why not use GET ?

BiAiB
  • 12,932
  • 10
  • 43
  • 63