PHP 和 Javascript 之间的通信
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13791474/
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
communication between PHP and Javascript
提问by Jimmy
I want to send some data to Javascript from PHP.(these two file is different file in same folder) For example, If I calculate some value in PHP side, I want to send the data to javascript and I use the data. How can I do this??
我想从 PHP 向 Javascript 发送一些数据。(这两个文件是同一个文件夹中的不同文件)例如,如果我在 PHP 端计算一些值,我想将数据发送到 javascript 并使用这些数据。我怎样才能做到这一点??
回答by Vyktor
There's complete technology for that called AJAX
with a lot of tutorials on the internet.
And there's already a great and easy-to-deploy implementation - within jQuery
.
并且已经有一个很棒且易于部署的实现 -在jQuery
.
回答by mpm
<script type='text/javascript'>
var myVar = <?php echo $myVar; ?>;
</script>
in a nutshell. There are more sophisticated way to communicates though.
简而言之。虽然有更复杂的通信方式。
回答by MartinSt77
In practice, you can use this:
在实践中,你可以使用这个:
FILE: index.php
文件:index.php
<HTML>
<body>
<input type="text" id="test" value="123"><br>
<input type="button" id="btn" onclick="send_to_php()" value="send to php">
<script src="http://code.jquery.com/jquery-2.1.4.min.js"></script>
<script>
function send_to_php() {
$.ajax({
url: 'js_php.php',
type: 'POST',
// Form data
data: function(){
var data = new FormData();
data.append('test', $("#test").val() );
return data;
}(),
success: function (data) {
var obj = JSON.parse(data);
$("#test").val( obj.result );
},
error: function (data) {
console.log(data);
},
complete: function () {
},
cache: false,
contentType: false,
processData: false
});
}
</script>
</body>
</HTML>
FILE: js_php.php
文件:js_php.php
<?php
//FILE: js_php.php
$test = $_POST["test"];
$test .= "456";
$arrResult = array(
'result' => $test
);
print json_encode($arrResult);
die();
?>
The file "index.php" is the communication between JavaScript and PHP using jQuery Ajax method. When click the "send to php" button will run the "send_to_php ()" that will take the value of the input id "test" and send through ajax, for "js_php.php" file. In turn, the file "js_php.php" will receive this variable as POST, modify and print the value in JSON format. The method implemented by ajax function "send_to_php ()" will be "listening" all that "js_php.php" print.
“index.php”文件是 JavaScript 和 PHP 之间使用 jQuery Ajax 方法的通信。当单击“发送到 php”按钮时,将运行“send_to_php()”,它将获取输入 id“test”的值并通过 ajax 发送“js_php.php”文件。反过来,文件“js_php.php”将接收此变量作为 POST,修改并以 JSON 格式打印值。ajax 函数“send_to_php()”实现的方法将“监听”所有“js_php.php”打印出来的内容。
After the success of the return, javascript convert the text "js_php.php" printed on a JSON object and then the JS able to process within the javascript code:
返回成功后,javascript将打印在JSON对象上的文本“js_php.php”转换成javascript代码内的JS就可以处理了:
success: function (data) {
var obj = JSON.parse (data);
$("# test").val(obj.result);
},
回答by Phil Rykoff
Have a Look at this AJAX Tutorial: http://news.php.net/php.general/219164
看看这个 AJAX 教程:http: //news.php.net/php.general/219164
回答by Aditya M P
Assign a JavaScript global variable in a script tag in the PHP page, and include the other javascript files after.
在 PHP 页面的脚本标签中分配一个 JavaScript 全局变量,并在之后包含其他 javascript 文件。
Sample:
样本:
<html>
<head>
<script type='text/javascript'>var testGlobal = <?php echo $globalJSValue ?></script>
<script type='text/javascript' src="url"></script>
<script type='text/javascript' src ="url"></script>
</head>
</html>
testGlobal variable will now be available to both javascript files.
testGlobal 变量现在可用于两个 javascript 文件。