douwen4178 2018-04-26 13:39
浏览 91
已采纳

jQuery远程验证阻止提交按钮发布值

In my form, there is 2 submit button (save & exit, next) & remotely checking email address duplication. it's working fine. when submitting a form I'm not getting which submit button is clicked.

form.php

<form method="post" action="" id="user_reg_form" name="user_reg_form">
    <input class="form-control" id="user_id" value="<?php echo ($user->user_id!='')? $user->user_id:'0'; ?>" type="hidden" name="user_id">
    <div class="no-padding">
        <div class="col-md-12">
            <div class="form-group no-margin">
                <label>Email:</label>
                <input type="text" class="form-control" name="email" id="email" value="<?php echo ($user->email!='')? $user->email:''; ?>" maxlength="50">
            </div>
        </div>
    <div class="col-md-6">
        <div class="form-group no-margin">
            <label> Name</label>
            <input type="text" class="form-control" name="name" id="name" value="<?php echo ($user->name!='')? $user->name:''; ?>" maxlength="50">
        </div>
    </div>
    <div class="footer-btn" style="text-align:left;">
        <input type="submit" name="save_exit" id="save_exit" class="btn btn-primary"  value="Save & Exit"/>
        <input type="submit" name="next" id="next"  class="btn btn-success"  value="Next"/> 
        <a href="<?=base_url('home/user')?>" class="btn btn-default">Cancel</a> 
    </div>
</form>

jquery validation code:

$("#reg_form").validate({
    rules: {
        email:{
            required:true,
            remote: 
            {
                url: "<?php echo base_url('home/check_email_duplication')?>",   
                type: "post",
                data: { 'user_id': function() { return $("#user_id").val()},'email': function() { return $("#email").val()}},           
            },
        },
        name: "required",
    },
});

After form submit print $_POST array:

User Controller Action:

public function user_reg()
{
    print_r($_POST);
}

Button 1 Click:

Array ( 
    [user_id] => 14 
    [email] => test@test.com
    [name] => yyyy 
)

Button 2 Click:

Array ( 
    [user_id] => 14 
    [email] => test@test.com
    [name] => yyyy 
)

But I Need below-expected Response in the $_POST array: Need submit button value to find out which submit button.

Button 1 Click:

Array ( 
    [user_id] => 14 
    [email] => test@test.com
    [name] => yyyy 
    [next] => Next 
)

Button 2 Click:

Array ( 
    [user_id] => 14 
    [email] => test@test.com
    [name] => yyyy 
    [save_exit] => Save & Exit 
)
  • 写回答

2条回答 默认 最新

  • doukesou4452 2018-05-07 13:10
    关注

    Finally, I found the answer which is worked for me.

    There is a change in jquery code. I've added 2 changes ignore: [] & async: false in remote condition this solved my issue.

    jquery form validation code:

    $("#reg_form").validate({
        ignore: [],
        rules: {
            email:{
                required:true,
                remote: 
                {
                    url: "<?php echo base_url('home/check_email_duplication')?>",   
                    type: "post",
                    data: { 'user_id': function() { return $("#user_id").val()},'email': function() { return $("#email").val()}}, 
                    async: false,         
                },
            },
            name: "required",
        },
    });
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 高德地图点聚合中Marker的位置无法实时更新
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办