-1

This is the array of objects.

let arr=[
    {
        'teacherName':'skg',
        'subjectName':'c',
        'totalClass':4
    },
    {
        'teacherName':'skg',
        'subjectName':'php',
        'totalClass':4
    },
    {
        'teacherName':'skg',
        'subjectName':'js',
        'totalClass':4
    },
]

This is jQuery I used to send the data.

$('#myForm').submit(function () {
        console.log(arr);

        console.log('submit');
        $.ajax({
            type: 'POST',
            url: 'http://localhost/Backend/check.php',
            data: {
                arr: arr
            },
            success: function (data) {
                $('#output').html(data);
            }
        })

    })

I did not tried anything because I do not know what to do.

3
  • Associative array has keys defined in PHP, so you need to add key names to your arr. Just why you need that? Why $_POST['arr'] must be associative? Commented Jan 8, 2023 at 15:37
  • If you want to index that array by e.g. subjectName, then use array_column($_POST['arr'], null, 'subjectName') Commented Jan 8, 2023 at 15:38
  • You could sent the data encoded to a JSON string just replace your arr with JSON.stringify(arr) and from PHP script get is using: // Takes raw data from the request $json = file_get_contents('php://input'); // Converts it into a PHP object $data = json_decode($json); Commented Jan 8, 2023 at 17:22

1 Answer 1

0

I'm not familiar with jquery, but normally in php you'd receive it via $_POST['arr'] and to convert json string into associative array you'd need use json_decode() function:

<?php
if (isset($_POST['arr']))
{
  $data = json_decode($_POST['arr']);
}
?>

However, depending on how jquery sends data to the server, you might need change your php to something like this instead:

<?php
$data = json_decode(file_get_contents("php://input"));
if ($data)
{
  //data received
}
?>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.