Daniel
Daniel

Reputation: 1267

Decode $_POST data from ajax/Jquery serilized form

I submit a form using AJAX and JQuery.

serializedData = $form.serialize();

request = $.ajax({
    url: "processData.php",
    type: "post",
    data: serializedData
});

The problem is when I use the data on processData.php the text is urlencoded.

cuando=Una+fecha+%C3%BAnica

I tried using php's urldecode() but it is not working. it's still urlencoded..

$cuando = $_POST['cuando']; 
$cuando = urldecode($cuando);

Any suggestion? Thanks a lot!

Upvotes: 0

Views: 3240

Answers (3)

Daniel
Daniel

Reputation: 1267

First I tried changing the contentType attribute on JQuery $.ajax function, but it didn't work out:

  $.ajax({
    url: "insertar_datos.php",
    type: "post",
    contentType: "application/x-www-form-urlencoded;charset=ISO-8859-15",
    data: serializedData

So, I found that there some issues at working with other charset encodings that are not UTF8 on JQuery-ajax.

http://forum.jquery.com/topic/serialize-problem-with-latin-1-iso-8859-1-and-solution

Then, I tried with no hope the php function utf8_decode():

utf8_decode($_POST['cuando']);

And it worked. what I found on this link is that:

"utf8_decode simply converts a string encoded in UTF-8 to ISO-8859-1. A more appropriate name for it would be utf8_to_iso88591. If your text is already encoded in ISO-8859-1, you do not need this function. If you don't want to use ISO-8859-1, you do not need this function."

So, if someone is using iso-8859-1 and is having some problems with ajax posted data. You can decode it for sure with utf8_decode(). Maybe there is an easier way to decode all data before post it, but as you can see it didn't worked to me.

If someone knows a better/more efficient way I'll choose your answer happily.

Hope it helps someone,

Regards!

Upvotes: 1

MrCode
MrCode

Reputation: 64526

The form data is encoded in the same way as if it was a normal HTML form submission without Ajax, so there is nothing special to do, just use:

echo $_POST['cuando'];

PHP has already decoded and parsed the request body (post data).

Upvotes: 1

Maarten
Maarten

Reputation: 4671

Can't reproduce it..I made this:

<?php
echo urldecode('Una+fecha+%C3%BAnica');

Output is this:

Una fecha única

My only guess is that in your question you mean that your output is still what my input is, in that case you are somehow double urlencoding it on the other end

Upvotes: 1

Related Questions