【问题标题】:I want to show an error message near to my form我想在我的表单附近显示一条错误消息
【发布时间】:2016-02-10 07:08:00
【问题描述】:

控制器 这是我的控制器功能。在 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>

【问题讨论】:

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


    【解决方案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>
    

    【讨论】:

    • 我得到它就像一个警报。好。你有什么想法可以在表格附近显示一条错误消息吗?
    • 在表单标签中放一段。并赋值 $("#errormessage").html(data.message);否则 $("#errormessage").empty();并在该段落中设置红色
    【解决方案2】:

    在您的控制器中

      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 );  
    
                }
    

    在阿贾克斯

        <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>
    

    【讨论】:

    • 在表单之后添加任何 div,例如
    猜你喜欢
    • 2019-04-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多