Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

5 Answers

up vote 5 down vote accepted

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>';
}
?>
share|improve this answer
I tried this code. Alert window doesn't pop-up. I tried to debug this code, but the cursor jumps over show_alert() line... – Klausos Klausos May 19 '12 at 22:47
Try now, fixed $outputs to $output – alexpja May 19 '12 at 22:49
Tested it locally with $output = -1; instead of $output = run_function();, works for me. – alexpja May 19 '12 at 22:53
Well, if you say it works for you, I will accept the answer. For some strange reason I don't see alert box. – Klausos Klausos May 19 '12 at 22:58
1  
$output = run_function(); if ($outputs == -1) { See what I mean? This is from your post. Either $output should be $outputs or viceversa. – alexpja May 19 '12 at 23:13
show 15 more comments

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.

share|improve this answer
Could you give an example? I tried your suggestion,but probably I did something wrong. – Klausos Klausos May 19 '12 at 22:40
If it didn't work then you didn't follow what I wrote. – Ignacio Vazquez-Abrams May 19 '12 at 22:42
@KlausosKlausos: In what way doesn't it work? Update the question to show the client-side code actually being rendered in the browser. (Hint: If the indented code block isn't in the page source then $outputs doesn't equal -1. Which means the code does work and does exactly what it's intended to do.) – David May 19 '12 at 22:56

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
}
?>
share|improve this answer
I don't know why, but it doesn't work. Alert window should pop-up, but it doesn't. I tried to debug this code. Indeed the cursor jumps over show_alert() line... – Klausos Klausos May 19 '12 at 22:47
does the if statement work? if u echo some php would it work? it might be that $output never == -1. But not sure - sorry – aurel May 19 '12 at 22:53
No,output is equal to -1. That's the case. – Klausos Klausos May 19 '12 at 22:57
<?php
    $output = run_function();
    if ($outputs == -1) {
        echo "<script type='text/javascript'>alert("No Solution Found");</script>"
    }
?>
share|improve this answer
My answer is almost the same :/ – alexpja May 19 '12 at 22:57
Mine doesn't ask for a global function that was already defined in the Javascript. Instead, it just runs it without looking for a function – Jonah Allibone May 19 '12 at 22:58
True, but he had the global function in his so I included that in mine. – alexpja May 19 '12 at 23:04

I would suggest

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

You are right about it David

share|improve this answer
3  
Your double-quotes are probably doing to confuse the parser and produce an error. – David May 19 '12 at 22:54
or just use 'text/javasript' instead since you are using the double quotes for the echo? – alexpja May 19 '12 at 23:09
I think that double quotes (") are more xhtml like and I prefer them! – Johntor May 19 '12 at 23:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.