jQuery/Php Error Illegal string offset from unserialized post data

Viewed 38

I'm posting a jquery serialize data posting from modal/pop up div tag with name attribute such as formdata[1]field_x

<div class="modal fade show" id="modal-payref-settings" data-bs-backdrop="static" data-bs-keyboard="false" tabindex="-1" aria-labelledby="modal-paycode-detailsLabel" aria-modal="true" role="dialog" style="padding-right: 17px; display: block;">
<div class="modal-dialog   modal-dialog-scrollableX   "><div class="modal-content ">
<div class="modal-body ">
<input type="text" name="formdata[1]payref_autonum" class="form-control dropdown-vw-item-text input-payref-autonum  ">
<input type="text" name="formdata[1]payref_prefix" class="form-control  input-payref input-payref-prefix  text-end">
<input type="text" name="formdata[1]payref_suffix" class="form-control  input-payref input-payref-suffix text-end">
<input type="text" name="formdata[1]payref_pad" class="form-control  input-payref input-payref-num-pad text-end" value="4">
<input type="text" name="formdata[1]payref_next" class="form-control  input-payref input-payref-num  text-end" value="876">
<input type="text" name="formdata[1]payref_sample" class="form-control input-payref-sample text-end" placeholder="Sample" aria-label="Sample" readonly="">
</div>
<!-- modal-body ends -->
</div>
</div>
</div>


$.ajax({
type: 'POST',
    url: 'model/trcode.php',
    data: {
        'a': 'SAVE_PAYREF',
    
      'formdata':$('#modal-payref-settings').find('[name^="formdata"]').serialize(),
    },
  //  dataType: "JSON",
    success: function (jsonStr) {
       alert(jsonStr);
    }
});

The Fetch/XHR capture the serialized posted data, which indicate the posting is correct. The same result return with print_r($_POST["formdata"] ); .

formdata%5B1%5Dpayref_autonum=Yes&formdata%5B1%5Dpayref_prefix=P2022&formdata%5B1%5Dpayref_suffix=&formdata%5B1%5Dpayref_pad=4&formdata%5B1%5Dpayref_next=876&formdata%5B1%5Dpayref_sample=

The data are getting un-serialized parse_str($_POST["formdata"], $formdata );. However $formdata return an empty array.

(
  [formdata] => Array
   (
     [1] => 
   )
)
2 Answers

You're trying to use a string as a full array

You can change the ajax code like

$.ajax({
type: 'POST',
    url: 'model/trcode.php',
    data: {
        'a': 'SAVE_PAYREF',
    
      'formdata':$('#modal-payref-settings').find('[name^="formdata"]').serialize(),
    },
  dataType: "JSON",
contentType: false,
            cache: false,
            processData:false,
    success: function (jsonStr) {
       console.log(jsonStr);
    }
});

And inside model/trcode.php page you can pass the

echo json_encode($_POST["formdata"]);

if use alert(jsonStr) instead of console.log(jsonStr) you will not get actual output sometimes because you are passing the object.

The issue is your form fields name attribute is not an array,(which you are thinking is an array, but has a syntax error).To fix this do like below:

change names like this: formdata[1][payref_autonum] and so on for others.

Related