dimanche 12 juin 2016

Ajax is not working properly in wordpress

I'm Developing a plugin in which i'm using ajax for deleting and updating information about the uploaded file..

Following is the code:

//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;
}

it deletes and update properly but not instantly. Means if i delete any row it remains there but when i refresh page then it is removed. I don't know where i have done mistake.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire