Jump to content

compatible escaping of characters between php and javascript


pernest

Recommended Posts

At one end I have text from the user handled by javascript

At the other I have php looking after a mysql database

The gap is bridged by ajax using POST, but I cannot find a character encoding scheme that both ends agree on.

 

I've tried several encoding functions in both languages but haven't been able to find one that both languages agree on. Is there a standard way to achieve this, because I'm stuck.

 

I've posted a similar question on a javascript forum but have not received an answer.

 

cheers for any help

Link to comment
Share on other sites

Honestly, I'm not certain, which is part of my problem.

 

What I'm doing is reading data from a form into a javascript variable, then transmitting this string using ajax POST. Then I'm trying to read this data into a php string. No matter which character escaping functions I use, some characters do not survive the transmission intact.

 

I also want to go the other way (php string to javascript).

 

If I'm still not making myself clear, then please ask questions that will help me clear this up for you.

 

Cheers

Link to comment
Share on other sites

I don't think any of us grasp what your asking, but my take on it is your trying to echo something out of php and running into a error in the way it is being interpreted by one of the other languages.

 

I ran into this when I was creating a PHP scrilt that would write a XML file to the server so flash could then read it, the error I discovered was <?xml could not be echoed the conventional way as "<?" was confusing PHP by thinking I was opening a new script tag.

 

echo "<?xml lang=blah"; //simply didn't work

 

echo "<" . "?xml lang=blah"; //did work

 

Try to be more detailed in your next posting and include errors / code snipplets to help us grasp what your talking about.

Link to comment
Share on other sites

Thanks for trying to help. I was very tired last night when I posted this question so I apologise for the lack of clarity. Mental note: sleep first, ask for help on internet forums later!

 

I've now solved my problems, so I'll let other know what I did.

My problem was had two parts to it:

 

1) QUESTION. Using an html file with embedded php, what is the correct escaping procedure to get a php string into a javascript variable without mangling it.

ANSWER.

var js_string = <?php echo json_encode($php_string); ?>;

 

2) QUESTION. Using GET or POST (through ajax but that doesn't matter), how do you correctly escape a javascript string for transmission and how should php unescape it?

ANSWER.

encoding with

var js_post_string = "data="+encodeURIComponent(js_string);

and decoding with

$php_string = stripslashes($_POST['data'])

 

I've tested this (uk keyboard) and it works for all alphanumeric characters, all punctuation, special characters like tab carrage return and even Guillemets and umlaut's.

 

If I've made a rod for my own back, then please let me know the simple way of sorting this out.

 

 

Link to comment
Share on other sites

You only need to perform stripslashes() on the server if magic quotes are turned on.  Not all hosts have magic quotes on and magic quotes is being removed from PHP so you should use the gpc_magic_quotes() function to determine if you need to strip slashes.

 

Also, rather building your post string manually in JS, I'd highly recommend using something like Dojo as a JavaScript framework.  You have the following methods in Dojo that would greatly help you in your task:

dojo.xhrPost()

dojo.toJson()

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.