12

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!

4
  • do you want to append the $string and $error variables?
    – Eddie
    Apr 28, 2014 at 2:01
  • Why not use javascript to detect if all the forms are filled in? Apr 28, 2014 at 2: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! Apr 28, 2014 at 2: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, 2014 at 2:09

4 Answers 4

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');
5
  • 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 '<script>alert('.'Here is some other text!\\n"'.$error.'");</script>'; Apr 28, 2014 at 2: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 Apr 28, 2014 at 2: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, 2015 at 14:42
  • 1
    This displays nothing. As in it does not display the php variable's value for some reason
    – Scarl
    Oct 12, 2015 at 8:29
  • Your answer did not work for me, but this did: echo '<script>alert("' . $str . '");</script>'; -source
    – Crickets
    Jun 8, 2019 at 5:27
5

Display variable php in alert javascript

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

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

    <?php   die(); 
          } ?>
3

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

1
  • 3
    Ciao, try to add a small explanation. Code-only answer discourages. Aug 15, 2020 at 14:08

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.