为JavaScript中的表单元素创建键值对

I have a module where the forms created are dynamic. So the number of inputs can defer always. Also, the array key can also defer.

My current method of posting form is this:

name = form_options[option_1] value = 1

On submitting the form using POST, I get the form as array in $_POST, which looks like this.

form_options(
    option_1 => 1
)

But, now I am trying to implement the same thing using AJAX. So, I would need a common module to get all form values. I found a way to do it.

var objectResult = $('#options_form').serializeArray();
console.log(objectResult);

This gives me a result like this:

0: Object
  name: "form_options[option_1]"
  value: "1"

How can parse this result to get an array like $_POST array, which I can send as data in AJAX.

P.S: All the form elements have name field as form_options[key]

You should use this for get post data in PHP file.

    // You can use like this    
    var objectResult = $('#options_form').serializeArray();
    $.ajax({
        type: "POST", // Enter Request type GET/POST
        url: 'action.php', // Enter your ajax file URL here,
        dataType: 'json', // If you are using dataType JSON then in php file use die( json_encode($resultArray) );
        data: objectResult, // Put your object here
        beforeSend: function(){
            alert('before');
        },
        error: function(data) {
            console.log(data);
        },
        success: function(response){
            console.log(response);
        }
     });

    // In php file get values like this way
    $_POST['form_options']

try like this, In JQuery:

var objectResult = $('#options_form').serializeArray();
 $.ajax({
               type: 'POST',
                url: 'yoururl'',
               data: objectResult ,
               success:function(data){ 
                   alert(data);
                 }
  });

In your php:

<?php echo var_dump($_POST);?>

You can use jquery serialize with ajax directly, there is no need to use serializeArray:

     $.ajax({
           type:"post",
           url:"formHandleSkript.php",
           data:  $("#options_form").serialize(),
           success: function(response){
               $(".result").html(response);
           }
     });

For more information see http://api.jquery.com/serialize/