Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cant echo PHP variable inside <script> tags? [closed]

This is my PHP code. I want error to be displayed using an alert window.

 if ($projectid=="")
  {
    $error = 'You must choose a project.
              Click &lsquo;back&rsquo; 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 ?

like image 961
marc Avatar asked Mar 08 '26 21:03

marc


2 Answers

The real problem is that this is what your rendered result looks like:

var error = You must choose a project.
            Click &lsquo;back&rsquo; 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.\r\n                 Click &lsquo;back&rsquo; and try again.";

Much better.

like image 190
Niet the Dark Absol Avatar answered Mar 11 '26 09:03

Niet the Dark Absol


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>
like image 29
mseifert Avatar answered Mar 11 '26 10:03

mseifert



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!