最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to introduce JavaScript alert window inside PHP code - Stack Overflow

programmeradmin3浏览0评论

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

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
    }
?>

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

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
    }
?>
Share Improve this question edited May 19, 2012 at 23:13 Klausos Klausos asked May 19, 2012 at 22:37 Klausos KlausosKlausos Klausos 16.1k52 gold badges142 silver badges219 bronze badges 0
Add a ment  | 

5 Answers 5

Reset to default 6

Try this, it echos the show_alert(); in 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>';
}
?>

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.

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

I would suggest

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

You are right about it David

发布评论

评论列表(0)

  1. 暂无评论