Problem with ajax using jquery when sending data to php

I am having a weird problem sending data to php using ajax with JQuery here is my code:
<?php
$chkStates = $_POST['chkStatesObject'];
$newChkStates = json_decode($_POST['newChkStates'], true); // True to convert the JSON result to an associative array.
echo gettype($newChkStates);
echo sizeof($newChkStates);
?>
<?php
$chkStates = $_POST['chkStatesObject'];
$newChkStates = json_decode($_POST['newChkStates'], true); // True to convert the JSON result to an associative array.
echo gettype($newChkStates);
echo sizeof($newChkStates);
?>
JavaScript
function ConfirmReservationOptions()
{
if(newChkStates.charAt(newChkStates.length - 1) == ",")
newChkStates = '"{' + newChkStates.slice(0, -1) + '}"';
// console.log(newChkStates);
$.ajax({
url: 'confirm_reservation_options.php',
type: 'POST',
async: true,
data: {chkStatesObject: chkStatesObject, newChkStates: JSON.stringify(newChkStates)},
success: function(response){
$("#reservationOptionsResults").html(response);
},
});
}
function ConfirmReservationOptions()
{
if(newChkStates.charAt(newChkStates.length - 1) == ",")
newChkStates = '"{' + newChkStates.slice(0, -1) + '}"';
// console.log(newChkStates);
$.ajax({
url: 'confirm_reservation_options.php',
type: 'POST',
async: true,
data: {chkStatesObject: chkStatesObject, newChkStates: JSON.stringify(newChkStates)},
success: function(response){
$("#reservationOptionsResults").html(response);
},
});
}
Solution:
Can you not edit this? You are converting it to a string here newChkStates = '"{' + newChkStates.slice(0, -1) + '}"';...
Jump to solution
6 Replies
Alex
Alex6mo ago
Here are the errors I am getting from the php
No description
Alex
Alex6mo ago
The error is refering to the php part where I used echo sizeof($newChkStates); I need it to be converted to associative array instead of a string.
Alex
Alex6mo ago
Here is the value of newChkStates in the console when I console.loged it
No description
Solution
Eakam
Eakam6mo ago
Can you not edit this? You are converting it to a string here newChkStates = '"{' + newChkStates.slice(0, -1) + '}"';
Eakam
Eakam6mo ago
Or maybe I am reading too far and the error happens earlier
Alex
Alex6mo ago
You are absolutely right. That was the problem that kept my brain hurt for 2 days. Thanks a lot for the help