我想在我的表单附近显示错误消息

时间:2016-02-10 07:08:01

标签: php ajax codeigniter codeigniter-2 codeigniter-3

控制器    这是我的控制器功能。在addinvoices函数中,调用一个检查函数。如果条件为真,页面将被重定向到视图页面,否则保持在同一页面上并显示错误消息。

    $this->load->helper(array('form', 'url'));

    $this->load->helper('file');
    $ip = $_SERVER['REMOTE_ADDR'];
    $sales = 2;
    $userid = $this->input->post('userid');
    $referenceid = $this->input->post('txtPOCJ');
    $entering_amount = $this->input->post('txtAmount');
    $netamount = $this->input->post('checkamount');
    $checking = $this->check_data($userid, $referenceid, $entering_amount, $netamount);

    $data7 = array(
        'inv_reftype' => $sales,
        'inv_userid' => $this->input->post('userid'),
        'inv_refid' => $this->input->post('txtPOCJ'),
        'inv_amount' => $this->input->post('txtAmount'),
        'inv_paymethod' => $this->input->post('sbPaymethod'),
        'inv_status' => $this->input->post('sbPaystatus'),
        'inv_dated' => time(),
        'inv_ipadd' => $ip
    );
    if ($checking == '2') {
        $inserted_id = $this->pocustomer_model->invoiceinsert($data7);
        $response = array('id' => $inserted_id, 'message' => "inserted successfully");
        echo json_encode($response);
        die();
    } else {
        $array = array(
            "message1" => "Error"
        );

        $datamessage['json'] = $array;
        echo json_encode($data);  

    }
}

 public function check_data($userid, $referenceid, $entering_amount, $netamount) {
        $find_total = $this->pocustomer_model->findsum($userid, $referenceid);
        foreach ($find_total as $new_total) {
            $get_value = $new_total->total;
        }
        $new_amount = $netamount - $get_value;
        if (($entering_amount <= $new_amount) && ($entering_amount != '0')) {
            return 2;
        } else {
            return false;
        }
    }

视图页面中的Ajax功能

<script type="text/javascript">
    $('#submit-invoice').click(function () {
   var formData = new FormData($('#myform')[0]);
        $.ajax({
            url: "<?php echo base_url() ?>moderator/Invoices/addinvoices",
            type: 'POST',
            data: formData,
            dataType: "Json",
            mimeType: "multipart/form-data",
            contentType: false,
            cache: false,
            processData: false,
            success: function (data) {
                var last_inserted_id = data.id;
                window.location.href = "<?php echo base_url() ?>moderator/Invoices/viewinvoices/" + last_inserted_id;
            }   
        });
    return false;
    });
</script>

2 个答案:

答案 0 :(得分:1)

控制器

 if ($checking == '2') {
            $inserted_id = $this->pocustomer_model->invoiceinsert($data7);
            $response = array('id' => $inserted_id,'type' => 'success' 'message' => "inserted successfully");
            echo json_encode($response);
            die();
        } else {
            $array = array(
                "type" => "error",
                "message" => "Invalid value, pls try again"
            );

            echo json_encode($array );  

        }

查看文件

<script type="text/javascript">
    $('#submit-invoice').click(function () {
   var formData = new FormData($('#myform')[0]);
        $.ajax({
            url: "<?php echo base_url() ?>moderator/Invoices/addinvoices",
            type: 'POST',
            data: formData,
            dataType: "Json",
            mimeType: "multipart/form-data",
            contentType: false,
            cache: false,
            processData: false,
            success: function (data) {

if(data.type== 'error') {
alert(data.message);
} else {
                var last_inserted_id = data.id;
                window.location.href = "<?php echo base_url() ?>moderator/Invoices/viewinvoices/" + last_inserted_id;
}

            }   
        });
    return false;
    });
</script>

答案 1 :(得分:1)

在你的控制器中

  if ($checking == '2') {
                $inserted_id = $this->pocustomer_model->invoiceinsert($data7);
                $response = array('id' => $inserted_id,'type' => 'success' 'message' => "inserted successfully");
                echo json_encode($response);
                die();
            } else {
                $array = array(
                    "message" => "Error Message"
                );

                echo json_encode($array );  

            }

在Ajax中

    <script type="text/javascript">
    $('#submit-invoice').click(function () {
   var formData = new FormData($('#myform')[0]);
        $.ajax({
            url: "<?php echo base_url() ?>moderator/Invoices/addinvoices",
            type: 'POST',
            data: formData,
            dataType: "Json",
            mimeType: "multipart/form-data",
            contentType: false,
            cache: false,
            processData: false,
            success: function (data) {

if(data.message) {
$("#divid").html(data.message);
} else {
                var last_inserted_id = data.id;
                window.location.href = "<?php echo base_url() ?>moderator/Invoices/viewinvoices/" + last_inserted_id;
}

            }   
        });
    return false;
    });
</script>
相关问题