2013-01-11 2 views
1

특정 div 클래스를 내보기에 표시하고 싶습니다. 그러나 먼저 특정 데이터가 아약스를 통해 데이터베이스에 성공적으로 입력되었는지 확인하고 다른 다이빙 오류가 있는지 확인해야합니다 클래스는 잠시 동안 위에 나타나고 숨 깁니다.Controller에서 요청을 수신 한 후 CodeIgniter가 div를 표시합니다.

내보기 JavaScript 코드는 다음과 같습니다. 여기에 이벤트가 suceesfully 발생했을 때 대화 상자가 나타납니다.이 이벤트가 실제로 발생하면 "div success message"else div failure message라는 div 클래스로 바뀌고 싶습니다. 내가 컨트롤러에서 볼 수있는 특정 매개 변수를 전달해야하지만 그것을 어떻게 해야할지 모르겠다 생각합니다.

첫째 :

  <script type="text/javascript"> 
      $('#btn').click(function() { 

var item_name = $('#item_name').val(); 
var cat_id = $('#cat_id').val(); 

if (!item_name || item_name == 'Name') { 
    alert('Please enter Category Name'); 
    return false; 
} 

var form_data = { 
     item_name: $('#item_name').val(), 
     cat_id: $('#cat_id').val(), 
    ajax: '1'  

}; 

$.ajax({ 
    url: "<?php echo site_url('itemsController/additems'); ?>", 
    type: 'POST', 
    data: form_data, 
    success: function(msg) { 
     //$('#message').html(msg); 

     alert("items added successfully"); 
      $('#item_name').val(""); 



    } 
}); 

return false; 
    }); 


    </script> 

내 컨트롤러

function additems(){ 

    //getting parameters from view 
    $data = array(
      'item_name' => $this->input->post('item_name'), 
      'cat_id' => $this->input->post('cat_id') 

    ); 


    $is_ajax = $this->input->post('ajax'); //or use this line 
    //$this->input->is_ajax_request(); 

    $this->load->model('itemsModel'); 
    $query = $this->itemsModel->addItemstoDB($data); 


      if ($query && $is_ajax){    //if the user c validated 
     //data variable is created becx we want to put username in session 


     $page['main_content'] = 'itemsView'; 

     $this->load->view('dashboardTemplate/template',$page); 


    } 
    else 
    { 
     echo "not added"; 
    } 
    } 




    } 

모델

 class ItemsModel extends CI_Model { 


public function addItemstoDB($data){ 

    $successfull = $this->db->insert('item',$data); 


    if ($successfull){ 
     return true; 
    }else{ 
     return false; 
    } 
} 

}

답변

1

이 모든 컨트롤러에 아약스 처리하는 방법을하지입니다 나는 엉덩이 갈거야. 매화는 삽입 false되지 다음 경우 경우 모델 true 반환되는 :

는 컨트롤러 :

function additems(){ 
//your processing goes here. 
    $result = array(); 
    $this->load->model('itemsModel'); 
    $query = $this->itemsModel->addItemstoDB($data); 
    //var_dump($query); the results will be desplayed either on your page or you can see it using firebug in firefox 
    if ($query){ //&& any other condition 
     $result['res'] = 1;//process successful - replace 1 with any message 
    } 
    else 
    { 
     $result['res'] = 0;//process failed - replace 0 with any message 
    } 
     echo json_encode($result);//at the end of the function. 
    } 

은 다음 아약스 부분 :

$.ajax({ 
    url: "<?php echo site_url('itemsController/additems'); ?>", 
    type: 'POST', 
    data: form_data, 
    dataType: 'json', 
    success: function(msg) { 
     if(msg.res == 1) 
     { 
      $("your div").removeClass("error").addClass("ok"); 
     } 
     else{ 
      $("your div").removeClass("ok").addClass("error");    
      } 


    } 
}); 
+0

감사는 나에게 많은 도움이 그리고 99 일 % ... 1 %는 문제가되는 곳입니다. 코드를 구현할 때 .. $ ("your div")를 대체하십시오. removeClass ("error"). addClass ("ok"); $ ("your div"). removeClass ("ok"). addClass ("error"); 경고 상자가 true와 false 인이 두 줄 ... 그리고 대화 상자에서 false를 출력하는 스크립트를 실행하면 .. 데이터가 데이터베이스에 성공적으로 추가됩니다 .. – mynameisjohn

+0

오케이, 빠른 입력 오류. 나는 그것을 새롭게했다. 처음에 추가 된 배열 선언 :). 다른 어떤 것이 있습니까? –

+0

테스트 목적으로이 줄을 바꿉니다. – mynameisjohn

관련 문제