Ajax call undefined index

There are 2 problems:

Problem 1

Your AJAX endpoint looks for my_id:

      $id = $_POST['my_id'];

But your javascript sends id:

            id : my_id

Importantly:

id != my_id

They do not match. There is no $_POST['my_id'], that’s why you get a PHP notice

Problem 2

You’re using $_POST but the code does not make a POST request:

type: 'POST',

There is no type parameter, the jQuery docs say to use method: 'POST'


As a side note, a REST API endpoint would have complained that the required my_id parameter was missing, and it would have complained that no GET endpoint existed, making both problems super clear in human readable text.

Even if the AJAX endpoint starts working, consider migrating to a REST API endpoint, it will give you:

  • human readable error messages
  • dedicated fields for listing your parameters, who can access it, all implemented by WP Core so you know you didn’t make a mistake or forget something
  • a pretty permalink URL
  • better authentication, and the option for remote API access with the use off additional plugins
  • easier to debug code