Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Sep 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Passing javascript variable to PHP

    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?

  • #2
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    You could try loading it via an Image object (untested, I'm no JS guru):
    Code:
    <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.

  • #3
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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.

  • #4
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Quote Originally Posted by BluePanther View Post
    @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.

    index.html
    Code:
    <script>
    bar = 'something';
    
    img = new Image();
    img.src ='./file.php?foo=' + encodeURIComponent( bar );
    delete img;
    </script>
    file.php
    PHP Code:
    <?php

    file_put_contents
    './data.txt'$_GET['foo'] );
    data.txt, after viewing index.html
    Code:
    something
    Last edited by kbluhm; 09-18-2011 at 10:37 PM.

  • #5
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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.

  • #6
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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?

  • #7
    Senior Coder
    Join Date
    Jul 2011
    Posts
    1,226
    Thanks
    3
    Thanked 171 Times in 171 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •