How To Use Javascript Using PHP?
Solution 1:
This is because you start a new <?
/ <?php
tag inside another
else if($box== "BOX A" && $row[item] == "100")
{
<?php
echo "<script type='text/javascript'>";
echo "var r=confirm('The Box you chosen is already full. Do you want to save the other items?')";
echo "if (r==true){";
echo "alert('You pressed OK!')}";
echo "else{";
echo "alert('You pressed Cancel!')}";
echo "</script>";
?>
}
should be
else if($box== "BOX A" && $row[item] == "100")
{
echo "<script type='text/javascript'>";
echo "var r=confirm('The Box you chosen is already full. Do you want to save the other items?')";
echo "if (r==true){";
echo "alert('You pressed OK!')}";
echo "else{";
echo "alert('You pressed Cancel!')}";
echo "</script>";
}
But personally I think
else if($box== "BOX A" && $row[item] == "100")
{
?>
<script>
var r=confirm('The Box you chosen is already full. Do you want to save the other items?')";
if (r==true) {
alert('You pressed OK!');
} else {
alert('You pressed Cancel!');
}
</script>
<?
}
is much cleaner and more readable. Keep javascript out of PHP, dont echo it out, if you can avoid it. It can be hard to survey in 6 months.
Solution 2:
Or you can do something like this.
<?php
//some PHP code
if($box== "BOX A" && $row[item]== 100)
{
?>
<script type='text/javascript'>
var r=confirm('The Box you chosen is already full. Do you want to save the other items?');
if (r==true){
alert('You pressed OK!')}
else{
alert('You pressed Cancel!')}
</script>
<?php
}
?>
Solution 3:
I'm assuming that you have put a php tag and an if statement before this line
else if($box== "BOX A" && $row[item] == "100")
The fact is that you don't need to open another php tag. You are already writing in php so you don't need to specify it again since it will cause your code to fail. You could write something like this instead.
else if($box == "BOX A" && $row[item] == "100")
{ ?>
<script type='text/javascript'>
var r=confirm('The Box you chosen is already full. Do you want to save the other items?');
if (r==true)
alert('You pressed OK!');
else
alert('You pressed Cancel!');
</script>
<?php
}
Solution 4:
$str = <<<MY_MARKER
<script type="text/javascript">
document.write("Hello World!");
</script>
MY_MARKER;
echo $str;
Solution 5:
Your problem is basically because when you're echo'ing your javascript you're forgetting that the ;
character still needs to proceed JS lines:
<?php
//else if......
else if($box== "BOX A" && $row[item] == "100")
{
echo "<script type='text/javascript'>
var r = confirm('The Box you chosen is already full. Do you want to save the other items?'); // <- this was missing
if (r == true){
alert('You pressed OK!');
}
else{
alert('You pressed Cancel!');
}
</script>";
}
?>
Little trick I use is that PHP echo allows new lines without breaking so you can always just echo a block of code and look over it easier
Post a Comment for "How To Use Javascript Using PHP?"