NameSilo

Passing variables from Java Script to PHP

Located in Software Development Discussion, started by freeflow, Jun 11, 2011

Replies:
5
Views:
8,843

  1. freeflow

    freeflow Established Member ★★★★★★★★★★

    Posts:
    275
    Likes Received:
    14
    Hi everyone! Can you tell me how to pass variables from Java Script to a PHP File?


    This works with a .js file, but how can I make it work with php.

    html + .js file

    <body>
    <h1>Example</h1>
    <div id="geo" class="geolocation_data"></div>
    <script type="text/JavaScript" src="geo.js"></script>
    </body>
    </html>



    html + php file - How do I have to change the code above to make it work with
    the php file below?

    <?php

    $geo = 'http://maps.google.com/maps/api/geocode/xml?latlng='.htmlentities(htmlspecialchars(strip_tags($_GET['latlng']))).'&sensor=true';
    $xml = simplexml_load_file($geo);

    ...


    Thanks for your help.
     
    The views expressed on this page by users and staff are their own, not those of NamePros.
  2. immediate

    immediate Established Member ★★★★★★★★★★

    Posts:
    160
    Likes Received:
    18
  3. Roos

    Roos Established Member

    Posts:
    12
    Likes Received:
    1
    you can just echo the code that returns the value of the variable.
     
  4. Tork

    Tork New Member

    Posts:
    8
    Likes Received:
    0
    php to javascript is easy since php runs before and you can echo the javascript, if you can make it work on ajax (best solution but a little more complex) then you are on the other side

    when I need JS to PHP I usually make a cookie and retreive the data 1 click late or by running a second include with the echo
     
  5. The_Inferno

    The_Inferno Established Member ★★★★★★★★★★

    Posts:
    487
    Likes Received:
    1
    dude just use a hidden value in the JS
     
  6. Winleet

    Winleet Established Member

    Posts:
    14
    Likes Received:
    0
    <script type="text/javascript">
    var myVar = "<?PHP echo $phpVar; ?>";
    </script>
     

Want to reply or ask your own question?

It only takes a minute to sign up – and it's free!
NameWorth
  1. NamePros uses cookies and similar technologies. By using this site, you are agreeing to our privacy policy, terms, and use of cookies.
    Dismiss Notice
Loading...