将javaScript变量发送到php变量[duplicate]

时间:2022-08-27 17:38:03

Possible Duplicate:
How to pass JavaScript variables to PHP?

可能的重复:如何将JavaScript变量传递给PHP?

First I thought that I had to convert javascript to php, but then I found out that I cannot because of server and client side executions. So now I simply want to send ONE variable

首先,我认为我必须将javascript转换为php,但后来我发现,我不能因为服务器和客户端执行。现在我只想发送一个变量

<script type="text/javascript">
function scriptvariable()
{        
    var theContents = "the variable";
}
</script>

to a php variable

一个php变量

<?php
$phpvariable
?>

That function in the javascript executes when let's say I click on a button.

当我点击一个按钮时,javascript中的函数就会执行。

Now I have no idea on how to assign that phpvariable to the javascript one to use the phpvariable to look up stuff in my Database. I know I can add it to my url or some thing and refresh the page.. But I would like to do it with AJAX as I might have to use this Ajax method further in my webpage...

现在我不知道如何将phpvariable赋值给javascript变量,以便使用phpvariable在我的数据库中查找内容。我知道我可以把它添加到我的url或其他东西,然后刷新页面。但是我想用AJAX来做,因为我可能需要在我的网页中使用这个AJAX方法……

So is there an easy way to do this without having to dump pages of code on my page to do one simple thing?

那么,有没有一种简单的方法可以做到这一点,而不需要在我的页面上转储大量的代码来做一件简单的事情呢?

3 个解决方案

#1


43  

As Jordan already said you have to post back the javascript variable to your server before the server can handle the value. To do this you can either program a javascript function that submits a form - or you can use ajax / jquery. jQuery.post

正如Jordan已经说过的,在服务器处理该值之前,必须将javascript变量回传给服务器。为此,您可以编写提交表单的javascript函数——或者使用ajax / jquery。jQuery.post

Maybe the most easiest approach for you is something like this

也许对你来说最简单的方法是这样的。

function myJavascriptFunction() { 
  var javascriptVariable = "John";
  window.location.href = "myphpfile.php?name=" + javascriptVariable; 
}

On your myphpfile.php you can use $_GET['name'] after your javascript was executed.

myphpfile。在执行javascript之后,可以使用$_GET['name']。

Regards

问候

#2


62  

PHP runs on the server and Javascript runs on the client, so you can't set a PHP variable to equal a Javascript variable without sending the value to the server. You can, however, set a Javascript variable to equal a PHP variable:

PHP在服务器上运行,Javascript在客户机上运行,因此如果不将值发送给服务器,就不能将PHP变量设置为等于Javascript变量。但是,可以将Javascript变量设置为PHP变量:

<script type="text/javascript">
  var foo = '<?php echo $foo ?>';
</script>

To send a Javascript value to PHP you'd need to use AJAX. With jQuery, it would look something like this (most basic example possible):

要向PHP发送Javascript值,需要使用AJAX。对于jQuery,它应该是这样的(可能是最基本的示例):

var variableToSend = 'foo';
$.post('file.php', {variable: variableToSend});

On your server, you would need to receive the variable sent in the post:

在您的服务器上,您需要接收发送到post的变量:

$variable = $_POST['variable'];

#3


3  

It depends on the way your page behaves. If you want this to happens asynchronously, you have to use AJAX. Try out "jQuery post()" on Google to find some tuts.

这取决于页面的行为方式。如果您希望这种情况异步发生,您必须使用AJAX。尝试“jQuery post()”在谷歌上找到一些tuts。

In other case, if this will happen when a user submits a form, you can send the variable in an hidden field or append ?variableName=someValue" to then end of the URL you are opening. :

在其他情况下,如果用户提交表单时发生这种情况,您可以将变量发送到隐藏字段或append ?variableName=someValue“到您打开的URL的末尾。:

http://www.somesite.com/send.php?variableName=someValue

or

http://www.somesite.com/send.php?variableName=someValue&anotherVariable=anotherValue

This way, from PHP you can access this value as:

通过这种方式,从PHP可以访问这个值:

$phpVariableName = $_POST["variableName"];

for forms using POST method or:

使用邮寄方式填写的表格或:

$phpVariableName = $_GET["variableName"];

for forms using GET method or the append to url method I've mentioned above (querystring).

对于使用GET方法或上面提到的url附加方法(querystring)的表单。

#1


43  

As Jordan already said you have to post back the javascript variable to your server before the server can handle the value. To do this you can either program a javascript function that submits a form - or you can use ajax / jquery. jQuery.post

正如Jordan已经说过的,在服务器处理该值之前,必须将javascript变量回传给服务器。为此,您可以编写提交表单的javascript函数——或者使用ajax / jquery。jQuery.post

Maybe the most easiest approach for you is something like this

也许对你来说最简单的方法是这样的。

function myJavascriptFunction() { 
  var javascriptVariable = "John";
  window.location.href = "myphpfile.php?name=" + javascriptVariable; 
}

On your myphpfile.php you can use $_GET['name'] after your javascript was executed.

myphpfile。在执行javascript之后,可以使用$_GET['name']。

Regards

问候

#2


62  

PHP runs on the server and Javascript runs on the client, so you can't set a PHP variable to equal a Javascript variable without sending the value to the server. You can, however, set a Javascript variable to equal a PHP variable:

PHP在服务器上运行,Javascript在客户机上运行,因此如果不将值发送给服务器,就不能将PHP变量设置为等于Javascript变量。但是,可以将Javascript变量设置为PHP变量:

<script type="text/javascript">
  var foo = '<?php echo $foo ?>';
</script>

To send a Javascript value to PHP you'd need to use AJAX. With jQuery, it would look something like this (most basic example possible):

要向PHP发送Javascript值,需要使用AJAX。对于jQuery,它应该是这样的(可能是最基本的示例):

var variableToSend = 'foo';
$.post('file.php', {variable: variableToSend});

On your server, you would need to receive the variable sent in the post:

在您的服务器上,您需要接收发送到post的变量:

$variable = $_POST['variable'];

#3


3  

It depends on the way your page behaves. If you want this to happens asynchronously, you have to use AJAX. Try out "jQuery post()" on Google to find some tuts.

这取决于页面的行为方式。如果您希望这种情况异步发生,您必须使用AJAX。尝试“jQuery post()”在谷歌上找到一些tuts。

In other case, if this will happen when a user submits a form, you can send the variable in an hidden field or append ?variableName=someValue" to then end of the URL you are opening. :

在其他情况下,如果用户提交表单时发生这种情况,您可以将变量发送到隐藏字段或append ?variableName=someValue“到您打开的URL的末尾。:

http://www.somesite.com/send.php?variableName=someValue

or

http://www.somesite.com/send.php?variableName=someValue&anotherVariable=anotherValue

This way, from PHP you can access this value as:

通过这种方式,从PHP可以访问这个值:

$phpVariableName = $_POST["variableName"];

for forms using POST method or:

使用邮寄方式填写的表格或:

$phpVariableName = $_GET["variableName"];

for forms using GET method or the append to url method I've mentioned above (querystring).

对于使用GET方法或上面提到的url附加方法(querystring)的表单。