Adamlin0708 Adamlin0708 - 3 years ago 157
PHP Question

why can't I call php file with ajax in Cpanel?

I'm trying to call php file with ajax, but it seems there's a bug to fix, but I can't find it.....

here's my button in html (p.s $row["id"] is a variable represented article id)

<button type='button' class='btn btn-danger delete' data-info-id='<?php echo $row["id"];?>'>Delete</button>


and here's my ajax code

<script>
$(document).ready(function(){
$(document).on('click', '.delete', function() {
var info_id = $(this).data("info-id");
var deleteUrl = "/delete.php";
$.ajax({
type: "POST",
url: deleteUrl,
data: { 'info-id': info_id },
dataType: 'json',
success: function (results) {
console.log(results);
}
});
});
});
</script>


And here's delete.php, just test if it receives the variables from ajax or not

<?php

$id = $_POST['info-id'];

$result = json_encode($id);

return $result;

?>


But I there's nothing showed in console.log

Hope someone can give me some advises to fix this!

Thanks

Answer Source

Change

return $result;

to

echo $result;

return is for returning, echo is for echoing (printing).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download