Eamon Spillane Eamon Spillane - 6 months ago 10
PHP Question

Javascript error message not showing in php

First time posting here so be kind!
For some reason when my php script reaches the
if ($beds == 'nopref')
the only way I can get the message to display is with echo $message.
The following line with javascript won't display it like my other pages.
Any ideas?
CODE:

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

Answer

You have broken text because of ' in you'd. Try this:

$message = htmlspecialchars("Please select how many beds you'd like", ENT_QUOTES);