1

I'm currently busy with PHP and thought of sending a POST request to another page without a form to do so. But how do you do this? I googled a bit and saw that cURL was used: is this the best way to go or can you do it (easy) without it?

I was thinking of making a small page where suggestions can be placed and you could decline / accept them. The suggestions are loaded from a (MySQL) database and presented along with an accept and decline button. If accept is clicked i would like to send the parameters to a page that accepts POST as in this way:

if(isset($_POST['name'] && isset($_POST['text'])) 
{
    // ...
}

I do know this can be done with $_GET, but i'd like to do it by $_POST for a change.

Sander Marechal
  • 22,978
  • 13
  • 65
  • 96
Gooey
  • 4,740
  • 10
  • 42
  • 76
  • Examples here: http://stackoverflow.com/questions/6757569/sending-data-using-post-method-without-form/6757695#6757695 – JK. Jul 09 '12 at 14:19
  • if it's really that important, use a session. AJAX still relies on the client side to support JS, and some people are still paranoid an turn it off all together. Sessions are your best bet, IMO – Elias Van Ootegem Jul 09 '12 at 14:19
  • Please, in the interest of helping other people looking for the same thing, choose one answer and mark it as accepted. Lets make a better and a more valuable resource for the community! :) – Nemanja Jul 09 '12 at 14:25
  • Internet had some trouble with me, so couldn't vote. Anyway it's up now so i've voted. @kyokasuigetsu I am loading multiple suggestions from a db with 2 buttons. The suggestions are entered somewhere else so all i have to do is click accept / decline. If i accept i would like to send it to another page which i've already created a while ago. – Gooey Jul 09 '12 at 14:38

3 Answers3

2

Use jQuery to do a post request. More information can be found here.

An example would be:

$.post('ajax/test.html', function(data) {
  $('.result').html(data);
});

If you have difficulty implementing, feel free to ask further.

Nemanja
  • 1,505
  • 12
  • 24
0

You can send Posts easily via Javascript (Ajax), see jQuery Ajax for examples. Or in PHP just use CURL, it is very easy to use.

If you need a curl-free alternative, use fopen like http://wezfurlong.org/blog/2006/nov/http-post-from-php-without-curl/

see also http://api.jquery.com/category/ajax/

Florian Brinker
  • 735
  • 7
  • 17
0

There are two acceptable methods:

  1. Sessions - Instead of using $_POST, use $_SESSION

    Reference: Manual

  2. AJAX - You can send POST requests without a form.

    Reference: AJAX tutorial / jQuery AJAX

Nadav S.
  • 2,429
  • 2
  • 24
  • 38