doushaju4901 2013-05-17 08:58
浏览 85
已采纳

从函数输入到Javascript插入带换行符的单词

I have a condition below. pop function in button input has line 1 followed by newline Line2. When I click on button to javascript it pops error " Unexpected token ILLEGAL " in Console.

Value inside pop() of button is generated dynamically. I get this error only if I have a new line char in the input text.

<script type='text/javascript'>

function pop(valu)
{
alert("here"+valu);
document.getElementById('box').innnerHTML = valu;
}

</script>

<button onclick="pop('Line 1
Line 2')"> Click </button>
<textarea id='box'></textarea>

Backend is PHP.

Is there any way to achieve this on foreground ? or should I make any changes of inserting values to DB ?

I directly store the values in DB with newline character.

  • 写回答

2条回答 默认 最新

  • duanchun2349 2013-05-17 09:07
    关注

    Solution is use to nl2br function in PHP side

    nl2br($yournewlinestring); 
    

    function of PHP to convert newline to enter a new line for

    But before storing convert your html line breaks in php like this

    $_POST['xyz'] = preg_replace('/(?:
    |[
    ])/', PHP_EOL, $_POST['xyz'] ); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?