jquery - Disabling submit button stops form from being submitted PHP / Javascript -


i think got classic problem not able find working solution far.

i have got form , user clicks "send" , works fine using prg pattern , doing both client-side , server-side validation.

the problem arises when user (let's assume entered valid inputs) clicks more once before server script ends execution...

i not duplicated entry because took care of browser not go "thanks submitting page". instead re-submits same page same values , custom errors set warn user trying enter details stored in database. details sent in first place in database user has no chance know that.

i tried disable submit button on submit event using jquery in case data not submitted.

html

   <div id="send-button-container">    <input id="send-emails" type="submit" name="send_emails" value="send"/>    </div> 

jquery

   $(document).ready(function(){          $('#mail-form').submit(function(){         $('#send-emails').attr('disabled','disabled');         });    }); 

i wondering if can force submission using javascript after disabling button , how deal uas javascript disabled

thanks in advance

i assume means depending on value of submit button service request? checking

$_request['send_emails'] == 'send'; 

this not practice. should never depend on value of submit button because displayed user. instead, should add hidden input contains event want fire. after form submitted, don't need care value of submit button , can disable it. other non-disabled data in form still submitted.

you can indeed force submission after disabling button.

$(function () {    $("#mail-form").submit(function () {       $("#send-emails").attr('disabled', 'disabled');       window.location = '?' + $("#mail-form").serialize() + '&send_mails=send';       return false;    }); }); 

server side set $_session variable keeps track of last time made submission , block submissions within time.

<?php session_start(); if (isset($_request['send_emails'])) {    if (isset($_session['mail_sent'])    && strtotime($_session['mail_sent']) < strtotime('5 seconds ago')    ) {       redirect_to_thanks();    }    do_post(); }  function do_post() {    if (do_validate()) {       $_session['mail_sent'] = time();       redirect_to_thanks();    }    else {       yell_at_user_a_lot();    } } ?> 

Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -

PostgreSQL 9.x - pg_read_binary_file & inserting files into bytea -