2

I have a string which looks like this:

id_company=57&name=&address=&zip=&place=&phone=&mobile=&email=&birthdate=&birthplace=&bsn=&driver_expires=&role=&date_started=&contract=Tijdelijk&id_type=Paspoort&id_number=&id_expires=

Above example is empty for the most part (except id_company).

I serialize my form in jquery like this:

var $serialized = $form.serialize();

And in my php I do this:

$jsonemployee = unserialize($_POST['serialized']);

After that I try to print it because I expect an array:

echo '<pre>';
print_r($jsonemployee);
echo '</pre>';

But this shows me nothing.

I am 100% sure all data is passed, in my networktab I see the string being posted and if I just echo the string, it shows the string.

How can I create a PHP array from that string?

4
  • First thing to do is var_dump($_POST['serialized']). If you don't see anything then that data was never sent to the server. Commented Sep 19, 2018 at 12:58
  • @JohnConde I see the string when I var_dump it. string(185) "id_company=57&name=&address=&zip=&place=&phone=&mobile=&email=&birthdate=&birthplace=&bsn=&driver_expires=&role=&date_started=&contract=Tijdelijk&id_type=Paspoort&id_number=&id_expires=" Commented Sep 19, 2018 at 12:59
  • Do you know how to explode on for example '&'?. This will create an array for you. Commented Sep 19, 2018 at 13:01
  • @RonnieOosting Yes but shouldn't there be a PHP function that does all this for me? Commented Sep 19, 2018 at 13:01

2 Answers 2

5

unserialize is not for unserialising that format, it's the counterpart to serialize, which produces a very different result. To parse a URL-encoded string in PHP, use parse_str.

parse_str($_POST['serialized'], $result);
var_dump($result);

Of course, it's somewhat bizarre that you're sending a URL encoded string as $_POST['serialized']… You should send it as the one and only request body, and PHP will automatically parse it into $_POST and you won't have to do any of this.

Sign up to request clarification or add additional context in comments.

5 Comments

Great! This is what I needed, thank you. - I'll accept in 9 minutes.
@twan Note that jQuery's serialize() and php's serialize() are completely unrelated and produce results that are nowhere near similar.
Yeah makes sense now, I thought they did the same thing @jeroen
This will not work well if any text has & symbol, is there anyone got the same issue ?
@Udara Post your own question with details. & needs to be correctly encoded in the first place, as it has a special meaning in URL-encoded strings.
-1

you just need to convert this string into an array by doing :

$myArray = explode("&",$_POST['serialized']);

i think that will work for you.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.