【问题标题】:PHP: Ajax ignores line foldings in the textPHP:Ajax 忽略文本中的行折叠
【发布时间】:2011-02-04 20:58:30
【问题描述】:

我不明白为什么我的 AJAX 脚本会忽略所有行折叠。我首先在 textarea 中输入文本,然后点击发送按钮。这是我的 AJAX 实现:

// creating ajax object
// ====================

function createRequestObject(){
try { return new XMLHttpRequest() }
catch(e)
{
try { return new ActiveXObject('Msxml2.XMLHTTP') }
catch(e)
{
try { return new ActiveXObject('Microsoft.XMLHTTP') }
catch(e) { return null; }
}
}
}

// message options (save, cancel)
// ==============================

function form1(text){
var http = createRequestObject();
if(http){
http.open("GET", "my_script.php?text=" + text);
http.onreadystatechange = function (){
if(http.readyState == 4){
alert("Ok!");
}
}
http.send(null);
} else {
document.location = "my_script.php?text=" + text;
}
}

html 表单

<p align="justify" style="margin-right:10; margin-left:10;">

<table style="margin-right:10; margin-left:10;" align="center" border="0" cellpadding="0" cellspacing="0" width="680">
<TBODY>
<form name="fgform">
<tr>
<td width="680" height="100" colspan="2"><p><textarea id="edit_text1" name="edit_text" rows="3" style="width: 680; height: 100;"></textarea></p></td>
</tr>

<tr>
<td width="340"><p><input type="button" id="saveB" value="Save Text" style="color:rgb(0,204,0); background-color:white; border-width:1; border-color:rgb(225,218,202); border-style:solid; width:100;" onclick="form1(document.getElementById('edit_text1').value);"></p></td>
<td width="340"><p align="right">&nbsp;</p></td>
</tr>
</form>
</TBODY>
</table>

【问题讨论】:

  • 还有什么解决这个问题的建议吗?

标签: php ajax line folding


【解决方案1】:

使用 nl2br() 函数处理 my_script.php 的文本输出,然后在 html 中显示它

您还应该使用转义函数在 url 中发送数据,如下所示:

document.location = "my_script.php?text=" + escape(text);

【讨论】:

    【解决方案2】:

    各位,我自己找到了答案! :-)

    这是一个用特殊字符替换所有折行的特殊函数。

    // multiply replacing function
    // ===========================
    
    function repl(text,replaceData1,replaceData2){
    if(text.indexOf(replaceData1)==-1){
    return text;
    }
    return text.split(replaceData1).join(replaceData2);
    

    然后,要在发送到 my_script.php 之前替换 textarea 中的所有行折叠,同时单击带有 onclick 事件的发送按钮,请使用上面的函数,如下所示:

    onclick="form1(repl(document.getElementById('edit_text1').value,'\r\n','|'));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-01-22
      • 2013-11-29
      • 1970-01-01
      • 2011-08-24
      • 2019-08-16
      • 1970-01-01
      • 2022-01-14
      相关资源
      最近更新 更多