Access $_POST variables from Javascript?

R

Robert Oschler

I know there isn't any $_POST array in Javascript, it exists on the server
side accessible from PHP and other server side scripting languages. But I
knew it would let you know specifically what data I'm after.

Is there any way to get the data that is POSTED to a web page from
client-side Javascript? Or does only the server get access to it?

I can get access to the URL/href data from the "search" property, but that's
because it's part of the URL. What about POST'ed data?

Thanks.
 
L

Lachlan Hunt

Robert said:
I know there isn't any $_POST array in Javascript, it exists on the server
side accessible from PHP and other server side scripting languages. But I
knew it would let you know specifically what data I'm after.

Is there any way to get the data that is POSTED to a web page from
client-side Javascript?

No, the data isn't posted to the web page, it's posted to the server,
which typically returns a web page (or other resource) in response.
Or does only the server get access to it?

That's correct.
I can get access to the URL/href data from the "search" property, but that's
because it's part of the URL. What about POST'ed data?

No, unless that data is output by the server somewhere into the
document, in which case you could access it through the DOM.
 
M

Martin Honnen

Robert said:
I know there isn't any $_POST array in Javascript, it exists on the server
side accessible from PHP and other server side scripting languages. But I
knew it would let you know specifically what data I'm after.

If data is posted to the server and JavaScript is used on the server
(for instance in a framework like ASP) then on the server of course the
posted data is exposed to JavaScript e.g. with
Request.Form
in ASP.
Is there any way to get the data that is POSTED to a web page from
client-side Javascript? Or does only the server get access to it?

A HTTP client can make a HTTP POST request sending some data to a HTTP
server and then that data can be processed on the server, if you have
PHP on the server then you can do it with PHP, but if you have
JavaScript on the server then you can do it with JavaScript for
instance. Data does not get POSTed to a web page as such, indeed if you
make a HTTP POST request to a static resource on a HTTP server then you
usually get an error.
As for client-side JavaScript accessing POST data when a from is
submitted (e.g. when for instance the POST request is initiated) then
client-side script in the onsubmit handler can of course use the
client-side DOM to read out any form data but you are reading out the
form controls in the DOM and not a data structure the browser exposes of
data it is going to post. And when your onsubmit handler is done (and
returns true) then the browser makes the HTTP request and at that stage
script in the HTML document certainly has no access to the POSTed data.
And script in the next HTML document the server sends as the response to
the posted data again has no access to the posted data unless (as
already pointed out) of course the server-side script tries to pass on
the data to script.
 
E

|-|erc

Robert Oschler said:
I know there isn't any $_POST array in Javascript, it exists on the server
side accessible from PHP and other server side scripting languages. But I
knew it would let you know specifically what data I'm after.

Is there any way to get the data that is POSTED to a web page from
client-side Javascript? Or does only the server get access to it?

I can get access to the URL/href data from the "search" property, but that's
because it's part of the URL. What about POST'ed data?

You can read $_GET from the URL!

For posted variables you have to write the PHP variables to Javascript
variables.

<script>
<? php echo "var1 = " . $_POST("var1"); ?>

</script>

Herc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top