簡體   English   中英

如何通過Codeigniter中的Ajax和Jquery顯示錯誤

[英]How to show the errors through ajax and Jquery in codeigniter

這是我的ajax電話

function exportCSV(){
        var sampleid = $("#sampleid").val();
        var scheme = $("#scheme").val();
        var v = $("#v").val();
        var date = $("#date").val();
        var assignedvalue = $("#assignedvalue").val();
        var units = $("#units").val();
        var assayvalue = $("#assayvalue").val();
        var analyte = $("#analyte").val();
        var filename=$("#filename").val();
        var sample_error=$("#sample_error").val();
        $.ajax({
            type: "POST",
            url: "<?php echo base_url(); ?>" + "import/validate_file",
            dataType: 'json',
            data: {
                sampleid: sampleid,
                scheme: scheme,
                v: v,
                date: date,
                assignedvalue: assignedvalue,
                units: units,
                assayvalue: assayvalue,
                analyte: analyte,
                filename:filename,
                sample_error: sample_error
            },
            success: function (data) {
                console.log(data);  //as a debugging message.
            }

        });
    }

這是我的控制器

<?php
if (!empty($unit_check) and !empty($analyt) and !empty($sch) and count($sample_id) == count(array_unique($sample_id)) and $assigned_check == '1' and $assay_check == '1') {
    for ($row = 2; $row <= $lastRow; $row++) {
        $data['sample_id']      = $worksheet->getCell($sampleid . $row)->getValue();
        $data['scheme']         = $worksheet->getCell($scheme . $row)->getValue();
        $data['v']              = $worksheet->getCell($v . $row)->getValue();
        $data['units']          = $worksheet->getCell($unit . $row)->getValue();
        $data['date']           = $worksheet->getCell($date . $row)->getFormattedValue();
        $data['assay_value']    = $worksheet->getCell($assayvalue . $row)->getValue();
        $data['assigned_value'] = $worksheet->getCell($assignedvalue . $row)->getValue();
        $data['analyte']        = $worksheet->getCell($analyte . $row)->getValue();
        $data['trace_id']       = $insert_id;
        $this->import_model->insert_data($data);
        $response['success'] = true;
    }
} else {
    $data['sample_id']      = '';
    $data['analyte']        = '';
    $data['unit_check']     = '';
    $data['sch']            = '';
    $data['assigned_value'] = '';
    $data['assay_value']    = '';
    if (count($sample_id) != count(array_unique($sample_id))) {
        $data['sample_id'] = '1';
    }
    if (empty($analyt)) {
        $data['analyte'] = '1';
    }
    if (empty($unit_check)) {
        $data['unit_check'] = '1';
    }
    if (empty($sch)) {
        $data['sch'] = '1';
    }
    if ($assigned_check == '') {
        $data['assigned_value'] = '1';
    }
    if ($assay_check == '') {
        $data['assay_value'] = '1';
    }
    $data['file_name'] = '';
}
?>

我必須在ajax調用中顯示錯誤和成功消息。

現在,我已經成功評估了數據並將其放入數據庫中。

但是我想通過單擊提交按鈕在頁面末尾顯示成功消息。

如果存在驗證錯誤,則必須在頁面末尾的該字段中顯示錯誤

任何幫助,將不勝感激。

這是您成功使用ajax的方法

success: function (data) {
   $("#resultDiv").html(data)
}

無論成功還是失敗,都從控制器返回一些真實數據。 並根據您的數據成功方法里面顯示您的消息。 喜歡:

success: function (data) {
     $("#resultDiv").html(data.success) //this requires string to convert your result in string if neccessary
     //But you should return a JSON data as msg from your controller
}

您應該放置一個結果HTML元素,例如:

<div id='resultDiv'></div> <!-- to match with #resultDiv -->

如果success=true ,否則將響應數據置於兩個條件下,否則success=false

在您的控制器中

if(.....){
  //what ever check you wanna do
  ..........
  ..........
  $response['msg']='success';
  header('Content-Type', 'application/json');
  echo json_encode($response);
}
else{
  $response['msg']='failed';
  header('Content-Type', 'application/json');
  echo json_encode($response);
}

在你的ajax中

success: function (data) {
   $("#resultDiv").html(data.msg)
}

嘗試類似:

$response = array(
    'errCode' = 0,
    'errMsg'  = 'msg'
);

通過json_encode()將這種數組從php返回到ajax()調用,並在ajax()成功中使用它,例如:

var data = JSON.parse(response);

alert(data.errMsg);

您還可以檢查errCode,例如:

if(errCode == 0) { something }
if(errCode == 1) { something }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM