user4265789
user4265789

Reputation: 67

jQuery: Ajax get request does not work with PHP

I created a simple script in order to pass a value in a PHP file. This is the .js script:

$("#test").click(function () {
    var id = 34;
    $.ajax({
        method: "POST",
        url: "ajax.php",
        data: {
            id: id
        },
        success: function (data) {
            alert("data sent");
        },
        error: function (data) {
            alert("Data sending failed");
        }
    });
});

And this is the code included in the PHP file:

if (isset($_POST['id'])) {
    $id = $_POST['id'];
    echo $id;
}

The Ajax request works, but in the PHP file I receive an empty variable.

Upvotes: 1

Views: 108

Answers (1)

Kisaragi
Kisaragi

Reputation: 2218

Check your $_GET global, if it's set there, you'll need to change method: "POST" to type: "POST"

Upvotes: 2

Related Questions