Skip to content

Instantly share code, notes, and snippets.

@mercuryseries
Last active September 18, 2021 11:10
Show Gist options
  • Save mercuryseries/d66e5b6035cce01e975bd17c78133c5c to your computer and use it in GitHub Desktop.
Save mercuryseries/d66e5b6035cce01e975bd17c78133c5c to your computer and use it in GitHub Desktop.
jQuery(function () {
var larails = {
// Define the name of the hidden input field for method submission
methodInputName: '_method',
// Define the name of the hidden input field for token submission
tokenInputName: '_token',
// Define the name of the meta tag from where we can get the csrf-token
metaNameToken: 'csrf-token',
initialize: function()
{
$('a[data-method]').on('click', this.handleMethod);
},
handleMethod: function(e)
{
e.preventDefault();
var link = $(this),
httpMethod = link.data('method').toUpperCase(),
confirmMessage = link.data('confirm'),
form;
// Exit out if there is no data-methods of PUT, PATCH or DELETE.
if ($.inArray(httpMethod, ['PUT', 'PATCH', 'DELETE']) === -1)
{
return;
}
// Allow user to optionally provide data-confirm="Are you sure?"
if (confirmMessage)
{
if( confirm(confirmMessage) ) {
form = larails.createForm(link);
form.submit();
}
} else {
form = larails.createForm(link);
form.submit();
}
},
createForm: function(link)
{
var form = $('<form>',
{
'method': 'POST',
'action': link.prop('href')
});
var token = $('<input>',
{
'type': 'hidden',
'name': larails.tokenInputName,
'value': $('meta[name=' + larails.metaNameToken + ']').prop('content')
});
var method = $('<input>',
{
'type': 'hidden',
'name': larails.methodInputName,
'value': link.data('method')
});
return form.append(token, method).appendTo('body');
}
};
larails.initialize();
});
@Enigmatiic
Copy link

Good Thanks for this script

@lingotacro
Copy link

trop cool mon grand

@frankgue
Copy link

frankgue commented Jul 3, 2017

God bless, merci Grand tu rend de plus en plus performant.

@fiicode
Copy link

fiicode commented Jul 6, 2017

Ce super franchement 👍 💯

@AdameLeNgolo
Copy link

Good

@Muco-Tresor
Copy link

thx my man Mishibushi

@johnkeychishugi
Copy link

nice bro

@delmovic
Copy link

delmovic commented Feb 5, 2018

Super pratique Merci frere

@guysolamour
Copy link

Thank you man

@biston
Copy link

biston commented Jun 23, 2018

good job

@poredaka
Copy link

Merci honoré, tu nous rend la vie facile. good job!

@jhechavarria
Copy link

Super script, merci beaucoup! Ça nous simplifie vraiment la vie.

@oumar90
Copy link

oumar90 commented Nov 3, 2018

MAmamia c'est magique cette script, Merci Mercuryceries

@PrimeClet
Copy link

ça c'est un mushibishi efficace.... Merci Honoré

@JeffNucleusT
Copy link

Enfin trouvé ! Merci Honoré !

@oumar90
Copy link

oumar90 commented Aug 17, 2019

Merci Prof.

@Abrangao
Copy link

Abrangao commented Oct 4, 2019

Merci Professeur

@Ahipeau
Copy link

Ahipeau commented Oct 6, 2019

Très bon script c'est plus rapide

@kemokocde
Copy link

super

@DjibySec4
Copy link

j'aime vraiment ce script !

@mikel49
Copy link

mikel49 commented Apr 17, 2020

très cool. Thanks Honoré.

@ndemeyvan
Copy link

merci honore , jaime enormement tes lecon depuis que j'ai debute dans le code ... et desole pour les accent mon ami....c'est les ushibishi qui me derange

@Abdazz
Copy link

Abdazz commented Aug 20, 2020

Hello !
I uses this script but I'm getting a 419 error (
419 Page Expired ) ! Someone can help me ?

<a data-method="DELETE" data-confirm="Etes-vous sùr ?" class="btn btn-danger" role="button" href="{{ route('events.destroy', $event->id) }}">Supprimer</a>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment