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
5 Answers
Reset to default 6Try 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