Jquery / Ajax从我的数据网格和mysql数据库中删除一行


Jquery / Ajax to delete a row from my datagrid and mysql database

我有这个脚本,它应该从mysql数据库中删除我的datagrid中的一行。

下面是主页面的代码:

    <?php
include('../classes/class.check.php'); 
$check = new Check(array(1,2)); 
// Open the DB connection and select the DB - creates the function getCreativePagerLyte()
include_once('configurations.php');
// Gets the data
$id=isset($_POST['id']) ? $_POST['id'] : '';
$search=isset($_POST['search']) ? $_POST['search'] : '';
$multiple_search=isset($_POST['multiple_search']) ? $_POST['multiple_search'] : array();
$items_per_page=isset($_POST['items_per_page']) ? $_POST['items_per_page'] : '';
$sort=isset($_POST['sort']) ? $_POST['sort'] : '';
$page=isset($_POST['page']) ? $_POST['page'] : 1;
$total_items=(isset($_POST['total_items']) and $_POST['total_items']>=0) ? $_POST['total_items'] : '';
$extra_cols=isset($_POST['extra_cols']) ? $_POST['extra_cols'] : array();
// Uses the creativeTable to build the table
include_once('creativeTable.php');
$ct=new CreativeTable();
// Data Gathering
$params['sql_query']        = "SELECT id, businessName, address, address2, city, state, zipCode, contactPerson, phone, email, url, page_show FROM com_listing order by id desc";
$params['search']           = $search;
$params['multiple_search']  = $multiple_search;
$params['items_per_page']   = $items_per_page;
$params['sort']             = $sort;
$params['page']             = $page;
$params['total_items']      = $total_items;
$params['items_per_page_init']      = '100'; 
// Layout Configurations
$params['header']           = 'ID, Business Name, Address 1, Address 2, City, State, Zip , Contact Person, Phone, Email, Website, Approved';    // If you need to use the comma use &#44; instead of ,
$params['width']            = '';
// ***********************************************************************************
// UNCOMMENT TO TEST THE DIFFERENTS OPTIONS AND SEE THE RESULTS AND TEST SOME YOURSELF
// extra columns - array(array(col,header,width,html),array(...),...) - default: array();
$arr_extra_cols[0]          = array(1,'<input type="checkbox" id="ct_check_all" name="ct_check_all" onclick="checkAll();" />','','<input type="checkbox" id="ct_check" name="ct_check[]" value="#COL3#" onclick="check();" />');
$arr_extra_cols[1]          = array(2,'Edit / Delete','','<a href="javascript: funcEdit(''&#35;ID&#35; - #ID#'n&#35;ROW&#35; - #ROW#'n&#35;PAGE&#35; - #PAGE#'n&#35;ITEMS_PER_PAGE&#35; - #ITEMS_PER_PAGE#'n&#35;TOTAL_ITEMS&#35; - #TOTAL_ITEMS#'n'n&#35;COL8&#35; - #COL8#'n&#35;COL3&#35; - #COL3#'n&#35;COL4&#35; - #COL4#'n&#35;COL5&#35; - #COL5#'n'');"><img src="images/icon-edit.gif" title="EDIT" style="margin: 0px 5px;" /></a>
<a href="javascript: funcDelete(''id: #ID#'nrow: #ROW#'npage: #PAGE#'nitems per page: #ITEMS_PER_PAGE#'ntotal items: #TOTAL_ITEMS#'n'ncolumn 8: #COL8#'ncolumn 3: #COL3#'ncolumn 4: #COL4#'ncolumn 5: #COL5#'n'');"><img src="images/icon-delete.gif" class="delete" title="DELETE" /></a>');
$params['extra_cols']       = $arr_extra_cols;
// actions select box - array(array(value,text)) - default: array();
$arr_actions[0]             = array('','-- Actions --');
$arr_actions[1]             = array('publish','Publish');
$arr_actions[2]             = array('duplicate','Duplicate');
$arr_actions[3]             = array('delete','Delete');
$params['actions']          = $arr_actions;
// url when actions changed - default: 'ctActions(''#ID#'')'
$params['actions_url']      = 'ctActions(''#ID#'')';        // javascript code triggered when actions is changed (you may use tags) - default
//$params['actions_url']    = 'alert(''Actions changed'')'; // javascript code triggered when actions is changed (you may use tags)
// ***********************************************************************************
$ct->table($params);
$ct->pager = getCreativePagerLite('ct',$page,$ct->total_items,$ct->items_per_page);
// If its an ajax call
if($_POST['ajax_option']!=''){
    echo json_encode($ct->display($_POST['ajax_option'],true));
    exit;
}else{
    $out=$ct->display();
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Lawn Care Business Directory Adminix</title>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link rel="stylesheet" type="text/css" href="css/creative.css">
    <script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
    <script type="text/javascript" src="js/creative_table_ajax-1.3.js"></script>
    <script type="text/javascript" src="js/my_javascript.js"></script>
    <script type="text/javascript">
    $(document).ready(function()
    {
        $('table#ct td .delete').click(function()
        {
            if (confirm("Are you sure you want to delete this row ?"))
            {
                var id = $(this).parent().parent().attr('id');
                var data = 'id=' + id ;
                var parent = $(this).parent().parent();
                $.ajax(
                {
                       type: "POST",
                       url: "deleteEntry.php",
                       data: data,
                       cache: false,
                       success: function()
                       {
                            parent.fadeOut('slow', function() {$(this).remove();});
                       }
                 });                
            }
        });
        // style the table with alternate colors
        // sets specified color for every odd row
        $('table#delTable tr:odd').css('background',' #FFFFFF');
    });
</script>
</head>
<body>
<br />
<br />
<div id="container">
<br />
    <h1>Lawn Care Business Directory</h1>

    <h2>Lawn Care Businesses</h2>
    <?php echo $out;?>
</div>
</body>
</html>

下面是mysql删除的代码:

   <?php
$id = $_POST['id'];
$mysql = new mysqli('localhost','*********','*********','com_listing') or die('There was a problem connecting to the database');
$stmt = $mysql->prepare('DELETE FROM data WHERE id=?');
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->close();
echo "done"; 
?>

(我编辑了我的数据库和密码…)现在它只是删除编辑和删除按钮。

在jQuery ajax()函数中,必须向它传递实际数据。您目前有data: data,不能工作。查看jQuery.ajax() API页面获取更多信息,并从那里查看这个示例,以了解我的意思:

$.ajax({
  type: "POST",
  url: "some.php",
  data: "name=John&location=Boston"
})