13

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? Commented Apr 28, 2014 at 2:01
  • Why not use javascript to detect if all the forms are filled in? Commented 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! Commented 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. Commented 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');
Sign up to request clarification or add additional context in comments.

5 Comments

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>';
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
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)
This displays nothing. As in it does not display the php variable's value for some reason
Your answer did not work for me, but this did: echo '<script>alert("' . $str . '");</script>'; -source
6

Display variable php in alert javascript

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

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

    <?php   die(); 
          } ?>

Comments

3

You can use function follow this:

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

Comments

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

Use this code it would work correctly

1 Comment

Ciao, try to add a small explanation. Code-only answer discourages.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.