简体   繁体   中英

php form submit issue with JS button

I have this form

 <form role="form" method='post' action='index.php' id='cme'>
    <input type="hidden" name="bonval" value="<?php echo $bonval ?>" />
    <fieldset>
        <h2 class="blink_me" style="color:green;font-size:40px;"><?php echo $bonval ?></h2>
        <div class="form-group">
            <center>
                <div class="g-recaptcha" data-sitekey="siteky"></div>
            </center>
        </div>
         <div class="row">
            <center>
                <input type="submit" name="claim" class="btn btn-lg btn-success btn-block" value="Claim Now" id="claim">
            </center>
        </div>
    </fieldset>
</form>

I have this javascript to disable button after click

<script>
$(function(){
    $('#claim').on('click',function(){
        $(this).val('Please wait ...')
        .attr('disabled','disabled');
        $('#cme').submit();
    });
});
</script>

and this is my form validation

 if(isset($_POST['claim'])) {
$recaptcha = $_POST['g-recaptcha-response'];
if(!empty($recaptcha)) {
    # Use the recaptcha function here
    $resp   =   getGoogleRecaptcha();
    if($resp['success']) {
        # Capture value from the form submit
        $bonval =   $_POST['bonval'];
        # Insert normally
        $db->fetchVal("insert into log (`user_id`,`amount`) values (?,?)", array($id, $bonval));
    } 
  } else { ?>
  <div class="overlay"><div class="popup" style="background:red;">
    <h2>Error</h2>
    <a class="close" href="#">&times;</a>
    <div> <center><span class="blink_me">Seems error</span></center></div>
  </div></div>  
   <?php  }
  }
  1. My issue is button gets disabled but form is not submitted
  2. if user refresh page form keeps getting submitted into database

Edit: sorry, lack of explanation, I read a little fast:

You can completely remove the var $ _POST after insert bdd with unset($_POST['...']);

if(isset($_POST['claim'])) {
    //code

    $db->fetchVal("insert into log (`user_id`,`amount`) values (?,?)", array($id, $bonval));

    unset($_POST['claim']);

    //code
}

You should stop the button from submitting form first by replacing the type="submit" by type="button" :

<input type="button" name="claim" class="btn btn-lg btn-success btn-block" value="Claim Now" id="claim">

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM