Access a JavaScript variable from PHP

后端 未结 8 951
萌比男神i
萌比男神i 2020-11-22 11:47

I need to access a JavaScript variable with PHP. Here\'s a stripped-down version of the code I\'m currently trying, which isn\'t working:

相关标签:
8条回答
  • 2020-11-22 11:56

    try adding this to your js function:

        var outputvar = document.getElementById("your_div_id_inside_html_form");
        outputvar.innerHTML='<input id=id_to_send_to_php value='+your_js_var+'>';
    

    Later in html:

        <div id="id_you_choosed_for_outputvar"></div>
    

    this div will contain the js var to be passed through a form to another js function or to php, remember to place it inside your html form!. This solution is working fine for me.

    In your specific geolocation case you can try adding the following to function showPosition(position):

        var outputlon = document.getElementById("lon1");
        outputlon.innerHTML = '<input id=lon value='+lon+'>';
        var outputlat = document.getElementById("lat1");
        outputlat.innerHTML = '<input id=lat value='+lat+'>';  
    

    later add these div to your html form:

    <div id=lat1></div>
    <div id=lon1></div>
    

    In these div you'll get latitude and longitude as input values for your php form, you would better hide them using css (show only the marker on a map if used) in order to avoid users to change them before to submit, and set your database to accept float values with lenght 10,7.

    Hope this will help.

    0 讨论(0)
  • 2020-11-22 11:58

    _GET accesses query string variables, test is not a querystring variable (PHP does not process the JS in any way). You need to rethink. You could make a php variable $test, and do something like:

    <?php
    $test = "tester";
    ?>
    <script type="text/javascript" charset="utf-8">
        var test = "<?php echo $test?>";
    </script>
    
    <?php
        echo $test;
    ?>
    

    Of course, I don't know why you want this, so I'm not sure the best solution.

    EDIT: As others have noted, if the JavaScript variable is really generated on the client, you will need AJAX or a form to send it to the server.

    0 讨论(0)
  • 2020-11-22 12:00

    Well the problem with the GET is that the user is able to change the value by himself if he has some knowledges. I wrote this so that PHP is able to retrive the timezone from Javascript:

    // -- index.php

    <?php
      if (!isset($_COOKIE['timezone'])) {
    ?>
    <html>
    <script language="javascript">  
      var d = new Date(); 
      var timezoneOffset = d.getTimezoneOffset() / 60;
      // the cookie expired in 3 hours
      d.setTime(d.getTime()+(3*60*60*1000));
      var expires = "; expires="+d.toGMTString();
      document.cookie = "timezone=" +  timezoneOffset + expires + "; path=/";
      document.location.href="index.php"
    </script>
    </html>
    <?php
    } else {
      ?>
    
      <html>
      <head>
      <body>
    
      <?php 
      if(isset($_COOKIE['timezone'])){ 
        dump_var($_COOKIE['timezone']); 
      } 
    }
    ?>
    
    0 讨论(0)
  • 2020-11-22 12:04

    If showing data to the user, do a redirect:

    <script language="JavaScript">
        var tester = "foobar";
        document.location="http://www.host.org/myphp.php?test=" + tester;
    </script>
    

    or an iframe:

    <script language="JavaScript">
        var tester = "foobar";
        document.write("<iframe src=\"http://www.host.org/myphp.php?test=" + tester + "\"></iframe>");
    </script>
    

    If you don't need user output, create an iframe with width=0 and height=0.

    0 讨论(0)
  • 2020-11-22 12:07

    The short answer is you can't.

    I don't know any PHP syntax, but what I can tell you is that PHP is executed on the server and JavaScript is executed on the client (on the browser).

    You're doing a $_GET, which is used to retrieve form values:

    The built-in $_GET function is used to collect values in a form with method="get".

    In other words, if on your page you had:

    <form method="get" action="blah.php">
        <input name="test"></input>
    </form>
    

    Your $_GET call would retrieve the value in that input field.

    So how to retrieve a value from JavaScript?

    Well, you could stick the javascript value in a hidden form field...

    <script type="text/javascript" charset="utf-8">
        var test = "tester";
        // find the 'test' input element and set its value to the above variable
        document.getElementByID("test").value = test;
    </script>
    
    ... elsewhere on your page ...
    
    <form method="get" action="blah.php">
        <input id="test" name="test" visibility="hidden"></input>
        <input type="submit" value="Click me!"></input>
    </form>
    

    Then, when the user clicks your submit button, he/she will be issuing a "GET" request to blah.php, sending along the value in 'test'.

    0 讨论(0)
  • 2020-11-22 12:11

    As JavaScript is a client-side language and PHP is a server-side language you would need to physically push the variable to the PHP script, by either including the variable on the page load of the PHP script (script.php?var=test), which really has nothing to do with JavaScript, or by passing the variable to the PHP via an AJAX/AHAH call each time the variable is changed.

    If you did want to go down the second path, you'd be looking at XMLHttpRequest, or my preference, jQuerys Ajax calls: http://docs.jquery.com/Ajax

    0 讨论(0)
提交回复
热议问题