Hobbyist
Hobbyist

Reputation: 16202

Javascript ILLEGAL when generated through PHP

The following code works:

var r = confirm('Are you sure you want to delete this category?\nDeleting the category will leave child items without a category');

However, when the same exact code is generated through php, it passes a Javascript Unexpected token ILLEGAL error... Help?

echo "<script>var r = confirm('Are you sure you want to delete this category?\nDeleting the category will leave child items without a category');</script>";

Is it possible to do this without moving into ajax, etc? I already know how to handle the events from the poup, just having some problems getting the confirm box to appear. I could just use a form and get the expected functionality, but I figured a confirm box would be nicer.

Upvotes: 2

Views: 43

Answers (1)

Alin P.
Alin P.

Reputation: 44346

You need to double escape the new line, once for JS and once for PHP:

echo "<script>var r = confirm('Are you sure you want to delete this category?\\nDeleting the category will leave child items without a category');</script>";

Upvotes: 5

Related Questions