In the website EasyNote I have got a problem with newlines.
On body onload I set a timer for auto-uploading a note every 3 seconds like this:
<body onload="setInterval(uploadNote,3000);current = 1;">
And the code for uploadNote is:
function uploadNote() {
var note = current+document.getElementById(\'note\').value; //current is the number of the note selected\' because echoed
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){}
}
xmlhttp.open("GET","uploadnote.php?q="+note,true);
xmlhttp.send();
}
And then there is this php-code:
$note = $_GET["q"]; //contains both notenumber as first digit and note
echo($note."\n"); //for debugging reasons
$notenumber = substr($note, 0, 1);
$notecontent = substr($note, 1, strlen($note));
$notecontent = str_replace("'","''",$notecontent);
$notecontent = nl2br($notecontent);
echo($notecontent); //for debugging reasons
$request = 'UPDATE notes SET note'.$notenumber.' = "'.$notecontent.'" WHERE mail LIKE "'.$email.'"';
$result = mysql_query($request);
Now, the problem is, that the newline characters in the textarea are erased completely, so the result of the php-snippet is twice the text without newlines and in the database also. However, there is no problem showing newlines in the textarea when I insert them directly in the database.
Help would be greatly appreciated.
EDIT: updated uploadNote() function:
function uploadNote() {
var note = current+document.getElementById(\'note\').value;
xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function()
{
if (xmlhttp.readyState == 4 && xmlhttp.status == 200){}
}
xmlhttp.open("POST","uploadnote.php",true);
xmlhttp.send("note="+note);
}
and php:
$note = $_POST["note"];
echo($note."\n");
$notenumber = substr($note, 0, 1);
$notecontent = substr($note, 1, strlen($note));
$notecontent = mysql_real_escape_string($notecontent);
echo($notecontent);
$request = 'UPDATE notes SET note'.$notenumber.' = "'.$notecontent.'" WHERE mail LIKE "'.$email.'"';
$result = mysql_query($request);
Problem now is that nothing works. The note won't update in the MySQL db.