2015-11-05 23 views
2

我使用AJAX调用一些数据插入到MYSQL检查AJAX调用是成功与否的笨

JS代码:

$("input.addtruck").click(function (event) { 
event.preventDefault(); 
     var user_id = $("input#user_id").val(); 
    var numar = $("input#numar").val(); 
    var serie = $("input#serie").val(); 
    var marca = $("select#marca").val(); 
    jQuery.ajax({ 
     type: "POST", 
     url: "<?php echo base_url(); ?>" + "aplicatie/add_truck", 
     dataType: 'json', 
     data: {user_id: user_id, numar: numar, serie: serie, marca: marca}, 
    }); 
    success: function (res) { 
     if (res) 
     { 
      jQuery("div#truck_form").hide(); 
      jQuery("div#success").show(); 
     } else { 
      jQuery("div#error").show(); 
     } 
    } 
}); 

方法从控制器中使用:

function add_truck() { 
      $data = array(
       'user_id' => $this->input->post('user_id'), 
       'marca' => $this->input->post('marca'), 
       'serie' => $this->input->post('serie'), 
       'numar' => $this->input->post('numar') 
      ); 
//Transfering data to Model 
      $this->trucks_model->insert_truck($data); 
      $data['confirmare'] = 'Data Inserted Successfully'; 
     } 

和模型文件的方法

function insert_truck($data){ 
$this->db->insert('trucks', $data); 
} 

基本上我需要隐藏#truck_form和显示#成功,如果数据插入,或显示#error。

回答

2

您需要检查数据插入或不在数据库中在模型中使用affected_rows

M奥德尔

function insert_truck($data){ 
$this->db->insert('trucks', $data); 
$afftectedRows=$this->db->affected_rows(); 
if($afftectedRows>0) 
{ 
    return TRUE; 
} 
else{ 
    return FALSE; 
} 

} 

你需要你的回应结果控制器

控制器

function add_truck() { 
      $data = array(
       'user_id' => $this->input->post('user_id'), 
       'marca' => $this->input->post('marca'), 
       'serie' => $this->input->post('serie'), 
       'numar' => $this->input->post('numar') 
      ); 
//Transfering data to Model 
     $res=$this->trucks_model->insert_truck($data); 
     if($res){ 
     $data['msg'] = 'true'; 
     }else{ 
      $data['msg'] = 'false'; 
     } 
     echo json_encode($data); 
     } 

阿贾克斯

success: function (res) { 
     if (res.msg=='true') 
     { 
      jQuery("div#truck_form").hide(); 
      jQuery("div#success").show(); 
     } else { 
      jQuery("div#error").show(); 
     } 
    } 
1

您可以像这样创建一个响应数组。就像你的ajax dataType是json一样,你将在json中发送响应。

function add_truck() { 
      $response = array(); 
      $data = array(
       'user_id' => $this->input->post('user_id'), 
       'marca' => $this->input->post('marca'), 
       'serie' => $this->input->post('serie'), 
       'numar' => $this->input->post('numar') 
      ); 
//Transfering data to Model 
      $check_insert = $this->trucks_model->insert_truck($data); 
      if(check_insert){ 
       $response['status'] = 'true'; 
       $response['msg'] = 'Data Inserted Successfully'; 
      }else{ 
       $response['status'] = 'false'; 
       $response['msg'] = 'Problem in data insertion'; 
      } 

      echo json_encode($response); 
      die; 
     } 

,然后在阿贾克斯:

success: function (res) { 
    if (res.status == 'true') 
    { 
     jQuery("div#truck_form").hide(); 
     jQuery("div#success").show(); 
    } else { 
     jQuery("div#error").show(); 
    } 

} 
error: function (result) { 
     console.log('Problem with ajax call insert'); 
} 

和方法,从模型文件 只是为了确保插入行返回INSERT_ID

function insert_truck($data){ 
    $this->db->insert('trucks', $data); 
    $insert_id = $this->db->insert_id(); 
    return $insert_id; 
} 
1

在AJAX

<script type="text/javascript"> 
    $("#addtruck").click(function (event) { // change 
     event.preventDefault(); 

     var user_id = $("#user_id").val(); // remove input(input#user_id) 
     var numar = $("#numar").val(); 
     var serie = $("#serie").val(); 
     var marca = $("#marca").val(); 

     $.ajax(
      { 
       type: "post", 
       dataType: 'json', 
       url: "<?php echo base_url(); ?>aplicatie/add_truck",   
       data: {user_id: user_id, numar: numar, serie: serie, marca: marca}, 
      } 
     ); 
     success: function (res) { 
      if (res == TRUE) 
      { 
       jQuery("truck_form").hide(); // remove div on here 
       jQuery("success").show(); // remove div on here 
      } else { 
       jQuery("error").show(); // remove div on here 
      } 
     } 
    }); 
</script> 

在HTML

按钮应

<input type="button" id="addtruck" value="Add New Truck"> 

和形式action=""应该被删除

在控制器

function add_truck() { 
    $data = array(
     'user_id' => $this->input->post('user_id'), 
     'marca' => $this->input->post('marca'), 
     'serie' => $this->input->post('serie'), 
     'numar' => $this->input->post('numar') 
    ); 

    # passing to model 
    $res = $this->trucks_model->insert_truck($data); 

    # Check return value on $res 
    if($res == TRUE) 
    { 
     $data['msg'] = 'true'; 
    } 
    else 
    { 
     $data['msg'] = 'false'; 
    } 
    echo json_encode($data); 
} 

在型号

function insert_truck($data){ 
    $this->db->insert('trucks', $data); 
    $row_affect = $this->db->affected_rows(); 

    if($row_affect > 0) 
    { 
     return TRUE; 
    } 
    else 
    { 
     return FALSE; 
    } 

} 
0

可以成功知道阿贾克斯成功与否调用后添加错误。

jQuery.ajax({ 

     type: "POST", 
     url: "<?php echo base_url(); ?>" + "aplicatie/add_truck", 
     dataType: 'json', 
     data: {user_id: user_id, numar: numar, serie: serie, marca: marca}, 
     success: function (res) { 
      if (res) 
      { 
       jQuery("div#truck_form").hide(); 
       jQuery("div#success").show(); 
      } else { 
       jQuery("div#error").show(); 
      } 

     }, 
     error: function (xhr,err) { 

      alert("readyState: "+xhr.readyState+"\nstatus: "+xhr.status); 
      alert("responseText: "+xhr.responseText); 

     }    
    }); 
0

刚刚从代码中删除event.preventDefault(),并用成功像下面

jQuery.ajax({ 
    type: "POST", 
    url: "<?php echo base_url(); ?>" + "aplicatie/add_truck", 
    dataType: 'json', 
    data: {user_id: user_id, numar: numar, serie: serie, marca: marca}, 
    success : functionName 
}); 

function functionName(){ 

//your code for success 

}