0

我正在使用用户将选择从数据库中删除记录的菜单。在菜单下方,我有一个显示数据库记录的表格。当我手动刷新页面时,表格中的显示结果显示该记录已被删除,但是,尽管我尽了最大努力,但我的#results 部分似乎没有收到任何消息。

这是HTML:

<form action="" method="POST" name="deleteAccountForm">
        <table>
            <thead>
                    <th>Delete Account</th>
                    <th></th>
            </thead>    
            <tbody>
                <tr>
                    <td><select name="deleteMenu" id="deleteMenu">
                    <?php
                    foreach($accounts as $key=>$value) { ?>
                        <option name="account_name" value="<?php echo $value['account_id_PK']; ?>"><?php echo $value['account_name']; ?></option>
                    <?php } ?>
                    </select>

                    </td>
                    <td><input type="submit" name="delete" value="Delete"></td>

                </tr>
            </tbody>
        </table>
    </form>

和我的 Javascript:

var delAccount = function(e) {
    e.preventDefault();
    var data = $('#deleteAccountForm').serialize();

    $.ajax({
        type: "POST",
        url: "inc/functions.php",
        data: data,
        beforeSend: function() {
            $('#results').html('processing');
        },
        error: function() {
            $('#results').html('failure');
        },
        success: function(response) {
            $('#results').html(response);
        },
        timeout: 3000,
    });
};

和我的PHP:

function delete_account() {

    require(ROOT_PATH . "/inc/database.php");

    $deleteAccount = $_POST['deleteMenu'];



    try {
        $results = $db->prepare("DELETE FROM account WHERE account_id_PK = ?");
        $results->bindValue(1, $deleteAccount);
        $results->execute();
    } catch(Exception $e) {
        echo "ERROR: Data could not be removed from the database. " . $e;
        exit;
    }
}

我还需要知道如何在完成后运行一个函数来更新我的显示表中的结果,而无需手动刷新页面。

4

2 回答 2

1

你如何在你的javascript中这样做:

var delAccount = function(e) {
    e.preventDefault();

    // Get the option that will be deleted to remove it
    // later on success.
    var opt  = $('#deleteMenu').children(':selected');

    var data = $('#deleteAccountForm').serialize();

    $.ajax({
        type: "POST",
        url: "inc/functions.php",
        data: data,
        beforeSend: function() {
            $('#results').html('processing');
        },
        error: function() {
            $('#results').html('failure');
        },
        success: function(response) {
            $('#results').html(response);
            // Remove the option
            opt.remove()
        },
        timeout: 3000,
    });
};

关于#results 的消息,在您的PHP 中,我只看到您在出现错误时做出响应,除非成功的响应被创建并发送到其他地方?

[编辑]

应该在try块的末尾创建响应,并且在这种情况下也发送响应。如果脚本到达那部分,则意味着一切顺利。

function delete_account() {
    require(ROOT_PATH . "/inc/database.php");
    $deleteAccount = $_POST['deleteMenu'];

    try {
        $results = $db->prepare("DELETE FROM account WHERE account_id_PK = ?");
        $results->bindValue(1, $deleteAccount);
        $results->execute();

        // If everything goes fine here, you can output the
        // success response.
        // Echo getAccounts() assuming it returns HTML (string).
        // Otherwise, setup your HTML and then echo it.
        echo getAccounts();
    } catch(Exception $e) {
        echo "ERROR: Data could not be removed from the database. " . $e;
        exit;
    }
}
于 2013-10-19T16:56:03.017 回答
1

您正在将数据提交到"inc/functions.php"。我假设您在问题中显示的php 代码片段在此页面内,并且您已在ANYWAY中调用它。
现在第一件事,您没有在 php 文件中回显任何内容。在 serer 端 php 脚本上回显的任何内容,只会在您在 ajax:success 方法中使用的变量“response”中发送到客户端。
此外,尚不清楚您的#result id 标记在 html 页面中的位置。
我的建议是,你使用“ jquery离线学习工具包”“(在google上搜索这个词,你会得到下载链接),它包含了所有的示例代码,你将学习如何有效地使用ajax,以及如何在成功或不成功的ajax提交后使用jquery选择器操作dom。

于 2013-10-19T17:00:20.630 回答