2016-07-13 1 views
0

레코드를 가져오고 싶습니다. 아약스를 사용하여보기에 표시하려고합니다. 여기 나는 json 데이터를 얻을 수 없었다. 그리고 제가 알기 만하면, 적절한 견해로 전달할 방법은 무엇입니까. 이를 통해 표에서 사용자 이름과 주석을 가져와 아약스를 사용하여 click 이벤트에 대한보기에 표시하려고합니다. 처럼 사용자가 댓글을 달았 으면 페이지로드없이 댓글이 표시됩니다.데이터베이스에서 레코드를 가져올 수없고 아약스를 통해보기에서로드 할 수 없습니다.

컨트롤러 :

public function get_comments() 
{ 

      $query=$this->db->query("SELECT user_name,comments FROM user_comments join user_reg where user_reg.user_id=user_comments.user_id"); 
      $temp = $query->result(); 
      foreach($temp as $row) 
      { 
       header('Content-Type: application/json'); 
       echo json_encode($row); 

      } 
      exit(); 

} 

보기 : 당신은 루프의 당신의 "에코로 json_encode"외부를 넣어 시도해야

<form action="" method="post" name="usercomments" id="usercomments"> 
      <div> 
       <textarea name="comment" form="usrform" placeholder="Enter comment here..." id="ucom"></textarea> 
       </br> 

       <div class="tab-group"> 
       <input type="submit" class="button button-block tab" id="submitbutton" value="Comment"/> 
       </div> 
      </div> 

     </form> 

    $(document).ready(function() 
    { 

     $("#submitbutton").click(function(event) 
     { 
      //alert('hiii'); 
      event.preventDefault(); 

      jQuery.ajax({ 
       type:"POST", 
       url:"<?php echo base_url();?>index.php/welcome/get_comments", 
       dataType:"json", 
       data:"", 
       success:function(data) 
       { 
        console.log(data); 
        alert(data); 
       } 


      }); 



     }); 

    }); 
+0

양식을 제출하면 어떻게됩니까? 콘솔에서 오류가 발생합니까? 응답으로 서버 측 오류가 발생합니까? 여기에 충분한 정보가 없습니다. – wrxsti

+0

양식을 제출하면 레코드가 삽입되지만 가져올 수 없습니다. 어떤 에러도없고 콘솔에서도 정보를 얻을 수 없었습니다. – Viral

답변

0

.

배열을 사용하여 행을 저장하고이 배열을 json_encode로 저장하십시오.

+0

Thnks @ J.Lgl하지만 어떻게 볼 수 있습니까? – Viral

+0

jQuery로 데이터를 사용하는 방법을 보려면이 항목 http://stackoverflow.com/questions/21040794/how-to-access-json-encoded-data-of-an-array-using-javascript를 참조하십시오. 내가보기에 귀하의 의견에 대한 HTML 구조를 볼 수 없다, 그냥 양식을 게시 할 수 있습니다. DOM을 조작하려면이 문서를 확인하십시오. http://www.w3schools.com/jquery/jquery_dom_add.asp –

0

이 줄을 따라 뭔가를 시험해보십시오. 행을보다 쉽게 ​​읽을 수있는 클라이언트 측 객체로 다시 작성하고 루프 외부에서 인코딩합니다. 또한 헤더를 그렇게 설정할 필요가 없습니다.

$query=$this->db->query("SELECT user_name,comments FROM user_comments join user_reg where user_reg.user_id=user_comments.user_id"); 
$temp = $query->result(); 
$response = array(); 
foreach($temp as $row){ 
    $user = $row['user_name']; 
    $response[$user][] = $row['user_comments']; 
} 
echo json_encode($response); 
관련 문제