如何在PHP中打印javascript变量? [英] how to print a variable of javascript in php?
本文介绍了如何在PHP中打印javascript变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有JavaScript来获取时间,代码如下。
I have JavaScript to get time and the code is below.
<script type="text/javascript">
var d = new Date();
var H = d.getHours();
var m = d.getMinutes();
if(H>11)
{
var h = H-12;
var a = 'PM';
}
else
{
var h = H;
var a = 'AM';
}
</script>
现在,我想打印PHP中使用的变量,例如,
Now, I want to print the variables in PHP that used in JavaScript like,
<?php echo $h.$m; ?>
有可能吗?
如果是的话然后帮助我。
if yes then help me.
推荐答案
Javascipt在客户端上运行,php在服务器上运行...
如果你只是想在浏览器中打印var
Javascipt runs on the client, php runs on the server... If you just want to print the var in the browser
var varName = "varValue";
document.write(varName);
如果你需要将javascript var发送到php,你需要用jquery将它发送到服务器或javscript然后返回数据...
if you need to send the javascript var to php, you need to send it to the server with jquery or javscript and then return the data...
$.post("yourFile.php", { varName: "varValue" },
function(data) {
//print the data coming from yourFile.php
document.write(data);
});
如果您只需要将var发送到服务器
if you only need to send the var to the server
$.post("yourFile.php", { hour: "hour", min: "min", sec:"sec" } );
这篇关于如何在PHP中打印javascript变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文