-1

does anyone know how to perform this?

I have a link on my page, when I click that button I want to POST to another page. I do have a form on my page, but it's not in <form> tags because the data is sent to the server with AJAX/POST (it's a search function, now I also want an export function but this requires me loading a separate page), so my loop collects all the input and stores it an object.

So basically, with GET things are very easy, but how do I pass the object with POST to another page? This should thus not use AJAX, it should go to the page as if it was a normal link.

A form without <form>, is this possible?


Basically wondering if there's a function in JS like this:
window.location.url = ['export.php', $postData];

silkfire
  • 24,585
  • 15
  • 82
  • 105

2 Answers2

5

A form without <form>, is this possible?

No. Forms and XMLHttpRequest are the only ways to make post requests from a webpage (without resorting to plugins).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • you can still cherry pick the inputs and send them in a post request, but is usually easier to do this if you have a `form` to serialize first. – Flosculus Aug 09 '13 at 11:14
  • @Quentin And the difference between the "post request" you mention and the XMLHttpRequest is...? – Shomz Aug 09 '13 at 11:15
  • @Flosculus "send them in a post request" ... with an XHR. – André Dion Aug 09 '13 at 11:15
  • @Shomz — A post request is a communication over HTTP. XMLHttpRequest is an API to make communications over HTTP. – Quentin Aug 09 '13 at 11:16
  • @AndréDion Exactly. I put that comment in before this answer was updated with `without resorting to plugins`. – Flosculus Aug 09 '13 at 11:16
  • @Quentin The only option I see right now then is first sending that object to the server and storing it in the session, and then load the URL and let it read the $_POST data from the session. Well well then... – silkfire Aug 09 '13 at 11:17
  • @Flosculus — "plugins" are things like Flash or Java, you didn't mention anything like that. – Quentin Aug 09 '13 at 11:17
  • @silkfire — Sending that object (what object?) to the server … how? – Quentin Aug 09 '13 at 11:18
  • @Quentin Spelling mistake, should have been 'object'. By AJAX, of course =) – silkfire Aug 09 '13 at 11:18
  • @Quentin I was including `jQuery` – Flosculus Aug 09 '13 at 11:18
  • @Quentin And how can you send that post request without using the XHR API (this way or another)? – Shomz Aug 09 '13 at 11:18
  • @silkfire — Ajax is forbidden by the question – Quentin Aug 09 '13 at 11:19
  • @Quentin The object that contains all the query data from my form – silkfire Aug 09 '13 at 11:19
  • 2
    @Flosculus — jQuery is a JS library. It wraps XHR for its Ajax stuff. So that is just using XHR. – Quentin Aug 09 '13 at 11:19
  • @Shomz — See the original answer. You have to use a form or XHR (or go silly with Flash/Java/etc). – Quentin Aug 09 '13 at 11:20
  • @Quentin My point exactly! Damn, I just figured I addressed my first comment to you and not to flosculus, sorry for the mess! I wanted **him** to explain how will he send a post request without using XHR. – Shomz Aug 09 '13 at 11:21
  • @roasted — I wouldn't. Java Applets are far too much work with far too limited browser support. – Quentin Aug 09 '13 at 11:23
  • @Quentin ya, makes sense – A. Wolff Aug 09 '13 at 11:25
  • 2
    @Shomz Sorry, it took me a while to realize that the question contradicts itself. Forms and XHR are the only vanilla way to send POST data in HTML/Javascript. – Flosculus Aug 09 '13 at 11:29
1

Either create form on the fly and fill it with all necessary data and then post or use proxy that will convert get to post

2Shomz (since I can't comment) - yes, it's still a form, but it's another (not main) form, and it can be deleted after post. and also you can post it to iframe so to user it will work as ajax call but still doing usual post

Vitaly
  • 2,064
  • 19
  • 23