简体   繁体   English

AJAX JQuery从html删除但不是从mysql数据库删除

[英]AJAX JQuery delete from html but not from mysql database

What is wrong here? 怎么了

My PHP/HTML (The only part that matters): 我的PHP / HTML(唯一重要的部分):

if(isset($_POST['submit']))
{
    $date = date('Y-m-d', strtotime(str_replace("-","/",$_POST['dateOfEntry'])));
    $username = $_POST['user'];

    $query = 'SELECT `ID`, `Date`, `Description`, `TypeOfDowntime`, `Machine#` FROM `machineissuesreport` WHERE `Date`="'.$date.'" AND `UpdatedBy` = "'.$username.'" ORDER BY `ID` DESC';

    $conn = mysqli_query($connection, $query);

    while($row = mysqli_fetch_array($conn))
    {
        echo '<tr>';
        echo '<td style="text-align: center" width="5px"><input type="button" name="edit" value="Edit"></td>';
        echo '<td style="text-align: center" width="5px"><a href="#" id="'.$row['ID'].'" class="delete">Delete</a></td>';
        echo '<td style="display: none;"><input type="hidden" value='.$row['ID'].'></td>';
        echo '<td>'.$row['Date'].'</td>';
        echo '<td>'.$row['Description'].'</td>';
        echo '<td>'.$row['TypeOfDowntime'].'</td>';
        echo '<td>'.$row['Machine#'].'</td>';
        echo '</tr>';
    }

}
?>

My Ajax/Javascript: 我的Ajax / Javascript:

$(document).ready(function() 
{
    $('.delete').click(function()
    {
        if(confirm("Are you sure you want to delete this row?"))
        {
            var del_id = $(this).attr('id');
            var $ele = $(this).parent().parent();

            $.ajax({
                type: 'POST',
                url: 'machineEntryLogEdit.php',
                data: {'del_id':'del_id'},
                success: function(data)
                {
                    $ele.fadeOut().remove();            
                },
                error: function (xhr, status, error)
                {
                    alert(this);
                }
            });
        }
    });
});

My PHP (on an external script: machineEntryLogEdit.php): 我的PHP(在外部脚本上:machineEntryLogEdit.php):

include('connServer.php');

$deleteID = $_POST['del_id'];

$query = 'DELETE FROM `machineissuesreport` WHERE `ID` ="'.$deleteID.'"';

$result = mysqli_connect($connection, $query);

if(isset($result)) 
{
   echo "YES";
} 
else 
{
   echo "NO";
}

?>

I have searched around and around for solutions but no avail. 我到处搜寻解决方案,但无济于事。 The only things it does is delete the record from the HTML table, but not from the database, causing the supposed-to-be-deleted row to reappear after refresh. 它唯一要做的就是从HTML表中删除记录,而不是从数据库中删除记录,从而导致应该删除的行在刷新后重新出现。 I am still very new to AJAX (in fact I just learned it myself today) and still reading the documentations and forums. 我对AJAX还是很陌生(事实上,我今天才刚刚学到它),并且仍在阅读文档和论坛。 Thanks. 谢谢。

This should be data: {'del_id': del_id} remove quotes so it react as a variable, not just a single string. 这应该是data: {'del_id': del_id}删除引号,以便它作为变量而不是单个字符串来响应。 And one more thing, your delete query does not execute cause you're using : 还有一件事,由于正在使用, 删除查询无法执行

$result = mysqli_connect($connection, $query);

Should be mysqli_query like the one you did on selecting data's part: 应该是mysqli_query就像您在选择数据部分时所做的那样:

$query = 'DELETE FROM `machineissuesreport` WHERE `ID` ="'.$deleteID.'"';
$result = mysqli_query($connection, $query);

It looks to me like you didn't pass the submit variable in your data. 在我看来,您没有在数据中传递submit变量。 If you want to include a form you need to pass the data, right now the server is receiving only one parameter, del_id 如果要包含表单,则需要传递数据,现在服务器仅接收一个参数del_id

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM