ajax post data to PHP from tinyMCE text field - javascript

i have a text field TinyMCE 4.0 i when i am posting html from this field using ajax i seem to be having a problem with the data not ending up server side
in Firefox firebug it shows i posted this data
attendanceID=&noteID=&Category=2&date=20-May-2014&leave=<p> </p> <p>fxghdfhdsfhsdfhsdf</p>&prn=15407&act=edit
server side PHP
print_r( $_POST['leave']);
It prints
<p>
but when i post this
attendanceID=&noteID=&Category=2&date=20-May-2014&leave=<p>fadsfdasfasdf</p>&prn=15418&act=edit
everything works as expected prints
<p>fadsfdasfasdf</p>

You need to have it properly url encoded. It hits and thinks you've started a new variable.
This question has some more detailed information - When are you supposed to use escape instead of encodeURI / encodeURIComponent?
If it's data that someone else is providing to you, you should use encodeURIComponent on each url parameter. This prevents them from sending something to the server you're not expecting.
Note:
There is also encodeURI which encodes the whole URI, ignoring some characters that have meaning to the url.
Instead of leave=<p> you should have leave=%20
%20 is the url encoded value for a space

You need to make your post parameters URL encoded.
Try
encodeURIComponent for javascript
or
rawurlencode for PHP

Related

what would be the best way to encode variable for url ? in javascript

I am making a bookmarklet that uses $.getJSON() to transfer data from browser (different domain) to server and back. and I am having several issues with url encoding.
When I send the data to php I use encodeURIComponent() and decodeURIComponent() the response. in the php i do no encoding or decoding.
In many situation this works fine in cases of "/ & etc.
However if i have ',' or % or some other character, when i get the response and it tries to decodeURIComponent. i get the following error :URIError: malformed URI sequence
Is there a better way to encode url ?
Thanks

unrecognized characters while pasting tags from facebook

I use this code in javascript to send the comment via ajax to a JSP file :
comment=encodeURIComponent(comment);
alert(comment);
$('mydiv').load('/SendComment.jsp?productId='+productId+'&comment='+comment);
I use encodeURIComponent because my comment can contain multiple lines, single quotes, double quotes etc which break the URL in the load call, when sent as it is.
When a user copy-pastes a tag from facebook (eg: #Egypt), the encodeURIComponent converts it to
%23%E2%80%8EEgypt%E2%80%AC
and when i later print that comment, it comes as:
‎Egypt‬
This problem doesnt happen when I manually type '#Egypt' which encodes to %23Egypt
How do I solve this?

How to use ajax to post form data with non-UTF8 url-encoded string

I'm writing a greasemonkey script to add some features to one webpage(so clearly I can't change the code of the server side).
This is a really old website which only receives GBK encoded string.
For example, the form in current webpage have a text input field filled up with value "中"(\u4e2d, %E4%B8%AD in UTF-8 byte stream, %D6%D0 in GBK byte stream), when the user clicks the submit button, the browser will automatically serialize the form to application/x-www-form-urlencoded text then post it to the server. Because the webpage is claimed to be encoded in GBK, "中" is automatically encoded as %D6D0% in the application/x-www-form-urlencoded text, then the server side will decoded it with GBK encoding and get the right result.
Now I'm switching to ajax to submit the form to server(30~40 times in a short while), and I used JQuery's serialize() method to serialize the form to application/x-www-form-urlencoded text, then posted it. The problem is, JQuery's serialize method eventually uses encodeURIComponent to encode all the values of the form, and at this time, "中" is encoded as %E4%B8%AD regardless of that the charset of current webpage is GBK. The server side still use GBK to decode it, then failed.
I have googled a lot about this, encodeURIComponent will always encode string as utf-8 stream, and in javascript language there is no utility functions can do what I want, which is get the serialized form data as the browser will automatically do.
Any ideas? thanks.
What about writing an firefox addons and use c/c++ code to implement a XPCOM to do the conversion from javascript string to GBK format url-encoded string?

How to handle string that looks URL encoded in REST URI

Hey so I'm having trouble figuring out how to include something that looks URI encoded but in fact must be treated literally in my RESTful URL. For example, say I had an endpoint on my server that looked like this:
/something/:value
Then from my client code, I want to make a GET request to:
/something/some%20value
On the server, I want ":value" to be the literal string "some%20value" and NOT "some value". How do I properly encode the request URL to ensure the server treats it as such? I should also mention that not all request URIs will have these potential URL encoded values in them.
Thanks in advance.
Actually I think I may have figured this out. It seems to work if I just encode the percentage sign in my URLs. URL code for % is %25. So for example the correct URL would be:
/something/some%2520value

Greasemonkey communication with server that requires windows-1250 encoding

I'm developing a greasemonkey plugin, which is supposed to send a form in background using POST (GM_xmlhttpRequest) on an application not under my control. That application is written in PHP and seems to expect all its input in windows-1250 encoding. What I need to do is to take all the form fields as they are, edit just one of them and resubmit. Some of the fields use accented characters and are limited in length.
Not a problem in theory - I iterate over all form fields, use the encodeURIComponent function on the values and concatenate everything to a post request body. HOWEVER. The encodeURIComponent function always encodes characters according to UTF-8, which leads to all sorts of problems. Because PHP doesn't seem to recode my request to windows-1250 properly, it misinterprets multibyte strings and comes to the conclusion that the resubmitted values are longer than the allowed 40 characters and dies on me. Or the script just dies silently without giving me any sort of useful feedback.
I have tested this by looking at the POST body firefox is sending when I submit the form in a browser window and then resending the same data to the server using xhr. Which worked. For example the string:
Zajišťujeme profesionální modelky
Looks as follows, when encoded by encodeURIComponent:
Zaji%C5%A1%C5%A5ujeme%20profesion%C3%A1ln%C3%AD%20modelky
Same thing using urlencode in PHP (source text in windows-1250) or Firefox:
Zaji%9A%9Dujeme+profesion%E1ln%ED+modelky
Apparently, I need to encode the post body as if it were in windows-1250 or somehow make the server accept utf-8 (which I doubt is possible). I tried all kinds of other function like escape or encodeURI, but the output is not much different - all seem to output in utf-8.
Is there any way out of this?
Another way to get Firefox to encode a URL is to set it as the href of a link. The property (NOT attribute) will always read back as an absolute link urlencoded in the page's encoding.
For a GET request you would simply set the href as http://server/cgi?var=value and read back the encoded form. For a POST request you would have to take the extra step to separate the data (you can't use ?var=value on its own because the link reads back as an absolute link).
Let the browser encode the form. Put it in a hidden iframe and call submit() on it.

Categories