panthro
panthro

Reputation: 24061

Serializing a form using jQuery

I'm sending data in two parts through ajax:

data: {
    imageData: ctx.imageData,
    formData: $("#content-add-form").serialize()
},

imageData is base 64 encoded images, these are then resized and saved using a PHP script.

formData is all of the form fields. The problem is, it's coming out at the other end as:

title=my+title&date=16-09-2013

How can I send it so each field is an array? And also use the serialize() method?

Upvotes: 0

Views: 108

Answers (2)

user2610222
user2610222

Reputation:

You can use serialize. Below is an example.

$("#submit_btn").click(function(){
    $('.error_status').html();
        if($("form#frm_message_board").valid())
        {
            $.ajax({
                  type: "POST",
                  url: "<?php echo site_url('message_board/add');?>",
                  data: $('#frm_message_board').serialize(),
                  success: function(msg) {
                      var msg = $.parseJSON(msg);
                      if(msg.success=='yes')
                      {
                                                                        return true;
                     }
                     else
                     {
                        alert('Server error');
                        return false;
                    }
                   }
            });
        }
        return false;
    });

Upvotes: 1

Alex
Alex

Reputation: 1593

You can use .serializeArray().

Upvotes: 0

Related Questions