Jump to content

escaping ' characters for ajax


ricmetal

Recommended Posts

hey

anyone tell me why the

xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

dec doesn't work.

 

the ' character still get's a slash added before it and appears in the the database, as opposed to not appearing in the database when the ' char is submitted through a html form with enctype set to urlencode.

 

is the reqeustheader supposed to emulate the html enctype or do i have to add more encoding and decoding?

 

i didn't really want to have the info in the database appeared as characters other than normal readable text.

 

 

Link to comment
Share on other sites

hi f1fan

thanks for replying

the rest of the code is probably okay

i saw a couple of examples on how to post using ajax and they all where very similar to my code.

and the differences, i tried with no avail.

i dont post the code bacause it's alot of it, and it's all interconnected between php's and js's,

but the basics i think the point is is that that setHeaderRequest line should treat input field values like the form tag does. at least that's what is looks like it should do!

i'll have a go at encoding and decoding, although i'll probably still look around for more explanations on this as i don't like writing code with encodes and decodes, specialy unsafe ones like stripslashes and similars.

Link to comment
Share on other sites

okay, i hate to go into encodings and decodings, and i need to buy a javascript book to learn more about js's inner workings but i got my problem solved, escaping and encoding.

 

i tried jquery's ajaxform and bullshit, it didn't help with stripslashing and character encoding, so i managed to emulate the ajax form and improve it with just a few lines of js :P

 

anyway for the peeps looking to have ajax forms and are stuck with encodings and characters here's a few thing to look up in order to get the job done. (thanks f1fan)

 

i'll actually post the lines i changed in my code (note that i dont go into charsets, but i do have my main html files with charset set to utf-8. you might want to look into setting charset headers for php files used with ajax) just review and acomodate:

 

<!-- encodeURIComponent -->
var params = "textContent="+encodeURIComponent(textContent)+"&cacheid="+Math.random();
xmlHttp.open("POST", url, true);

<!-- FUNCTION TO STRIP SLAHES I HAVE FROM A PHP GURU WHICH I HIRED SOME TIME BACK -->
<?php
function UndoMagicQuotesLambda(&$value, $key) {
       $value = stripslashes($value);
}
if (get_magic_quotes_gpc()) {
   $aMagicQuoted = Array(      '_POST', '_GET','_COOKIE', '_REQUEST');
   foreach ($aMagicQuoted as $var)
       array_walk_recursive($GLOBALS[$var], 'UndoMagicQuotesLambda');
}
?>

<!-- AND ON THE PHP SCRIPT CALLED FROM THE XMLHTTP OBJECT  -->
include '../phpincludes/m_quotes.php';//WHICH CALLS THE ABOVE FUNCTIONS
$textContent = $_POST['textContent'];

 

regards

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.