11

I'm making a form that is supposed to create a javascript alert when some fields aren't filled out or filled out properly. I want to be able to take the error messages I've put in a php variable and display them in the javascript alert window.

The following code does not work:

function died($error) {
    echo '<script type="text/javascript"> alert('.$error.')</script>';
    die();
}

How can I add the string contained in $error between the two "script" strings so it will output properly as a javascript alert?

Thank you!

MarvinLazer
  • 198
  • 2
  • 5
  • 16
  • do you want to append the $string and $error variables? – Eddie Apr 28 '14 at 02:01
  • Why not use javascript to detect if all the forms are filled in? – David Corbin Apr 28 '14 at 02:03
  • Sorry, I screwed up and added an old version of the code I was using to figure out what was wrong. The $string variable is completely unnecessary. I took it out. Thank you! – MarvinLazer Apr 28 '14 at 02:03
  • You just can do a client checking if you just want to 'alert' the error.. but then again the best way to validate is in the server side. – Eddie Apr 28 '14 at 02:09

4 Answers4

20

You only forgot quotations that are required for the JavaScript alert.

If you passed 'hello' to the function, your current code would create alert as:

alert(hello)

instead of doing:

alert("hello")

Therefore, change your code to the following (json_encode() the string and create a javascript variable out of it):

function died(string $message) {
    if ('' === trim($message)) {
        $message = 'died.';
    }
?>
    <script type="text/javascript">
      var message = <?php echo json_encode($message); ?>;
      alert(message);
    </script>
<?php
    die();
}

and you can use your function:

died('error on whatever');
hakre
  • 193,403
  • 52
  • 435
  • 836
user1978142
  • 7,946
  • 3
  • 17
  • 20
  • Thanks! It works, although I don't really understand why. What if I wanted to add other text in the alert that would be concatenated with the variable? Would I have to do something like this? echo ''; – MarvinLazer Apr 28 '14 at 02:18
  • as @frankgorman clearly stated, what happens is that, after you concatenated, you missed the quotes inside rendering the error on javascript. If you want to add, just concatenate another string on `$error`. Example: `$error = "Another Line \n" . $error` – user1978142 Apr 28 '14 at 02:22
  • You know you are very new to a language when you look for ways to alert/log variables :) (happens that I came to search for this answer too) – Eon Feb 24 '15 at 14:42
  • 1
    This displays nothing. As in it does not display the php variable's value for some reason – Scarl Oct 12 '15 at 08:29
  • Your answer did not work for me, but this did: `echo '';` [-source](https://stackoverflow.com/a/41312694/8840617) – Crickets Jun 08 '19 at 05:27
5

Display variable php in alert javascript

   <?php 
          function died($error) { ?>

            <script>alert("<?php echo $error; ?>")</script>

    <?php   die(); 
          } ?>
antelove
  • 3,216
  • 26
  • 20
2

You can use function follow this:

function died($error) {
    echo '<script> alert("'.$error.'")</script>';
    die();
}
0
<?php
 echo "<script type='text/javascript'>alert('{$_SESSION["success"]}');</script>";
 unset($_SESSION["success"]);
?>

Use this code it would work correctly