'Passing variable from JS using ajax to PHP undefined variable

I want to pass variable rating_index to the PHP code and send to database. Rating_index is set I'll not pass the code here because it is long, but right before AJAX i console.log(rating_index) and it has value(int). Add-review is a button that after click should send variable. In js script I am using AJAX :

$('#add-review').click(function(){
     $.ajax({
        url:"rating-data.php",
        method:"POST",
        data: {
            rating_index: rating_index
        },
        success:function(data)
        {
           
            console.log(data);
        }
    })
}

in my php file rating-data.php:

<?php 
    include 'connection.php';
    echo "work";
    echo $_POST["rating_index"];
?>

I got a console.log ('work') from PHP file and this error: Notice: Undefined index: rating_index in /Applications/XAMPP/xamppfiles/htdocs/bookwarm-app/rating-data.php on line 4

So it is getting my to php page but the variable are not passing correctly.

I was trying everything and I have no idea what is wrong and why this variable is undefined in php file. Thanks for any clue



Solution 1:[1]

This is the problem in your code, sir

$user_name = $_GET["user_name"];
$user_rating = $_GET["rating_index"];
$user_review = $_GET["user_review"];

you're doing a $_POST request from Ajax and in php file you're getting values from $_GET.....?

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rao DYC