0

I tried to ask this question yesterday but my question was of bad quality.

I have a wordpress Site that I have integrated with react. I am sending a post request from a react form to the wp api.

The request from react Looks Like this.

axios.post('/wp-admin/admin-ajax.php', new URLSearchParams({
  action: 'report', 
  report: JSON.stringify(props.data),
  nonce : document.getElementById("my-react-app").getAttribute("data_id")
}), config)
.then(response => console.log(response))
.catch((err) => {
  let message = typeof err.response !== "undefined" ? err.response.data.message : err.message;
  console.log("error", message);
});
}

I am Able to receive the request on the server but I cannot target the json string of data in php.

$data

{"success":true,"data":{"action":"report","report":"{\\\"firstName\\\":\\\"rrt\\\",\\\"lastName\\\":\\\"trtr\\\",\\\"phone\\\":\\\"rt\\\",\\\"email\\\":\\\"rtrt\\\",\\\"placedata\\\":{\\\"Street\\\":\\\"tr\\\",\\\"City\\\":\\\"tr\\\",\\\"State\\\":\\\"tr\\\",\\\"zip\\\":\\\"tr\\\"},\\\"dateData\\\":{\\\"Start\\\":\\\"00:21\\\",\\\"End\\\":\\\"00:12\\\",\\\"Date\\\":\\\"2022-08-31\\\"},\\\"foodData\\\":{\\\"Food\\\":\\\"df\\\"},\\\"peopleData\\\":{\\\"People\\\":\\\"32\\\"}}","nonce":"f2331f42d4"}}

I have Tried ($data is an Array)

function report() {
global $wpdb;
$data = $_POST;
$test_One = json_decode($data['report']->firstName; //Response Nothing. Not even     Null
$Test_two = json_decode($data['report']); //Response (The Json String)--- {\\\"firstName\\\":\\\"rrt\\\",\\\"lastName\\\":\\\"trtr\\\",\\\"phone\\\":\\\"rt\\\",\\\"email\\\":\\\"rtrt\\\",\\\"placedata\\\":{\\\"Street\\\":\\\"tr\\\",\\\"City\\\":\\\"tr\\\",\\\"State\\\":\\\"tr\\\",\\\"zip\\\":\\\"tr\\\"},\\\"dateData\\\":{\\\"Start\\\":\\\"00:21\\\",\\\"End\\\":\\\"00:12\\\",\\\"Date\\\":\\\"2022-08-31\\\"},\\\"foodData\\\":{\\\"Food\\\":\\\"df\\\"},\\\"peopleData\\\":{\\\"People\\\":\\\"32\\\"}
//The above examples have all been placed in print_r
echo "<xmp>";
(Test 3) print_r(json_decode($data['report'])['lastName']); // Response --- {

echo "</xmp>";

}

I am new to PHP and have looked at many Answers On Here. Have yet to find a similar Issue. That tells me it's probably not much of an issue, yet I cant seem to figure it out. Any Help Would be Appreciated. Thank you!

1 Answer 1

1

try this solution

function report() {
    global $wpdb;
    $data = $_POST;
    $test_One = json_decode($data);
    $test_One  = $test_One->data->report->first_name;
    echo $test_One;
}
Sign up to request clarification or add additional context in comments.

2 Comments

$data is a PHP Array. json_decode says "expected string got array" for $test_One = json_decode($data);
Hello Wanted to give an Update. This does work. All I needed to Do Was Add stripslashes() before json_decode(). Thank you for your time

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.