views:

32

answers:

2

I don't understand why my AJAX script ignores all line foldings. I first type text to the textarea and then put onclick to send button. Here is my AJAX realization:

// 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 form

<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>
A: 

Treat your text output by my_script.php with nl2br() function before displaying it in html

Also you should use escape function for sending data in url like this:

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

Kamil Szot
Kamil Szot, I didn't mention on question: I store text in database but before storing I need to replace all \n in the text with special symbol like "|". All line foldings \n ignores in text when my_script.php receive text data.
ilnur777
A: 

Guys, I found an answer myself! :-)

Here is a special function that replaces all line foldings with special character.

// multiply replacing function
// ===========================

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

Then, to replace all line foldings in the textarea before sending to my_script.php while clicking to send button with onclick event, use above function like this:

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