Welcome to WebmasterWorld Guest from 50.16.79.246

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Newlines from textareas not saved to db?!

Actually, they appear to be getting lost in the 'GET' process

     
10:37 am on Oct 6, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 20, 2006
posts: 56
votes: 0


I have a form which contains a textarea...


<textarea cols="75"
rows="10"
name="data"
id="t_1"
db_table_name="_customers"
db_field_name="boa054_cust_notes"
where="1"
onblur="return update_field('t_',this.value,1,'');"
onclick="return add_note_head(this.value,1)">
</textarea>

The field and table names are just codes by the way. They will be translated serverside into the real names. No validation is necessary, so long as we don't end up in quirks mode, hence the custom tags - (won't be for qeneral public use).

The problem is that when I enter carriage returns into the textarea, when the data is saved in the database, it is saved without them.

I found that it looks like they are being removed during the http GET sending the data to the script that performs the INSERT.

I used

alert(data);
in the ajax.js script, immediately before transmission by GET. The data was echoed back in the alert box WITH line breaks at the correct places.

I then told insert.php to echo the the

$_GET['data']
back as the response and told the waiting ajax.js to
alert(request.response);
. This time there were no line breaks where there should have been (needless to say, it was not saved in the db with linebreaks either).

Any ideas what could be going wrong?

The trouble is that I want to display the data in html when I retrieve it. I'm using the php function, nl2br() to format the data I pull out of the db, but it's not currently working since the newlines are not getting as far as the database.

Let me know if you need to see the ajax.js or insert.php (since they are quite lengthy and I'm not sure which bit(s) you'd want to look at.

I'm hoping there's a really simple solution :)

Regards

Pete

5:30 pm on Oct 6, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


Try using get_magic_quotes_gpc() [php.net] to see if php is 'helping' you by escaping them - just do it as a test & echo the result. If so, you can use stripslashes() [php.net] to get it all back (or if you control the php.ini you can turn it off there), but if you're using mysql_real_escape_string() [php.net] to sanitize the data, that'll mess you back up. What I do is translate "\n" to '\n' and "\r" to '\r' prior to mysql_real, then translate them back afterward.
3:29 am on Oct 7, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 20, 2006
posts: 56
votes: 0


Thanks Cameraman. Sounds a bit complicated but will look into your suggestion tomorrow and let you know how I got on.

Thanks.

Peter

6:33 am on Oct 7, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 20, 2006
posts: 56
votes: 0


I've just tried everything you suggested I think.

I've tried all combinations of double and single quotes, addslashes, stripslashes, mysql_real_escape_string etc but nothing makes any difference.

I have echoed the URL prepared for the GET statement using alert(url); and the alert shows line breaks at the same places I entered them in the textarea.

My suspicion is that somehow, by the time it reaches my insert.php the linebreaks have somehow been removed, since I can't get them into the database and when I echo the data back to request.response the alert then shows the data without the linebreaks.

I've included the relevant bits of code below:

The text area from mypage.php :


echo '<textarea style="width:95%;" rows="6" name="data" id="t_' . $cust_id . '" db_table_name="' . $tbl_cust_pub . '" db_field_name="boa054_cust_notes" where="' . $cust_id . '" onBlur="return update_field(\'t_\',this.value,' . $cust_id . ',\'\');" onClick="return add_note_head(this.value,' . $cust_id . ')"></textarea>';

Extract from ajax.js :


// add my vars to url
url += '&db_table_name=' +db_table_name+ '&db_field_name=' +db_field_name+ '&record_id=' +record_id+ '&data=' +data+ '&where=' +where+ '&action=' +action ;
alert(url);
request.open("GET", url, true);
request.send(null);

Relevant part of insert.php :


$query = ' UPDATE `' . $table_name . '` SET `' . $field_name . '` = CONCAT("' . $_GET["data"] . '","\n\n\n",' . $field_name . ') ';

// save the info to the database
$result = mysql_query($query) or die(sql_failure_handler($query, mysql_error()));

echo $_GET['data'] ;

get_magic_quotes_runtime == 0

Any help would be greatly appreciated since I've wasted the best part of two days now trying to resolve this little issue :(

Thanks in advance.

peter

4:52 pm on Oct 7, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Jan 16, 2007
posts:914
votes: 0


Have you tried:
'&data=' +escape(data)+
in the ajax?
1:28 am on Oct 8, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:Apr 20, 2006
posts: 56
votes: 0


You're a STAR cameraman!

Thank you very much! I'd never heard of that on but it's worked a treat!

Now I can get on with being creative again!

Regards, Peter.