dongzhan3937 2015-03-14 14:50
浏览 64
已采纳

php表单与jquery验证问题

** Updated with additional code **

I am redesigning a site for someone and reusing some of the php code from the previous developer. The form submits and does validation server-side on the process page. This is obviously bad from a user experience perspective. I am trying to incorporate jquery validation. I am more of a designer and have limited knowledge on php and moderate jquery.

Its a pretty straight forward form that has 2 submit options at the end. 1 button for paying with a check, 1 for paypal.

The code being used for these buttons is completely bypassing my jquery form validation. If I put a simple submit button the jquery validation kicks.

This is the code for the 2 buttons being used.

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://cdn.jsdelivr.net/jquery.validation/1.13.1/jquery.validate.js"></script>
<script>
$(document).ready(function(){
    $('form').validate();
});     
</script>

<form id="lx" class="form-horizontal" action="reg_process.php" method="post" name="regform">



    <div class="row mbs">                
             <label class="control-label col-sm-4" for="firstname"><span class="font-standout">*</span> First Name: </label>
             <div class="col-sm-8">
                <input type="text" id="firstname" name="FirstName" class="form-control input-lg mrs mls" min="2" required  />
             </div>
        </div>



    <a href="javascript:void(0)" onclick="document.regform.Submit.value = 'PayPal'; document.regform.submit()" class="btn btn-primary">
        <i class="fa fa-paypal"></i><br/>
        Pay with Paypal Online
    </a>

    <a href="javascript:void(0)" onclick="document.regform.submit()" class="btn btn-primary" >
        <i class="fa fa-check"></i><br/>
        Pay By Check
    </a> 

The buttons are simply passing either paypal or null into the form submit process page and then redirecting after that. Is there a way I can make this pass jquery validation and then submit with those values?

  • 写回答

2条回答 默认 最新

  • dongyong1400 2015-03-14 16:02
    关注

    You just need to add a rules object for the FirstName field and remove the attributes from the input element.

    Also the following doesn't work:

    // Uncaught TypeError: Cannot set property 'value' of undefined
    document.regform.Submit.value = 'PayPal';
    

    However if it did work there is a corner case, where the user clicks the paypal button but the form does not validate, the user then fixes the validation errors and submits the form with the check button. In this event the form will be submitted with Submit=PayPal.

    jQuery(function($) {
    
      var form = $('#lx'), submit = $('input[name=Submit]', form);
    
      form.validate({
        rules: {
          FirstName: {
            required: true,
            minlength: 2
          }
        }
      });
    
      // lets keep JavaScript out of the DOM
      $('#bypaypal, #bycheck').on('click', function(event) {
        var paymentType = event.target.id === 'bypaypal' ? 'PayPal' : '';
        submit.val(paymentType);
        form.submit();
      });
    
      // this is just for demo purposes, you don't need it 
      form.on('submit', function() {
        if (form.valid()) {
          alert('Form is valid: Submit="' + submit.val() + '"');
        }
      });
    });
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
    <script type="text/javascript" src="http://cdn.jsdelivr.net/jquery.validation/1.13.1/jquery.validate.js"></script>
    
    <form id="lx" class="form-horizontal" action="reg_process.php" method="post" name="regform">
    
      <div class="row mbs">
        <label class="control-label col-sm-4" for="firstname">
          <span class="font-standout">*</span> First Name:
        </label>
        <div class="col-sm-8">
          <input type="text" name="FirstName" class="form-control input-lg mrs mls" />
          <input type="hidden" name="Submit" value="" />
        </div>
      </div>
    
      <a href="javascript:void(0);" id="bypaypal" class="btn btn-primary">
        <i class="fa fa-paypal"></i>
        <br/>Pay with Paypal Online
      </a>
    
      <a href="javascript:void(0);" id="bycheck" class="btn btn-primary">
        <i class="fa fa-check"></i>
        <br/>Pay By Check
      </a>
    </form>

    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能