I have a very small php script which in which I have a few buttons and no form. When I press one button, I want a small php script to be executed based on the id that I'm trying to send using ajax.
Those buttons look like this:
<button onclick="deleteTask()" class="task" data-id="1">Task 1</button>
<button onclick="deleteTask()" class="task" data-id="2">Task 3</button>
<button onclick="deleteTask()" class="task" data-id="3">Task 4</button>
...
And I have this ajax function:
function deleteTask() {
var id=$(this).data("id");
var dataString='id='+ id;
$.ajax({
type: "POST",
url: "test.php",
data: dataString,
cache: false,
success:function(html) {
alert(html);
}
});
}
The PHP script that is executed is just a test:
$id = $_POST['id'];
echo "Response: " . $id;
And the result is that alert box with a response of:
Response: undefined
What I want is to get that data-id of the button that was pressed as a response. If I press button 1 with data-id="1" I want to get a response of "1".
data: { 'id': id }