• Administrator
  •  
    Welcome to the new PunBB.biz design forum.

    We decide to switch from Forumotion due to technical limitations.

    Information: Choosing social login option will register your account automatically without need for email confirmation.
     

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

A place for codes and tutorials which will improve the functionality of your board.
Post Reply
User avatar
Shadow
Administrator
Posts: 19
Joined: Sat Jan 06, 2018 4:56 pm

Sun Jan 07, 2018 1:15 am

Description:
The following code will delete a message using AJAX.

Preview:
Image

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

Code: Select all

/**
 *! 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:

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 1 guest