Passing javascript variable to PHP

前端 未结 4 1742
渐次进展
渐次进展 2020-12-07 05:01

I refer to this question: Javascript value to PHP with Jquery

I tried with below code in file named a.php:



        
4条回答
  •  孤街浪徒
    2020-12-07 05:22

    OK, looking at this code:

    
    
    
    

    I'm presuming that this is all in one file. You have two bits of code in two different languages that are interpreted in different places. First, you have the Javascript at the top. This is not interpreted by your server. It is returned to the browser just as HTML is.

    Later, you have a piece of PHP. We're still on the server, and haven't sent anything to the browser yet. You look for a $_GET['test'] value. Your URL was http://localhost/learn/b.php: plainly there are no GET values in that URL, hence the error.

    When your code is sent to the browser, the browser sees the line $.get and does an AJAX request. This is another HTTP request. It does not modify the original request, so it doesn't mitigate the error you received above. With this request, your browser will send http://localhost/learn/b.php?test=3000 to the server, and there won't be an error. However, because you're not doing anything with the response, you aren't seeing the effects of this second request.

提交回复
热议问题