0

Possible Duplicate:
Pass a PHP string to a Javascript variable (and escape newlines)

I am using javascript to do some form validation on a php page. There are several parts to it, but a typical one looks like this:

var brek=document.forms["seasonprice"]["price_brek"].value
if (brek==0)
{
alert("<?php echo $lang["brekzero"] ?>");
return true;
}

$lang['brekzero'] is defined in an included language file. As the text is fairly long I want to break it up onto several lines for better readability. I expected to be able to do that by inserting \n in the appropriate places. Instead it stops the alert working altogether, and it took me a long time to identify that as the cause.

What should I be doing instead?

The same script has also been giving me problems with accented letters. Writing á in the lang file results in á appearing in the alert instead of á. Writing á in the lang file causes strange symbol to appear in the alert. For the moment I've taken the very English way out, and left out the accents!

Community
  • 1
  • 1
TrapezeArtist
  • 777
  • 1
  • 13
  • 38

1 Answers1

1

You need escaped \n characters:

echo str_replace("\n", "\\n", $lang["brekzero"]);
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
  • I tried many things without success, and I thought I had tried this. Obviously not, because I've just done it again and solved the problem. Thanks a lot. – TrapezeArtist Jan 23 '13 at 16:46