简体   繁体   中英

I don't know how to fetch serialized data in php

My html form is:

<form id="myform" action="" method="post" >
    <input id="firstname" name="firstname" type="text" placeholder="FIRST NAME" autofocus><br>
    <input id="lastname" name="lastname" type="text" placeholder="LAST NAME"><br>
    <input id="gender" name="gender" type="text" placeholder="GENDER" ><br>
    <input id="email" name="email" type="email" placeholder="EMAIL"><br>
    <input id="date" name="date" type="date" placeholder="JOINING DATE" ><br>
    <input id="designation" name="designation" type="text" placeholder="DESIGNATION"><br>
    <input id="username" name="username" type="text" placeholder="USER NAME" ><br>
    <input id="password1" name="password1" type="password" placeholder="PASSWORD" ><br>
    <input id="password2" name="password2" type="password" placeholder="CONFORM PASSWORD" ><br>
    <input type="button" id="submit" value="SUBMIT" />
</form>

In this program, i'm using ajax for validation. For that i'm serializing the form.

data:$("#myform").serialize(),

and i don't know how to deserialize it in PHP. Can any one explain the code to deserialize in PHP, and i used html5 for writing the form.

As your method is post , your variables should be read as any other post variable:

$firstname=$_POST['firstname'];
$lastname=$_POST['lastname'];

use unserialize() php function.

unserialize() takes a single serialized variable and converts it back into a PHP value.

http://php.net/manual/en/function.unserialize.php

but in your case.. since you are posting the data, you can just take the postd data with there coressponding name..

 $firstname=$_POST['firstname'];
 $email=$_POST['email'];
 .....

All serialize do is turn the form fields into a url string that can be sent to a server.

You act on it the same way you act on regular form submitted:

$firstName = $_POST['firstname'];
....

I guess the problem is in submitting your form. Check jquerypost if you are posting by AJAX. or else TRY

<input type="submit" id="submit" value="SUBMIT" />

instead of

<input type="button" id="submit" value="SUBMIT" />

I had no issues accessing as $_POST['firstname'] once it got posted successfully.

You are not showing us the full $.ajax request code: it may be either GET or POST request.

From jQuery.ajax documentation

By default, Ajax requests are sent using the GET HTTP method. If the POST method is required, the method can be specified by setting a value for the type option.

So in case of GET request you should inspect $_GET in your PHP code. Or just inspect $_REQUEST array. And access the form values as shown in other answers: $_GET["firstname"], for example

数据是从$_POST变量中检索的,不需要进行反序列化...因为在发送ajax请求时,序列化的数据已作为数组反序列化

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM