Arjun Bajaj
Arjun Bajaj

Reputation: 1962

POST without Form Method (PHP)

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?

Upvotes: 13

Views: 57701

Answers (7)

Jack Billy
Jack Billy

Reputation: 7211

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

Upvotes: 9

Lassi
Lassi

Reputation: 3940

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.

Upvotes: 0

Carbos
Carbos

Reputation: 117

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)

Upvotes: 0

Iwan Luijks
Iwan Luijks

Reputation: 486

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.

Upvotes: 5

BiAiB
BiAiB

Reputation: 14122

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 ?

Upvotes: 0

Headshota
Headshota

Reputation: 21449

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

Upvotes: 1

Wesley van Opdorp
Wesley van Opdorp

Reputation: 14941

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?

Upvotes: 1

Related Questions