Forum Moderators: open

Message Too Old, No Replies

Escape AJAX response

         

music_man

11:24 pm on Apr 29, 2007 (gmt 0)

10+ Year Member



Hi

I have a problem whereby I cannot get the response from my php script to parse in my Javascript.

It goes:

"Do php - make a response

e.g. $response = 'test<br /><a href="test.php">Test</a>';

Now I do:

echo 'doFunc(\''.$response.'\')';

In the ajax, the responseText is eval'd.

So it is now doing the function doFunc(e) {

}

However, I get the error "unterminated string literal". I have tried urlencoding the php variable and unescaping it in javascript, but that doesn't work.

Is there anyway I can encode the response in php and unencode it in my javascript?

Dabrowski

8:30 pm on Apr 30, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ok, I can't be sure, and I'm not a PHP guy, but I've been programming long enough to know an error when I see one.

I think.

This line:

echo 'doFunc(\''.$response.'\')';

Seems to have a strange collection of single/double quotes. Should it be like this?

echo 'doFunc("'.$response.'"');';

Assuming . is used for string concatenation, same as Perl?

music_man

1:45 am on May 1, 2007 (gmt 0)

10+ Year Member



Hi,

It works for me...

echo 'doFunc(\''.$response.'\')';

is giving:

doFunc('Response information')

Which is eval'd.

The problem is that I get all these "+", I've tried replacing "+" with " " and I think it sorta works...

Dabrowski

8:44 am on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oh yes that old gem!

It's because when you send, for example, form data to a URL via get, the space character is substituted with '+' and not escaped to %20. This would be much easier but hey I didn't invent it. You'll have to substitute the '+'s then unescape the string.

You can see this in action, go to Google and search for "escape my+string". Now look in the address bar and you'll see it's been escaped to "escape+my%2Bstring".

You have to put the spaces back before you unescape, if you didn't this example would be unescaped to "escape my string" and you've lost the '+' that was actually supposed to be there.

There's no other exceptions that I have ever encountered.

Hope this is helpful.

music_man

10:31 am on May 1, 2007 (gmt 0)

10+ Year Member



So...

php does:

echo 'doShowOldNote(\''.$id.'\',\''.urlencode($response).'\',\''.$date.'\')';

Javascript does:

eval(obj.responseText);

Then:

function doShowOldNote(id,r,date) {
r=unescape(r);
// $('') gets the element
$('tNotes').value = r.replace(/\+/,' ');
// etc

Should I do the escaping etc at the eval?

Dabrowski

11:28 am on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You need to swap over (in your JS) your unescape and replace, like this:

function doShowOldNote(id,r,date) {
var val = r.replace(/\+/,' ');
// $('') gets the element
$('tNotes').value = unescape( val);
...
...

Otherwise if you have +'s in your text, these will be changed to ' '.

But apart from that it's fine.

music_man

11:44 am on May 1, 2007 (gmt 0)

10+ Year Member



Doesn't seem to have changed anything the other way around... Am I encoding it ok in php?

Dabrowski

11:54 am on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I don't know PHP, but it looks about right. I can't really tell you if it's doing it right without looking at some strings. It would only look different if you actually have some + characters in the text. Try it both ways and see.

Do this.....

function doShowOldNote(id,r,date) {
var val = r.replace(/\+/,' ');
$('tNotes').value = unescape( val);

alert( "before: "+ r +"\nafter"+ $('tNotes').value);
...
...

That should give you a popup of the text before and after. Before you should see escape characters, like %20 is a space, %2C is a comma, etc.... After these should have been replaced with actual characters.

Please post the strings here so I can have a look.

music_man

12:13 pm on May 1, 2007 (gmt 0)

10+ Year Member



r is:

Test+test+test%0A%0ATest+test+test

unescape(val) is:

Test test+test

Test+test+test

Dabrowski

1:33 pm on May 1, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I see, it's working perfectly. It's only doing the first replace though, change your regex....

var val = r.replace( /\+/g,' ');

The 'g' on the end makes it do it through the entire string, left to itself it only does it once.

Also, the %0A's are linebreaks, for these to work in HTML they'll need to be changed to <br>'s.

Use this regex in addition to the other one....

val = val.replace( /(%0D)?%0A/g, "<br>");

That will account for %0D's too as Win32 systems use %0D%0A for newlines.

Has that now sorted it?

music_man

1:41 pm on May 1, 2007 (gmt 0)

10+ Year Member



Works a treat!

Thanks very much.