1

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

5 Answers 5

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>';
}
?>
Sign up to request clarification or add additional context in comments.

18 Comments

I tried this code. Alert window doesn't pop-up. I tried to debug this code, but the cursor jumps over show_alert() line...
Try now, fixed $outputs to $output
Tested it locally with $output = -1; instead of $output = run_function();, works for me.
Well, if you say it works for you, I will accept the answer. For some strange reason I don't see alert box.
$output = run_function(); if ($outputs == -1) { See what I mean? This is from your post. Either $output should be $outputs or viceversa.
|
6

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.

2 Comments

Could you give an example? I tried your suggestion,but probably I did something wrong.
@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.)
1

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

3 Comments

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...
does the if statement work? if u echo some php would it work? it might be that $output never == -1. But not sure - sorry
No,output is equal to -1. That's the case.
0
<?php
    $output = run_function();
    if ($outputs == -1) {
        echo "<script type='text/javascript'>alert("No Solution Found");</script>"
    }
?>

3 Comments

My answer is almost the same :/
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
True, but he had the global function in his so I included that in mine.
0

I would suggest

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

You are right about it David

3 Comments

Your double-quotes are probably doing to confuse the parser and produce an error.
or just use 'text/javasript' instead since you are using the double quotes for the echo?
I think that double quotes (") are more xhtml like and I prefer them!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.