将javascript变量传递给PHP [英] Passing javascript variable to PHP

查看:124
本文介绍了将javascript变量传递给PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我指的是这个问题:
使用Jquery对PHP的Javascript值

我尝试使用名为a.php的文件中的以下代码:

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

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script type="text/javascript">
 var ms = 9000;
function test()
{
    $.ajax({ url: "a.php",
         data: {"test":ms},
         type: 'get',
         success: function(output) {
                      $('#testing').html(output);
                  }
    });
}
test();
</script>
<?php
$ms = $_GET["test"];
echo "I am getting below value:";
echo $ms;
?>

然后我将浏览器指向 http://localhost/learn/a.php 但收到了错误消息& $ ms的值未按预期显示:

Then I point browser to http://localhost/learn/a.php but got error message & value of $ms is not shown as expected:


(!)注意:未定义的索引:测试
C:\第17行的wamp\\learn\a.php

( ! ) Notice: Undefined index: test in C:\wamp\www\learn\a.php on line 17

$ ms = $ _GET [test]; < - a.php中的第17行

$ms = $_GET["test"]; <-- The line 17 in a.php

我在下面尝试了另一个更简单的代码(b.php):

I tried another simpler code (b.php) below:

<script src="js/jquery.min.js"></script>
<script type="text/javascript">
 var ms = 3000;
 $.get("http://localhost/learn/b.php", { "test": ms } );
</script>
<?php
$ms = $_GET["test"];
echo $ms;
?>

然后我将浏览器指向 http://localhost/learn/b.php 但得到了类似的错误消息&没有显示$ ms的值:

Then I point browser to http://localhost/learn/b.php but got similar error message & no value of $ms displayed:


(!)注意:未定义的索引:测试
C:\ wamp \第7行的www \learn\b.php
以下是第7行的代码
$ ms = $ _GET [test];

( ! ) Notice: Undefined index: test in C:\wamp\www\learn\b.php on line 7 Below is code of line 7 $ms = $_GET["test"];

请咨询。谢谢。

推荐答案

好的,看看这段代码:

<script src="js/jquery.min.js"></script>
<script type="text/javascript">
 var ms = 3000;
 $.get("http://localhost/learn/b.php", { "test": ms } );
</script>
<?php
$ms = $_GET["test"];
echo $ms;
?>

我认为这都在一个文件中。您有两种不同语言的代码,可以在不同的地方进行解释。首先,您在顶部有Javascript。这不是您的服务器解释的。它就像HTML一样返回到浏览器。

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.

稍后,你有一块PHP。我们还在服务器上,还没有向浏览器发送任何内容。您要查找 $ _ GET ['test'] 值。您的网址是 http://localhost/learn/b.php :显然该网址中没有GET值,因此出错。

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.

当您的代码发送到浏览器时,浏览器会看到行 $。get 并执行AJAX请求。这是另一个 HTTP请求。它不会修改原始请求,因此不会减轻您上面收到的错误。使用此请求,您的浏览器将向服务器发送 http://localhost/learn/b.php?test = 3000 ,并且不会出现错误。但是,因为您没有对响应做任何事情,所以您没有看到第二个请求的影响。

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.

这篇关于将javascript变量传递给PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆