在jquery验证时将一些数据从模型或控制器返回到submithandler

时间:2015-05-29 18:01:36

标签: php jquery twitter-bootstrap codeigniter jquery-validate

我在模态引导程序中有一个表单,如下所示:

    <?php
$properties = array('class' => 'form-horizontal', 'id' => 'formpassword');
echo form_open("control_member/updatePassword", $properties);
?>
<div class="modal hide fade" id="myModalChangePassword">
    <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal">×</button>
        <h2 id="mainTitle" style="color: black; text-shadow: none;">Silahkan ganti Password Anda: </h2>
    </div>

    <div class="modal-body">
        <fieldset>

            <div class="input-prepend" title="Password">
                <span class="add-on"><i class="halflings-icon lock"></i></span>
                <input  class="input-xlarge"  name="password_lama" id="old_password" type="password" placeholder="Current password" autofocus>
            </div>

            <div class="input-prepend" title="Password Baru">
                <span class="add-on"><i class="halflings-icon lock"></i></span>
                <input class="input-xlarge" name="password_baru" id="new_password" type="password" placeholder="New Password"/>
            </div>

            <div class="input-prepend" title="Confirm Password">
                <span class="add-on"><i class="halflings-icon lock"></i></span>
                <input class="input-xlarge" name="password_confirm" id="confirm_password" type="password" placeholder="Confirm Password"/>
            </div>

            <div>   
                <button class="btn btn-primary" id="btn-save-password">Save</button>
                <button class="btn" data-dismiss="modal">Cancel</button>

            </div>

            <div class="clearfix"></div>
        </fieldset>

    </div>
</div>

<?php echo form_close(); ?>  

这是处理上述代码数据的控制器:

public function updatePassword() {
    $new = $this->input->post('new_password');
    $user_id = $this->session->userdata('id_user');

    $row = $this->model_user->update_password($user_id, $new);

    $affected_rows = $this->db->affected_rows();

    if ($affected_rows > 0) {
        //echo json_encode($row); just test, the browser return true
        return json_encode($row);
    } else {
        return "No updates";
    }     
}

现在,我使用jquery验证来验证上面代码中的数据,如下所示:

$(document).ready(function() {
    $("#formpassword").validate({
        rules: {
            old_password : {
                required: true,
            },
            new_password : {
                required: true,
            },
            confirm_password : {
                equalTo: "#new_password"
            }

        },
        messages: {
            old_password : "Password lama anda salah",
            new_password : "Password baru tidak cocok",
            confirm_password : "Tidak cocok"

        },
        submitHandler: function(form) {
            alert("Working"); // Not success
            $.blockUI({message: '<h1><img src="http://localhost/tresnamuda/assets/img/loader.gif" />Sedang diproses</h1>'});

            $.ajax({
                method: "POST",
                url: form.action,
                data: form.serialize()
            })
                .done(function(msg) {
                    $.blockUI({message: "Data Submited "});
                });


        }

    });

我的目标是,当用户成功时,如何关闭模态引导程序,以及数据是否成功更新? Coz,在我的情况下,它重定向到控制器updatePassword。所以,我预计,当SUbmit成功时,它仍然会给我一个相同的页面。

0 个答案:

没有答案