Ajax表单提交工作但没有响应

时间:2013-10-19 16:23:59

标签: javascript php ajax database

我正在使用一个菜单,用户将选择该菜单从数据库中删除记录。在菜单下方,我有一个表格,显示数据库中的记录。当我手动刷新页面,并且表格中的显示结果显示记录被删除时,尽管我付出了最大的努力,我似乎​​无法在我的#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;
    }
}

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

2 个答案:

答案 0 :(得分: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;
    }
}

答案 1 :(得分:1)

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

相关问题