C
C
Cheizer2018-02-25 13:13:02
PHP
Cheizer, 2018-02-25 13:13:02

How to accept serialize() in php?

Friends, I can't accept serialize in php and print an array. Some kind of fool.

Was in JS

$.ajax({
                               type: "POST",
                               url: mailHandlerURL,
                               data: {
                                    name: getValFromLabel($('.name', _.form)),
                                    email: getValFromLabel($('.email', _.form)),
                                    phone: getValFromLabel($('.phone', _.form)),
                                }
                            })


In php took like this

$email = "";
if (isset($_POST["email"])) {
$email = $_POST["email"]; 
}

And then sent everything worked, now when a multidimensional array appeared, it’s very difficult to transfer it to ajax in php, it didn’t work, I struggled for several days, so I decided to use serialize

$.ajax({
                                type: "POST",
                                url: mailHandlerURL,
        data: form.serialize(),
                                success: function () {
          console.log(_.form.serialize())
                                }
                            })


BUT now nothing arrives in php, or it arrives but does not print, how to accept it? Now there is no $_POST["email"] or how is it correct now?

Did it like that
parse_str($_POST['serialize'], $email); 
$email = $email['email'];


It still doesn't print anything :(

Answer the question

In order to leave comments, you need to log in

5 answer(s)
S
SagePtr, 2018-02-25
@Cheizer

Why not use JSON? On the PHP side - using the json_decode function to convert back to an array

I
imhuman, 2018-02-25
@imhuman

There is no element named serialize in the post, look in the browser at the data being sent

A
Andrew Popov, 2018-02-25
@KPEATuBHO

The $.serialize method casts the form field data to a query string format. The data property expects an object to be passed. Nothing needs to be converted to JSON - this is a game, all that is needed is to transfer the object normally. There are a bunch of implementations for this, here's a simple example: https://codepen.io/kpeatubho/pen/oEPrBm ($.serializeObject method) as a result, we have a normal object in the data variable, passing which we can work normally with POST data.

A
Artem Spiridonov, 2018-02-25
@customtema

var_dump($_POST); what does it show? I bet $_POST['data'], which is where everything lies?
Get it out of it.

L
Loki9928, 2020-09-10
@Loki9928

Damn they ask one thing. And they answer differently. Andrew Popov you can then cycle through the form and collect all the data. Why bother with what you wrote?
parse_str($_POST[arraydata], $searcharray);
print_r($searcharray);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question