1

How can I javascript alert my php error messages? Here is my code:

<?php echo showmessage($msg)?>

I want to make it into an alert, how can I do that?

1
  • side note: i'd recommend finding alternatives to an alert, e.g.: jquery UI model or equiv, "warning/notice/info"-styled paragraph on the page, etc. Commented Nov 5, 2013 at 3:18

2 Answers 2

1

The use of json_encode makes sure that characters that needs to be escaped are escaped.

<script>
  alert(<?php echo json_encode($msg); ?>);
</script>
Sign up to request clarification or add additional context in comments.

Comments

1
 ?>
  <script type="text/javascript">
    alert("The error is  <?php echo $msg ?>");
    history.back();
  </script>
<?php

Or

echo '<script language="javascript">';
echo 'alert(Message here)';
echo '</script>';

4 Comments

hopefully the message has no " or line breaks in it.
a " wouldn't break it.
True, I'll leave it as " " then.
@Waclock really? alert("The error is I am going to break " because I am mean");

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.