pernest Posted May 10, 2010 Share Posted May 10, 2010 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 https://forums.phpfreaks.com/topic/201304-compatible-escaping-of-characters-between-php-and-javascript/ Share on other sites More sharing options...
roopurt18 Posted May 10, 2010 Share Posted May 10, 2010 What do you mean encoding scheme? Do you mean for a way to serialize PHP objects back to JS and / or vice-versa? Or do you literally mean character encoding like UTF8? Link to comment https://forums.phpfreaks.com/topic/201304-compatible-escaping-of-characters-between-php-and-javascript/#findComment-1056153 Share on other sites More sharing options...
pernest Posted May 10, 2010 Author Share Posted May 10, 2010 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 https://forums.phpfreaks.com/topic/201304-compatible-escaping-of-characters-between-php-and-javascript/#findComment-1056160 Share on other sites More sharing options...
roopurt18 Posted May 10, 2010 Share Posted May 10, 2010 Are you using any sort of JavaScript framework for your AJAX? Show us how you're making your AJAX post, what value you're sending, and how it's not being received correctly. Link to comment https://forums.phpfreaks.com/topic/201304-compatible-escaping-of-characters-between-php-and-javascript/#findComment-1056166 Share on other sites More sharing options...
sharp.mac Posted May 11, 2010 Share Posted May 11, 2010 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 https://forums.phpfreaks.com/topic/201304-compatible-escaping-of-characters-between-php-and-javascript/#findComment-1056347 Share on other sites More sharing options...
pernest Posted May 11, 2010 Author Share Posted May 11, 2010 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 https://forums.phpfreaks.com/topic/201304-compatible-escaping-of-characters-between-php-and-javascript/#findComment-1056483 Share on other sites More sharing options...
roopurt18 Posted May 11, 2010 Share Posted May 11, 2010 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 https://forums.phpfreaks.com/topic/201304-compatible-escaping-of-characters-between-php-and-javascript/#findComment-1056765 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.