1

I know its much to ask, but maybe someone please can have a quick look at this file of an abandoned WordPress plugin, which is not working as it should. The Ajax post data results in an empty array, thus the script dies with an error. I guess something in WordPress changed, which makes this Script now failing. What i found out is that the Ajax call with Action

mbh_save_bounce_settings

is posting to the function which checks the data with

if(isset($_POST['data']['bounce']) && is_array($_POST['data']['bounce'])){

The above statement is not ture because the array $_POST['data']['bounce']) is empty and thus ending in the custom error from the "else" part. The data itself coming from the ajax call

'data': settings_data,

is working in the console log. But it's not getting into the if statement. There is no error displayed in console. Neither in the PHP log nor in the DEV console.

This is the file:

https://plugins.trac.wordpress.org/browser/bounce-handler-mailpoet/trunk/includes/class-mailpoet-bounce-handler.php

Daniel
  • 87
  • 1
  • 8
  • *"ending in the error"*. What kind of error? On the `php` side? Or in the console of your browser? Can you include the error in your question? – Ruvee Nov 29 '21 at 04:08
  • the custom error which get displayed in the "else" part of this `if(isset($_POST['data']['bounce']) && is_array($_POST['data']['bounce'])){` , since this statement is not true. `$_POST['data']['bounce']` is not set after the ajax post. – Daniel Nov 29 '21 at 11:21
  • 1
    I'm wondering why this is using `var settings_data = $(this).serializeObject();` - that does not appear to be a core jQuery function, but this plugin, https://plugins.jquery.com/serializeObject/ - and that serializes form data as JSON. PHP does not populate $_POST for JSON input. – CBroe Nov 29 '21 at 11:48
  • well im a beginner so i cant tell. but after checking the xhr payload in the dev console, i see its getting displayed like `data[bounce[hostname]]:` - is this then the correct way to call the check via `isset($_POST['data']['bounce'])` ? – Daniel Nov 29 '21 at 13:07

1 Answers1

0

Ok, i just found the fix by crawling thru every similar topic.

Actually this post answered it: AJAX form posting to PHP script but $_POST is empty in PHP script

It is indeed a problem with PHP/JSON parsing.

I removed this line

header('Content-Type: application/json');

in the function which receives the Ajax data and now its working!

Daniel
  • 87
  • 1
  • 8