PHP deserialize form data submitted by jquery ajax post serialized

0 votes
asked Aug 3, 2014 in Php by Ajay

I am trying to implement form submit using jquery ajax post. As per the information given jQuery library documentation, I have serialized the form data before I submit the form to my php url.

<form id="form1" method="POST" action="phpurl">
    <div class="input-group">
        <span class="input-group-addon">Title</span>
        <div>
            <input type="text" name="title" class="form-control" data-validation="required" data-validation-error-msg="Title required">
        </div>
    </div>

    <div class="input-group">
        <span class="input-group-addon">Description</span>
        <div>
            <input type="text" name="desc" class="form-control" data-validation="required" data-validation-error-msg="Description required">
        </div>
    </div>
    <input name="submitform1" value="Save" type="submit" class="qa-form-tall-button qa-form-tall-button-save">
</form>

jQuery("form1" ).on( "submit", function( event ) {
    event.preventDefault();
    var $form = jQuery('#form1'), url = $form.attr('action').val();
    jQuery.post(
            url,
            {formdata: form.serialize()},
    function(data)
    {
        //more code
    });
});

But in php server side code, I am not able to access the data directly from $POST. All I am looking for is, how to read data from $POST like

$title = $_POST['title'];
$desc = $_POST['desc'];
Share

1 Answer

+1 vote
answered Aug 3, 2014 by Aadhira (1,213 points)
selected Aug 3, 2014 by administrator
 
Best answer

I have faced the same issue and finally figured out the solution.
When a form data is submitted using serialized, the data will be sent like query string with key value pair. So, in your example, if you read the value of $_POST['formdata'], it will have the serialized string. So, use php parse_str method, that will convert your string to array. After that, you can read value of your form field from that array.

parse_str($_POST['formdata'], $formdata);//This will convert the string to array
$title = $formdata['title'];
$desc = $formdata['desc'];
commented Sep 20, 2014 by anonymous
very good it's amazing. it's solved my problem
commented Sep 14, 2015 by kamlesh
Yes it solved my problem too ! thanks
commented Feb 9, 2016 by Bibitybobityboo

Thanks for the tip!

Your answer

Preview

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
Anti-spam verification:
To avoid this verification in future, please log in or register.
site design / logo / content © 2013 - 2015 pinfaq.com
...