Good art is a taste, good design is an opinion.


Come as guest, stay as family.
Board Index

Come as guest, stay as family.


You are not connected. Please login or register

Foto

[Javascript] Delete message via AJAX (without refreshing the page)



View previous topic View next topic Go down Message [Page 1 of 1]

1
Offline

  Shadow

Shadow
Administrator
Administrator
Description:

The following code will delete a message using AJAX.
Credits: @Luiz

Preview:

[Javascript] Delete message via AJAX (without refreshing the page) VsgRm8X

Installation steps:

To use, create a new JavaScript, with the following specifications:
Name: Ajax message delete
Placement: In topics;

Code:
[panda=js]/**
 *! Delete post with AJAX.
 *
 *  @author Luiz
 *  @licence MIT
 *
 *  Copyright (c) Luiz Felipe | All rights reserved.
 */
(function ($) {
  'use strict';
 
  $(function () {
 
    $('.post a[href$="mode=delete"]')
      .on('click', function (event) {
 
        event.preventDefault();
 
        var $this = $(this);
        var $post = $this.parents('.post');
        var $pid  = $this.attr('href').replace(/^\/post\?p=(\d+)&.+/gi, '$1');
 
        var conf  = confirm('Are you sure that you want to delete a message?');
       
        if (!conf) {
          console.info('A postagem de ID ' + $pid + ' não foi deletada.');
          return false;
        }
 
        $.post('/post', {
          mode: 'delete',
          p: $pid,
          confirm: 1
        })
          .done(function () {
            $post.slideUp();
          })
          .fail(function () {
            alert([
              '[AJAX ERROR] Houve um erro ao tentar excluir a postagem de número ' + $pid,
              'Atualize a página e tente novamente.'
            ].join('\n'));
          })
        ;
      })
    ;
  });
}(jQuery));

Closing Notes:


Powered by PunBB
Currently 0 users have thanked Shadow for this post:

View previous topic View next topic Back to top Message [Page 1 of 1]


Topic URL's

URL
BBcode
HTML

Permissions in this forum:
You cannot reply to topics in this forum