George George George George - 6 months ago 24
Ajax Question

Jquery simple Ajax Post PHP not working

I have a simple code but is not working. I want to create a more complex function but I just need the basic structure.

HTML

<span class="cleanreport">Delete Record</span>


Javascript:

$( ".cleanreport" ).click(function() {

var token = "test";

$.ajax({
data: {"data": token},
type: "post",
url: "clean.php",
success: function (data) {
console.log(data);
$('.test').html(data);
}
});
});


PHP clean.php

$data = $_POST['data'];
echo $data;


What I am doing wrong?

tsg tsg
Answer

This should work for you:

var token = "test";
$.ajax({
    type: 'POST',
    url: "clean.php",
    data: {id: token},
    dataType: "json",
    success: function(response) {
        // some debug could be here
    },
    error: function(a,b,c) {
        // some debug could be here
    }
});

If not, please debug success and error parameters using console.log().

Based on jquery documentation setting type is an alias for method so this could not be a problem in you case for sure.