Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

POST without Form Method (PHP)

Tags:

html

post

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?

like image 474
Arjun Bajaj Avatar asked Apr 22 '11 08:04

Arjun Bajaj


People also ask

Why use$_ POST in PHP?

PHP $_POST is a PHP super global variable which is used to collect form data after submitting an HTML form with method="post". $_POST is also widely used to pass variables.

How to use POST method in PHP?

The POST method transfers information via HTTP headers. The information is encoded as described in case of GET method and put into a header called QUERY_STRING. The POST method does not have any restriction on data size to be sent. The POST method can be used to send ASCII as well as binary data.

How to use POST variable in PHP?

The $_POST variable is an array of variable names and values sent by the HTTP POST method. The $_POST variable is used to collect values from a form with method="post". Information sent from a form with the POST method is invisible to others and has no limits on the amount of information to send.

What is PHP POST request?

In the PHP POST method, data from HTML FORM is submitted/collected using a super global variable $_POST. This method sends the encoded information embedded in the body of the HTTP request and hence the data is not visible in the page URL unlike the GET Method.


1 Answers

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

like image 135
Jack Billy Avatar answered Oct 30 '22 12:10

Jack Billy