如何在 PHP 代码中引入 JavaScript 警报窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10669335/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 10:38:43  来源:igfitidea点击:

How to introduce JavaScript alert window inside PHP code

phpjavascriptalert

提问by Klausos Klausos

I need to introduce JavaScript alert function show_alert()inside PHP code. How this can be done?

我需要show_alert()在 PHP 代码中引入 JavaScript 警报功能。如何做到这一点?

script type="text/javascript">
        function show_alert() {
            var msg = "No solution found.";
            alert(msg);
        }
</script>
<?php
//...
    $outputs = -1;
    if ($outputs == -1) {
        echo '<script type="text/javascript"> show_alert(); </script>';
    } else {
        ?>
        <table width="100%">
            <tr>
                <td width="100%"> 
                    <div class="scrollbar" id="chart">
                        <img src="ganttchart.php">
                    </div>
                </td>
            </tr>
        </table>
    <?php
    }
?>

回答by Ignacio Vazquez-Abrams

In-line JavaScript needs to be inside <script>tags. Simply output those as well, in a valid place. Do note that you cannot affect the operation of the PHP code this way though.

内嵌 JavaScript 需要在<script>标签内。只需将它们也输出到有效位置即可。请注意,您不能以这种方式影响 PHP 代码的操作。

回答by alexpja

Try this, it echos the show_alert(); in PHP:

试试这个,它呼应了 show_alert(); 在 PHP 中:

<script type="text/javascript">
    function show_alert() {
    var msg = "No solution found";
    alert(msg);
    }
</script>

<?php
//...

$output = run_function();
if ($output == -1) {
   echo '<script type="text/javascript"> show_alert(); </script>';
}
?>

回答by aurel

use this

用这个

<script type="text/javascript">
         show_alert();
    </script>

so like this

像这样

$output = run_function();
if ($outputs == -1) {
?>
<script type="text/javascript">
             show_alert();
        </script>
<?php
}
?>

回答by Johntor

I would suggest

我会建议

    $msg = "My message";
    echo "<script type=\"text/javascript\"> alert($msg); </script>";

You are right about it David

你说得对,大卫

回答by comu

<?php
    $output = run_function();
    if ($outputs == -1) {
        echo "<script type='text/javascript'>alert("No Solution Found");</script>"
    }
?>