PHP:在echo下调用javascript

时间:2013-09-17 10:10:46

标签: javascript php jquery

我试图使用PHP调用javascript,但确认消息未显示...

这是我的代码:

echo "<button type='submit' name='deletePlaylist[]' value='" . $row['id']."' onClick='myFunction()' style='border: 0; background: transparent; cursor: pointer;'><img src='image/delete.png' /></button> ";

这是我的javascript:

<script>
function myFunction()
{
var x;
var r=confirm("Press a button!");
if (r==true)
  {
 window.location='booking_delete.php'"
  }
else
  {
  x="You pressed Cancel!";
  }

}
</script>

我是javascript的新手用PHP ...

当用户点击OKAY按钮时,它将进入另一页'booking_delete.php',在该页面中,它将删除用户选择删除的数据。

谢谢。

3 个答案:

答案 0 :(得分:1)

您的PHP代码没问题!但是您在two中犯了javascript code个错误。

window.location='booking_delete.php'"
                                    ^1) double qoutes closing but not opening
                                     2) No terminator(Semicolon i.e ';')

应该是这样的:

window.location = 'booking_delete.php';

答案 1 :(得分:-1)

将您的按钮类型更改为按钮,然后更改您的通话,我会这样写:

echo "<button type='button' name='deletePlaylist[]' value='" . $row['id']."' onClick='javascript:{myFunction();}' style='border: 0; background: transparent; cursor: pointer;'><img src='image/delete.png' /></button> ";

答案 2 :(得分:-1)

Try This Code:

<?php 

echo "<button type='submit' name='deletePlaylist[]' value='" . $row['id']."' onClick='myFunction()' style='border: 0; background: transparent; cursor: pointer;'><img src='image/delete.png' /></button> ";

?>
<script>
function myFunction()
{
var x;
var r=confirm("Press a button!");
if (r==true)
  {
 window.location='booking_delete.php'; // You made a mistake in this line.
  }
else
  {
  x="You pressed Cancel!";
  }

}
</script>