重载页面后,ajax不保存表单中的输入信息

问题描述 投票:0回答:1

我试图使用Ajax来保留用户输入的信息,但由于某些原因,它没有工作。如果出现错误,我的控制器重定向到带有错误数据的视图,然而,在页面上传后,表单是空的。我使用的是Codeigniter的MVC模型。

$("#save").click(function () {

        var tnum1 = $("#tnum1").val();
        var tnum2 = $("#tnum2").val();
        var tnum3 = $("#tnum3").val();
        var loc = $("#loc").val();
        var dine = $("#dine").val();
        var date = $("#date").val();
        var time = $("#time").val();
        var phone = $("#phone").val();
        var fullname = $("#fullname").val();
            $.ajax({
                type: 'POST',
                url: "<?php echo site_url(); ?>" + "/hosting/create",
                data: {tnum1:tnum1, tnum2:tnum2, tnum3:tnum3, loc:loc, dine:dine, 
                date:date, time:time, phone:phone, fullname: fullname},
                error: function () {
                    alert( "Load was performed.");
                },
                success: function (data) {
                    if (data === "") {

                        window.location.href = "<?php echo site_url('hosting/tableMap'); ?>";
                    }
                    else {
                        $("#error").html(data);
                    }
                }
            });
    });

控制器

public function create() {  

            $newDate = date("Y-m-d",strtotime($this->input->post('re_date')));
            $newTime = date("H:i", strtotime($this->input->post('re_time')));
            $data = array(
                'num' => $this->input->post('num'),
                'location' => $this->input->post('location'),
                'diners' => $this->input->post('diners'),
                're_date' => $newDate,
                're_time' => $newTime,
                'phonenumber' => $this->input->post('phonenumber'),
            );

            $dataclient = array(
                'fullname' => $this->input->post('fullname'),
                'phonenumber' => $this->input->post('phonenumber'),
            );

            $error = $this->validation($data,$dataclient);

        if ($error) {
              $data['error'] = $this->session->set_flashdata('error','<b><u>Failed! </u></b>'.$error.'');
                redirect(base_url("/hosting/tableMap"));
        } else {

            $this->Hosting_model->form_insert($data, $dataclient);

            }

}
ajax codeigniter
1个回答
0
投票

如果你重定向控制器,那么它将不会保留以前的值。相反,将错误保存在一个变量中,并将其返回到ajax函数中。这就是ajax的全部意义--不重定向或重载页面,即异步地完成任务。

redirect(base_url("/hosting/tableMap")); // redirecting on error

然后在你的控制器中

if ($error) {
    // data['error'] = $this->session->set_flashdata('error','<b><u>Failed! </u></b>'.$error.''); // remove this as page will not reload, flashdata wouldn't work
    // redirect(base_url("/hosting/tableMap"));
    $ajax_data['error'] = 1; // if error exists then value
    $ajax_data['validation_error'] = $error;
} else {
    $this->Hosting_model->form_insert($data, $dataclient);
    $ajax_data['error'] = 0;  // if error doesn't exist then value
}
return json_encode($ajax_data); // or echo json_encode($ajax_data);

现在,为了防止表单提交时的默认动作,即重定向页面,请使用

$("#save").click(function (e) { 
    e.preventDefault();
    // rest of your code

然后在你的ajax成功。

if (data.error == 0) {  // no error
    window.location.href = "<?php echo site_url('hosting/tableMap'); ?>";
}
else {  // error
    $("#error").html(data); // do whatever you want to do with error
    // errors can be retrieved from "data.validation_error" -- it will be an array probably sp you'll have to loop through each element
}
© www.soinside.com 2019 - 2024. All rights reserved.