从 php if 语句运行 JavaScript 函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3057317/
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
Run a JavaScript function from a php if statement
提问by Asim Zaidi
I am using PHP conditions and want to know if I can run a JavaScript function without some one have to click on it using JavaScript:
我正在使用 PHP 条件,想知道我是否可以运行 JavaScript 函数而无需使用 JavaScript 单击它:
if($value == 1)
{
JavaScript to run the function
}
How would I do that?
我该怎么做?
回答by Daniel Vassallo
First of all keep in mind that your PHP code is evaluated on the server, while JavaScript runs in the browser on the client-side. These evaluations happen in different places, at different times. Therefore you cannot call a JavaScript function from PHP.
首先请记住,您的 PHP 代码在服务器上进行评估,而 JavaScript 在客户端的浏览器中运行。这些评估发生在不同的地方、不同的时间。因此,您不能从 PHP 调用 JavaScript 函数。
However with PHP you can render HTML and JavaScript code such that it is only rendered when your PHP condition is true. Maybe you may want to try something like this:
但是,使用 PHP,您可以呈现 HTML 和 JavaScript 代码,以便仅在您的 PHP 条件为真时呈现。也许你可能想尝试这样的事情:
if($value == 1) {
echo "<script>";
echo "alert('This is an alert from JavaScript!');";
echo "</script>";
}
回答by Chadwick
Javascript is client-side code, PHP is server-side, so you cannot execute javascript while building the page in PHP.
Javascript 是客户端代码,PHP 是服务器端代码,因此您无法在用 PHP 构建页面时执行 javascript。
To execute javascript on the client-side as soon as the page has loaded, one way is to use the body onloadhandler to call your function:
要在页面加载后立即在客户端执行 javascript,一种方法是使用正文onload处理程序来调用您的函数:
<?php
echo '<script type="text/javascript">';
echo 'function myFunction(){ /* do_something_in_javascript */ };';
echo '</script>';
if ($value == 1) {
echo '<BODY onLoad="myFunction()">';
}
?>
Better yet, if you can afford the bandwidth, use jQuery and use $(document).ready():
更好的是,如果您负担得起带宽,请使用 jQuery 并使用$(document).ready():
<?php
if ($value == 1) {
echo '<script type="text/javascript">';
echo '$(document).ready(function(){ /* do_something_in_javascript */ });';
echo '</script>';
}
?>
回答by Ninja Fist
I've found that you cannot do something like this in IE and other browsers. It does work in Firefox though. You have to echo out each line as posted in the other method.
我发现你不能在 IE 和其他浏览器中做这样的事情。不过它在 Firefox 中确实有效。您必须将其他方法中发布的每一行都回显出来。
<?php
if(somecondition)
{
?>
<script>
Some javascript code
</script>
<?php
}
?>
回答by SuperCatchyUsername
I know this thread is old but I just came across it and wanted to add my technique.
我知道这个线程很旧,但我刚刚遇到它并想添加我的技术。
You can also echo the php variable into JavaScript and do something based on its value. I use it to place database values into js so the user can do math on the page with them
您还可以将 php 变量回显到 JavaScript 中并根据其值执行某些操作。我使用它将数据库值放入 js 中,以便用户可以在页面上用它们进行数学运算
<script>
var jsvar = <?php echo $phpvar ;?>
if (jsvar = x){ do something...}
</script>
回答by embulldogs99
After some tinkering, I was able to get the following to work within a .php file. This doesn't work in a .html file though.
经过一番修修补补,我能够在 .php 文件中使用以下内容。但这在 .html 文件中不起作用。
<?php
if (isset($_SESSION['loggedin'])) {$login='t';}else{$login='f';}
?>
<script>
var login = "<?php echo $login ?>";
console.log(login);
if (login == 'f'){ .. do something..}

