Frantisek
Frantisek

Reputation: 7703

how do I parse the POST data that gets sent to ajax?

I am sending a html form using jquery to ajax, When I send the form to my php file and use print_r($_POST) to see what got sent, this is the result:

Array
(
    [data] => user_first_name=&user_last_name=&user_birthday_day=&user_birthday_month=&user_birthday_year=&user_addr_street=&user_ad
dr_street_no=&user_addr_city=&user_addr_zip=&user_addr_country=1&user_contact_phone=&user_contact_email=&user_knows_us_from=
)

Basically, I get what this is doing, but I am not quite sure what is the best approach to split this string into an array. I know how to use explode('&', $data), but it only explodes my string into an array with values, but numbered keys. I need $key => $value to look like [user_first_name] => 'Peter' instead of [1] => 'user_first_name=Peter'

How do you solve this problem?

EDIT: This is my ajax code, but it works, so I think it won't really be neccessary here, but still ..

  var formData = $('#form-registracia').serialize();

  $.ajax({
     url: '/ajax/registracia.php',
     type: 'POST',
     dataType: 'text',
     data: {'data':formData},
     success: function(data){
        // something will be here
     }
  });

Upvotes: 1

Views: 1349

Answers (2)

salmane
salmane

Reputation: 4847

I see two things that are wrong:

you need to serialize the form data: $(form).serialize()

what you get on the other end is your normal $_POST array

Upvotes: 1

Explosion Pills
Explosion Pills

Reputation: 191789

Instead of data: {'data': formData} all you need to do is data: formData

Upvotes: 6

Related Questions