【问题标题】:Ajax is not working properly in wordpressAjax 在 wordpress 中无法正常工作
【发布时间】:2016-10-13 10:51:32
【问题描述】:

我正在开发一个插件,我在其中使用 ajax 删除和更新有关上传文件的信息..

以下是代码:

//Creating AJAX for saving value in databsae
add_action('wp_ajax_input_value', 'input_value');
add_action('wp_ajax_nopriv_input_value', 'input_value');

add_action('wp_ajax_update_value', 'update_value');
add_action('wp_ajax_nopriv_update_value', 'update_value');

add_action('wp_ajax_delete_value', 'delete_value');
add_action('wp_ajax_nopriv_delete_value', 'delete_value');

add_action('admin_head', 'input_ajax');
function input_ajax()
{
    ?>
    <script type="text/javascript">
        jQuery(document).ready(function () {
            jQuery("#submit").click(function () {
                var input_method = jQuery('#image_url').val();

                var data = {
                    action: 'input_value',
                    input_method: input_method
                };

                jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function (result) {
//                    alert(result);
                });
            });

            jQuery(".update").click(function () {
                var row = jQuery(this).parents('#list_item');
                var update_id = jQuery(this).data('id-update');
                var update_link = row.find('.img_link').val();
                var update_desc = row.find('.img_description').val();
                var update_title = row.find('.img_title').val();

                var data = {
                    action: 'update_value',
                   id: update_id,
                   update_link: update_link,
                   update_desc: update_desc,
                   update_title: update_title
                };

                jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function (result) {
//                    alert(result);
                });
            });

            jQuery(".delete").click(function () {
                var delete_id = jQuery(this).attr('data-id');

                var data = {
                    action: 'delete_value',
                    id: delete_id
                };

                jQuery.post('<?php echo admin_url('admin-ajax.php'); ?>', data, function (result) {
//                    alert(result);
                });
            });

        });
    </script>
    <?php
}

//Save values in database in "wp_images" table
function input_value()
{
    $name = $_POST['input_method'];

    global $wpdb;
    $wpdb->insert(
        'wp_images',
        array(
            'img_path' => $name
        )
    );
    die();
    return true;
}

//Updating values in database in "wp_images" table
function update_value()
{
    $id = $_POST['id'];
    $link = $_POST['update_link'];
    $desc = $_POST['update_desc'];
    $title = $_POST['update_title'];

    global $wpdb;
    $wpdb->update(
        'wp_images',
        array(
            'img_title' => $title,
            'img_description' => $desc,
            'img_link' => $link
        ),
        array('img_id' => $id)
    );
    die();
    return true;
}

//Deleting values in database in "wp_images" table
function delete_value()
{
    $id = $_POST['id'];
    global $wpdb;
    $wpdb->delete(
        'wp_images',
        array('img_id' => $id)
    );
    die();
    return true;
}

它会正确删除和更新,但不会立即更新。意味着如果我删除任何行,它仍然存在,但是当我刷新页面时,它会被删除。我不知道我在哪里做错了。

【问题讨论】:

  • 放置 location.reload();您的警报(结果)在哪里;代码是

标签: jquery ajax wordpress


【解决方案1】:

到目前为止,您的代码看起来不错,但您并未使用新响应刷新或清理 HTML 部分。

您的代码正在浏览帖子,但未在您的实际 DOM 中显示任何新数据,因为您没有对新响应进行编码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-01-11
    • 1970-01-01
    • 1970-01-01
    • 2015-06-22
    • 1970-01-01
    • 1970-01-01
    • 2013-05-21
    相关资源
    最近更新 更多