开发者

using ajax delete records form mysql

tell me with good example how can delete re开发者_如何学JAVAcords form database table using ajax


Write a PHP with code to delete records from database. Let's say delete.php (example extracted from here):

<?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
    {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db("my_db", $con);
    mysql_query("DELETE FROM Persons WHERE LastName='Griffin'");
    mysql_close($con);
?>

Then, do a GET or a POST with Ajax to delete.php. An example would be:

if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
} else {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","http://yourdomain.com/delete.php",true);
xmlhttp.send();

You can send parameters if you need to delete.php on the open call to xmlhttp. Take examples on how to do that from here.

As @Davide suggested in the comments, it might be a better idea to use POST commands instead of GET to perform this kind of operation.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜