0

I have a PHP snippet that generates a table and fills it, and adds a delete button at the end of each row.

while($row = mysql_fetch_array($result)){
$num=$row['id'];
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['adress']."</td>";
echo "<td>".$row['phonenumber']."</td>";
echo "<td><form action='delete.php' method='post'><button type='submit' value=$num name='deleteId'>delete</button></form></td>";
echo "</tr>";   
}

The delete.php file is this one :

<?php
$host="localhost"; 
$username="root"; 
$password=""; 
$db_name="students";

mysql_connect("$host", "$username", "$password"); 
mysql_select_db("$db_name");

$id = $_POST['deleteId'];

$sql="DELETE FROM students WHERE id='$id'";
$result=mysql_query($sql);
?>

I want to do this using Ajax asynchronously, ie. I don't want my page to refresh. Tried a million ways, yet it fails each time. Thanks in advance.

Rocksteady
  • 13
  • 1
  • 5
  • your may want to read this: [don't use `mysql_*` functions](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). And could you perhaps add the ajax code that you have tired so that we can see where you have possibly gone wrong – Michael Doye Mar 18 '15 at 15:40
  • 1
    We do not see any javascript here. What have you tried then? – David Soussan Mar 18 '15 at 15:41
  • The problem with your query is the quotes around the $id: it is an integer and does not need, or work with those quotes. If you do not want to get marked down for this question, show what javascript you have tried. – David Soussan Mar 18 '15 at 15:43

2 Answers2

1

Instead of using a form, you need to write your own JavaScript to handle the server call. Here's one way to do it. Make your PHP look something like this:

while($row = mysql_fetch_array($result)){
$num=$row['id'];
echo "<td>".$row['id']."</td>";
echo "<td>".$row['name']."</td>";
echo "<td>".$row['lastname']."</td>";
echo "<td>".$row['adress']."</td>";
echo "<td>".$row['phonenumber']."</td>";
echo "<td><button onclick="deleteStudent($num)">delete</button></td>";
echo "</tr>";   
}

And then have a JS function that looks something like this:

function deleteStudent(studentId) {
    $.ajax({
      url: "delete.php",
      method: 'post',
      data: {
        deleteId: studentId
      }
    });
}
Hayden Schiff
  • 3,280
  • 19
  • 41
1

Another way: Firstly, assign a unique ID for each of the delete button.

while($row = mysql_fetch_array($result)){
    $num = $row['id'];
    echo "<td>".$row['id']."</td>";
    echo "<td>".$row['name']."</td>";
    echo "<td>".$row['lastname']."</td>";
    echo "<td>".$row['adress']."</td>";
    echo "<td>".$row['phonenumber']."</td>";
    echo "<td><button id='delete-" . $row['id'] . "'>delete</button></td>";
    echo "</tr>";   
}

Then use jQuery:

$('button[id^="delete"]').click(function () {
    var id = $(this).attr('id').substr(6);
    $.ajax({
      type: "POST",
      url: "delete.php",
      data: {deleteId: id}
    });
}); 
lozy219
  • 561
  • 4
  • 18