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

avatar
Administrator
Administrator
Description:

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

Preview:



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:




Forum Administrator, Coolyou,
Due to the volume of PMs I receive, I do not provide support via PM.
We have community forums for most support requests. If it's truly confidential, please do PM me.

Useful links:
-> Before asking question,  please use a Search option.
-> If you want to check our staff members click here.
->  Forum rules, this is a MUST READ thread.
->  FAQ (Frenquently asked questions)

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