javascript - Preventing double submission of data in forms -
suppose have textarea in form named urlslist
user input list of urls , 1 on each line.i handle submit via ajax query follows.
$(function () { $("#urlslist").submit(function(e) { //prevent default functionality e.preventdefault(); //get action-url of form var actionurl = e.currenttarget.action; //do own request handle results $.ajax({ url: actionurl, type: 'post', datatype: 'json', data: $("#urlslist").serialize(), success: function(data) { //put result in textarea here } }); }); });
then display result of processing (i.e url each input) in textarea. works want improve old urls don't submitted server via ajax while not clearing either of textareas.
edit 1
i think need demonstrate example.
suppose urlslist
textarea contain 1 url.i click on submit , result.now add url in urlslist
, click on submit again.now there 2 urls in post request.how can make sure new urls sent.
using do once lib mozilla :
function executeonce() { var argc = arguments.length, bimplglob = typeof arguments[argc - 1] === "string"; if (bimplglob) { argc++; } if (argc < 3) { throw new typeerror("executeonce - not enough arguments"); } var fexec = arguments[0], skey = arguments[argc - 2]; if (typeof fexec !== "function") { throw new typeerror("executeonce - first argument must function"); } if (!skey || /^(?:expires|max\-age|path|domain|secure)$/i.test(skey)) { throw new typeerror("executeonce - invalid identifier"); } if (decodeuricomponent(document.cookie.replace(new regexp("(?:(?:^|.*;)\\s*" + encodeuricomponent(skey).replace(/[\-\.\+\*]/g, "\\$&") + "\\s*\\=\\s*([^;]*).*$)|^.*$"), "$1")) === "1") { return false; } fexec.apply(argc > 3 ? arguments[1] : null, argc > 4 ? array.prototype.slice.call(arguments, 2, argc - 2) : []); document.cookie = encodeuricomponent(skey) + "=1; expires=fri, 31 dec 9999 23:59:59 gmt" + (bimplglob || !arguments[argc - 1] ? "; path=/" : ""); return true; }
you can make following :
// custom helper string.prototype.s2b64 = function() { // make base64 string return window.btoa(unescape(encodeuricomponent(this))); } string.prototype.replacearray = function(find, replace) { // replace array var replacestring = this; (var = 0; < find.length; i++) { replacestring = replacestring.replace(find[i], replace[i]); } return replacestring; }; // here start : var output = []; $.each($("#urlslist textarea").first().val().split(/\n/), function(index, value) { alert(index + ": " + value); if (value != '') executeonce(function(v) { output.push(value); }, null, value, 'urldone_' + value.s2b64().replacearray( ["expires", "path", "domain", "secure"], ["e_xpires", "p_ath", "d_omain", "s_ecure"]) + ''); }); alert(output);
some part may bit odd, it's bullet-proofing manual setcookie (like use of b64 , search & replace in case unlucky , randomly cookie keyword b64 output)
and if want remove saved value, here code :
var cookienames = document.cookie.split(/=[^;]*(?:;\s*|$)/); // remove match pattern (var = 0; < cookienames.length; i++) { if (/^urldone_/.test(cookienames[i])) { document.cookie = cookienames[i] + '=; expires=thu, 01 jan 1970 00:00:00 gmt; path=/'; } }
this cookie based answer (by browser, refresh & restart proof).
Comments
Post a Comment