...

View Full Version : Passing javascript variable to PHP



xsalt
09-17-2011, 12:45 AM
I have a question:

Since Javascript is client-side and PHP is a server-side, and HTTP is a "stateless" protocol, therefore the two languages cannot directly share variables. Would it be possible to pass a variable from Javascript to PHP?

kbluhm
09-17-2011, 03:12 AM
You could try loading it via an Image object (untested, I'm no JS guru):


<script>
bar = 'something';

img = new Image();
img.src ='/file.php?foo=' + encodeURIComponent( bar );
delete img;
</script>

...or if you have some AJAX magic happening, sneak it through as an additional parameter.

BluePanther
09-18-2011, 12:02 PM
It's not possible, directly. You can use AJAX to make them 'interface', but that's about it. AJAX will send a request to a file using either GET or POST, which you'll be familiar with in PHP. So, if you had a script to handle the ajax sent information, you'll be able to interface with the JS based on the return from the PHP script.
Indirectly, you can set up a JS redirect with the information in the URL (via GET), which you can pick up in PHP ($_GET). It does mean you'll leave the current page the JS executed on, and move on to the processing script (maybe that's what you're looking for anyway).

In a very loose sense, you can 'pass' a PHP variable into JS without AJAX. I say very loose, because you're not actually talking to the JS in real time - you output the actual JS with PHP, echoing the relevant parts. You're generating the JS code to run, as opposed to actually running the two in real time and 'passing' a variable.

@kbluhm, I don't think the image would work. As the output from the PHP script would have to be an image content-type (I'm pretty sure) for the JS to interact with it. For your method, it would be much easier to just set up a JS redirect like I said above, or AJAX like you touched on. No need to try and sneak it in an image.

kbluhm
09-18-2011, 07:02 PM
@kbluhm, I don't think the image would work. As the output from the PHP script would have to be an image content-type (I'm pretty sure) for the JS to interact with it. For your method, it would be much easier to just set up a JS redirect like I said above, or AJAX like you touched on. No need to try and sneak it in an image.

I'm pretty sure an image-related content type makes no difference, since you can deliver an image with a content type of application/octet-stream, etc. All you're doing is creating a HTTP request, I doubt client-side JavaScript gives a rats rump about the server-side content type. :)

So since you brought it up, this is now tested and confirmed. It worked in all browsers I cared to test it in: IE, FF, Chrome, Safari, and Opera. :thumbsup:

index.html


<script>
bar = 'something';

img = new Image();
img.src ='./file.php?foo=' + encodeURIComponent( bar );
delete img;
</script>


file.php


<?php

file_put_contents( './data.txt', $_GET['foo'] );


data.txt, after viewing index.html

something

BluePanther
09-18-2011, 11:32 PM
Haha I'll reword :P. My main, admittedly ambiguous, point was that your method would render the value useless unless you were writing it to a file, or assigning the value to a database or something similar. It would provide no return (to my knowledge) whereas a redirect would take you to the script itself, or an AJAX request would provide a return based on the input (potentially the best option).

Depends on what the OP wants.

kbluhm
09-19-2011, 12:43 AM
Once again you're not re-wording; you're changing. You said the image wouldn't work... then after seeing it does you said let me reword. Am I confused or are you? :)

BluePanther
09-19-2011, 08:12 AM
What I mainly meant is that it doesn't offer any return. Your image will pass a value into the PHP to do whatever, but it doesn't give an opportunity for return values. Whereas AJAX would, and a redirect would run the script (obviously) through navigation.

However the whole meaning to what I said, was that I thought JS would give an error relating to the image-type. Would this not cause problems with other parts of javascript?

I didn't change the meaning to my reply.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum