Encoding and Decoding for Ajax

encoding

One of the original issues with WSMS is that some special characters where not able to pass through the Ajax requests correctly due to special characters.  For example, if you used the “&” within your text boxes or within a link, everything after the “&” and include the “&” was removed when it would save the information to the database.

To fix this issue, the data must be URL (or URI) encoded before it is passed through the GET request, then it must be decoded before it is saved in the database; however, the problem is that it must be decoded within java script, then decoded within PHP.  This took a little bit of research and testing, but it does work.

Here is how to encode the data:

If you are receiving the data as variable v in java script:

var vout = encodeURIComponent(v);

Then pass the variable v=vout to the request.

On the PHP site, use the following:

$value = rawurldecode($_GET['v']);

This will then decode the data and you can store the variable $value within the database.

To see an example of this in action:

Within the dba section of panicd.com, look at the files

Encoding in Java Script:  dnstc_ajax.js under the function: saveFieldValue()

Decoding in PHP: location-sae-main-inforamtion-ajax-process.php

Leave a Reply