第一次在这里张贴所以要善良! 出于某种原因,当我的PHP脚本到达时 if($ beds ==' nopref') 我可以显示消息的唯一方法是使用echo $ message。 使用javascript的以下行不会像我的其他页面一样显示它。 有任何想法吗? 代码:
<?php
$beds = $_POST['beds'];
$orientation = $_POST['orientation'];
$checkin = $_POST['checkin'];
$checkout = $_POST['checkout'];
$conn = mysqli_connect("localhost", "user", "password", "name");
if(!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
if ($beds == 'nopref')
{
$message = "Please select how many beds you'd like";
echo $message;
echo "<script type='text/javascript'>alert('$message');</script>";
}
?>
答案 0 :(得分:2)
由于'you'd
,您的文字已损坏。试试这个:
$message = htmlspecialchars("Please select how many beds you'd like", ENT_QUOTES);
答案 1 :(得分:1)
你可以逃避&#39;在JavaScript中,如\&#39;
if ($beds == 'nopref')
{
$message = "Please select how many beds you\'d like";
echo $message;
echo '<script>alert("'.$message.'");</script>';
}
答案 2 :(得分:0)
试试这个
print "<script type='text/javascript'>window.alert('$message');</script>";
或
print "<script type='text/javascript'>window.alert('Please select how many beds you had like');</script>";
答案 3 :(得分:0)
试试这段代码:
<?php
$message=htmlspecialchars("Hello World's",ENT_QUOTES);
echo "<script>alert('$message')</script>";
?>