This is my PHP code. I want error to be displayed using an alert window.
if ($projectid=="")
{
$error = 'You must choose a project.
Click ‘back’ and try again.';
include error.html.php';
exit();
}
This is error.html.php that is supposed to be parsed by the browser
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Script Error</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script>
var error = <?php echo $error ; ?>;
window.onload = function(){ alert(error); }
</script>
</body>
</html>
Wahts up with the script tags ? Do they prevent PHP from running ?
The real problem is that this is what your rendered result looks like:
var error = You must choose a project.
Click ‘back’ and try again.
Does that looks like valid JavaScript to you? I think not.
var error = <?=json_encode($error);?>;
That should result in:
var error = "You must choose a project.
Click ‘back’ and try again.";
Much better.
Your problem is that javascript
is run on the client and will run after the page loads and so after the php
is run on the server.
However, you can do something like the following which allows php
to set the value of a javascript
variable when the page loads and then AFTER the page is loaded runs the javascript
to display the message.
<?php
$error = "test me";
echo "<script>error = '" . $error . "'</script>";
?>
<script>
var error;
window.onload = function(){
alert(error);
}
</script>
UPDATE Based on your edits, here's an updated answer. The echo "<script>error = '" . $error . "'</script>"
is needed to assign the $error
to the javascript
variable when the page is loaded.
if ($projectid=="")
{
$error = 'You must choose a project.
Click ‘back’ and try again.';
echo "<script>error = '" . json_encode($error) . "'</script>"
include error.html.php';
exit();
}
And the html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Script Error</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script>
var error;
window.onload = function(){ alert(error); }
</script>
</body>
</html>
If you have an $error within your PHP, then you can write:
<?php
echo "<html><head></head><body></body>";
$error = "whoops!"; // just for testing
echo "<script>
";
echo "alert('{$error}')
";
echo "</script>
";
echo "</html>";
?>
and the alert will happen as soon as the page is loaded, tested it on my server. This is for the situation where the $error is happening on the server side. (By the way, thanks for the fun question this late in my day!)
Actually it's quite simple.
if ($projectid=="")
{
$error = 'You must choose a project.
Click ‘back’ and try again.';
include 'error.html.php';
exit();
}
$error
will now contain You must choose a project. Click ‘Back’ and try again
. Note, there are no quotes, so then the line
var error = <?php echo $error ; ?>;
looks like this in the client:
var error = You must choose a project.
Click ‘Back’ and try again;
Which naturally causes an error as that is not valid javascript. You need to add quotes, either inside the definition of $error
serverside or outside the PHP -- something along the lines of:
$error = '"You must choose a project.
Click ‘back’ and try again."';
OR
var error = '<?php echo $error ; ?>';
as per your preference...
You certainly can use inline php to generate javascript code. They will, of course, be executed in different contexts, Server-Side vs. Client-Side, but that's mostly irrelevant.
However, you have to realize that the two languages aren't communicating as such, but part of the JS is being generated by the output of the PHP. You have to be careful about that output. One of the issues you have is that JS does not support multiline strings, so the value of $error in PHP can't have a newline.
Try this instead:
if ($projectid=="")
{
$error = 'You must choose a project. Click \"back\" and try again.';
include 'error.html.php';
exit();
}
and
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Script Error</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<script>
var error = "<?php echo $error ; ?>";
window.onload = function(){ alert(error); }
</script>
</body>
</html>
List of fixes: