如何将 JavaScript 函数数据放入 PHP 变量中
问题描述:
我正在使用 PHP 和 JavaScript.我的 JavaScript 代码包含一个函数 get_data():
I am using PHP and JavaScript. My JavaScript code contains a function, get_data():
function get_Data(){
var name;
var job;
.....
return buffer;
}
现在我的 PHP 代码如下.
Now I have PHP code with the following.
<?php
$i=0;
$buffer_data;
/* Here I need to get the value from JavaScript get_data() of buffer;
and assign to variable $buffer_data. */
?>
如何将 JavaScript 函数数据赋值给 PHP 变量?
How do I assign the JavaScript function data into the PHP variable?
答
使用 jQuery 将 JavaScript 变量发送到您的 PHP 文件:
Use jQuery to send a JavaScript variable to your PHP file:
$url = 'path/to/phpFile.php';
$.get($url, {name: get_name(), job: get_job()});
在您的 PHP 代码中,像这样从 $_GET['name']
和 $_GET['job']
获取变量:
In your PHP code, get your variables from $_GET['name']
and $_GET['job']
like this:
<?php
$buffer_data['name'] = $_GET['name'];
$buffer_data['job'] = $_GET['job'];
?>