How can I use ajax to send a message through a url and still keep line breaks?

Here is a javascript / ajax example on the PAGE A page:

var commReq = getXmlHttpRequestObject();
function AddComment(Comment){
    if (commReq.readyState == 4 || commReq.readyState == 0){
        commReq.open("GET", '/receiveComment.php?com='+Comment+'&' + Math.random(), true);
        commReq.onreadystatechange = handleComment;
        commReq.send(null);
    }
}

Now the php page that receives the comment (receiveComment.php) PAGE B:

$Comment = mysql_real_escape_string(preg_replace("/[^A-Za-z0-9_,.!@:'\"\/\n ]/","", $_GET['com']));
mysql_query("INSERT INTO Comments (Comment,Date) VALUES ('$Comment','$Date')");

Obviously, these are just selective abbreviations, but from 2 pages. Page B has not been visible since its use via ajax, which I use to store the comment. But I want to be able to store line breaks that the user can insert in the textarea field. Any help or recommendations would be greatly appreciated!

+3
source share
3 answers

Use encodeURIComponent

commReq.open("GET", '/receiveComment.php?com='+encodeURIComponent(Comment)+'&' + Math.random(), true);

You still need to code for POST (credit for agrothe)

commReq.open("POST", '/receiveComment.php?' + Math.random(), true);
commReq.onreadystatechange = handleComment;
commReq.setRequestHeader("Content-Type", "multipart/form-data");
commReq.send('com=' + encodeURIComponent(Comment));
+1
source

preg_replace() \n . , , , \n , .

"\n" preg_replace().

, \n , .

FYI, GET, jQuery. GET URL-, 255 ( Firefox 1 IE 6 ), POST .

+1

POST GET. URL- GET.

, POST , ..

0

Source: https://habr.com/ru/post/1784818/


All Articles