用JS按钮提交php表单问题

时间:2017-01-10 15:09:23

标签: javascript php jquery html forms

我有这个表格

 <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>

点击后我有这个javascript来禁用按钮

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

这是我的表单验证

 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. 我的问题是按钮被停用但表单未提交
  2. 如果用户刷新页面表单不断提交到数据库

2 个答案:

答案 0 :(得分:1)

编辑:抱歉,缺乏解释,我读得快一点:

使用unset插入bdd后可以完全删除var $ _POST($ _ POST ['...']);

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

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

    unset($_POST['claim']);

    //code
}

答案 1 :(得分:0)

您应该首先通过type="submit"替换type="button"来停止按钮提交表单:

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

希望这有帮助。