dpbvpgvrhwxen3222 2013-01-23 16:35
浏览 28
已采纳

使用PHP编写Javascript警报时换行[重复]

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 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!

  • 写回答

1条回答 默认 最新

  • dongpeng0127 2013-01-23 16:39
    关注

    You need escaped characters:

    echo str_replace("
    ", "\
    ", $lang["brekzero"]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 MATLAB中streamslice问题
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端