tags:

views:

46

answers:

3

I made a little script that send a textarea content to PHP page with get ajax request:

$.get('./data/pages/admin/actions.php?savepost=1','&post_id=' + $(box).attr('id') + '&text=' + new_text,'text');

new_text get content from textarea with val();

When I save the text from php it has no \n character. Some1 can solve my problem? Thanks

+2  A: 

Use encodeURIComponent() to encode the newline character to the right url notation.

ZeissS
+2  A: 

You're not encoding your data at all, so lots of things are going to blow up (percent signs, ampersands, ...). Either use encodeURIComponent on each bit or (and this is easier) let jQuery do it for you:

$.get('./data/pages/admin/actions.php?savepost=1', {
    post_id: $(box).attr('id'),
    text:    new_text
}, 'text');

See the docs for details on the data parameter.

T.J. Crowder
Thanks for your help :D
netcelli
@netcelli: A pleasure, glad it helped.
T.J. Crowder
A: 

before passing new_text try doing this first.

new_text.replace("\r\n","%0D%0A");
Reigel
"\n" is just "%0A". "\r\n" (Windows newline) would be "%0D%0A"!
AndiDog
Yeah... it was too late for me to realize that... and when I tried editing the SO page says under maintenance... sigh...
Reigel
In any case, it only addresses a very narrow part of the problem.
T.J. Crowder