将 JavaScript 变量发送到 PHP 变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8191124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:02:43  来源:igfitidea点击:

Send JavaScript variable to PHP variable

phpjavascriptajaxvariables

提问by mrbunyrabit

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?

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

回答by Jordan Brown

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):

要将 Javascript 值发送到 PHP,您需要使用 AJAX。使用 jQuery,它看起来像这样(可能是最基本的例子):

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

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

在您的服务器上,您需要接收帖子中发送的变量:

$variable = $_POST['variable'];

回答by Grrbrr404

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.php 上,您可以$_GET['name']在执行 javascript 后使用。

Regards

问候

回答by Ricardo Souza

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。在 Google 上尝试“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. :

在其他情况下,如果在用户提交表单时发生这种情况,您可以在隐藏字段中发送变量或附加?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:

对于使用 POST 方法的表单或:

$phpVariableName = $_GET["variableName"];

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

对于使用 GET 方法或附加到我上面提到的 url 方法(查询字符串)的表单。