PHP、JavaScript、组合
我对PHP有一些条件,如果是真的,我需要PHP变量来获取javascript函数: 诸如此类:PHP、JavaScript、组合,php,javascript,combinations,Php,Javascript,Combinations,我对PHP有一些条件,如果是真的,我需要PHP变量来获取javascript函数: 诸如此类: <?php if(x == true){ $x = <script type="text/javascript">xfunction();</script>; } ?> 我真的不知道如何将它们组合起来。 <?php if($x){ print '<script type="text/javascript">xfunct
<?php
if(x == true){
$x = <script type="text/javascript">xfunction();</script>;
}
?>
我真的不知道如何将它们组合起来。
<?php
if($x){
print '<script type="text/javascript">xfunction();</script>;';
}
?>
如果我理解您的问题,应该可以工作吗?在呈现文档时调用onload函数。。您可以只echo
输出一个自执行函数
<?php
// your other code
if( $x ) { // no need to check if true... this will fail if falsey
echo "<script>";
echo "(function() { xfunction(); }());"; // I will execute when the parser hits me
echo "</script>";
}
// or (after reading your comments above)
if( $x ) {
$x = "";
$x .= "<script>";
$x .= "(function() { xfunction(); }());"; // I will execute when the parser hits me
$x .= "</script>";
}
// somewhere else in the document....
echo $x;
在php端,您可以使用$\u POST['data']
可以对PHP请求变量进行更多阅读
关于XHR的更多信息可以阅读您的问题是什么?您所说的“我需要PHP变量来获取JavaScript函数”是什么意思?您将无法在PHP代码中实际执行JavaScript代码,但您可以使用PHP在生成的页面上有条件地包含JavaScript。我阅读了答案,谢谢,我的意思是有一个PHP变量($x),我希望他包含JavaScript函数,如果PHP条件为真,那么所有这些都会发生。自执行函数可能会有过大的杀伤力。。。但是嘿!为什么不呢。谢谢你的回答,祝你愉快。谢谢你的回答,祝你愉快。
var request = new XMLHttpRequest();
request.open('POST', 'http://www.somepage.com/foo.php', true);
request.send('data=somedata');
if (request.status === "200") {
console.log(request.responseText);
}