Access a JavaScript variable from PHP

后端 未结 8 1035
萌比男神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 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:

    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...

    
    
    ... elsewhere on your page ...
    
    

    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'.

提交回复
热议问题