Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Post method without a Form

Tags:

post

php

get

I am that kind that spends more time looking for bugs in web projects and correct them, but I still have one question about the use of the GET and POST method

To summarize , I often use the GET method for queries that may be coming from links or simple buttons example :

<a href="example.php?n=2030004">Click me</a>

and for forms (signup,login, or comment) I use the post method. but the question is:

sometimes (Multi-steps signup for e.g), I may need to pass the info collected from the form in page 1 to a page 2 (where the user can find a captcha for e.g). in order to send them to the database if the captcha test is Okey. But the question is , how to pass those info to a next page via a POST method without using a hidden form? Do I need to recreate a POST method from scratch with a socket?

thank you

like image 340
SmootQ Avatar asked Dec 13 '10 16:12

SmootQ


1 Answers

You can use JavaScript (jQuery):

First u need to load jQuery ( using google as host or you download it):

<script type="text/javascript" src="http://jqueryjs.googlecode.com/files/jquery-1.3.2.js"></script>

Then...

    <script type="text/javascript">
    $(document).ready(function() {
         $('#Link').click(function() {
              $.post("example.php", { n: "203000"} );
         });
    });
    </script>

<a id="Link" href="#">Click me</a>

Edit:

after that save it in the SESSION in example.php

$ _SESSION['N'] = (int) $_POST['n'];

When this value will be stored on the server side. And tied to the client session, until he closes browser or that it set the time for that session on the server side runs out.

Edit2: There is also another possibility to post requst, yes .. But I do not like this method myself ... And here is the form used, something the OP did not want.

<form name="myform" action="example.php" method="POST">
    <input type="hidden" name="n" value="203000">
       <a id="Link" onclick="document.myform.submit()" href="#">Click me</a>
</form>
like image 180
eriksv88 Avatar answered Sep 24 '22 05:09

eriksv88