skip to Main Content

I want to get the value of associative hidden fields array in PHP by posting it through jquery ajax, currently I am trying in this way

Here, HTML Code is:

<input type="hidden" class="override_arr" name="override_arr[][id]" value="1">';
<input type="hidden" class="override_arr" name="override_arr[][override_price_second]" value="5">';
<input type="hidden" class="override_arr" name="override_arr[][override_price_third]" value="10">

Jquery Code

var new_override_arr = [];
 $('input.override_arr').each(function(i, elem) {
       new_override_arr.push({
       'id': $(elem).attr('id'),
       'override_price_second': $(elem).val(),
        'override_price_third': $(elem).val(),
         });
   });
var formData = new FormData();
formData.append('new_override_arr', new_override_arr);

**Call Ajax**
$.ajax({
   url:'sa-store-view.php',
   type:'POST',
   data:formData,
   success:function(result){
   }
});

Try to get value in php file(sa-store-view.php)

<?php
echo "<pre>";print_r($_POST['new_override_arr']);
?>

but displaying like this

[object Object],[object Object],[object Object],[object Object],[object Object],[object Object]

please help me.

2

Answers


  1. .serialize() should work

    $.ajax({
       url:'sa-store-view.php',
       type:'POST',
       data: $('input.override_arr').serialize(),
       success:function(result){
       }
    });
    
    Login or Signup to reply.
  2. You can let PHP know that FormData element is an array

    formData.append('new_override_arr[]', new_override_arr);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search