[英]communication between PHP and Javascript
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. 我想从PHP发送一些数据到Javascript。(这两个文件是同一文件夹中的不同文件)例如,如果我在PHP端计算一些值,我想将数据发送到javascript并使用数据。 How can I do this?? 我怎样才能做到这一点??
There's complete technology for that called AJAX
with a lot of tutorials on the internet . 有完整的技术称为AJAX
, 在互联网上有很多教程 。
And there's already a great and easy-to-deploy implementation - within jQuery
. 在jQuery
已经有一个很棒且易于部署的实现。
<script type='text/javascript'>
var myVar = <?php echo $myVar; ?>;
</script>
in a nutshell. 简而言之。 There are more sophisticated way to communicates though. 虽然有更复杂的沟通方式。
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. 文件“index.php”是使用jQuery Ajax方法在JavaScript和PHP之间进行的通信。 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. 单击“发送到php”按钮将运行“send_to_php()”,它将获取输入id“test”的值并通过ajax发送“js_php.php”文件。 In turn, the file "js_php.php" will receive this variable as POST, modify and print the value in JSON format. 反过来,文件“js_php.php”将接收此变量作为POST,修改并以JSON格式打印该值。 The method implemented by ajax function "send_to_php ()" will be "listening" all that "js_php.php" print. 由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”,然后JS能够在javascript代码中处理:
success: function (data) {
var obj = JSON.parse (data);
$("# test").val(obj.result);
},
看看这个AJAX教程: http : //news.php.net/php.general/219164
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文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.