2013-01-06 4 views
0
<script type="text/javascript" language='javascript'> 
    $('#view_comment').submit(function() { 
     alert("msg"); 
     var sec={'post_id_for_view_comment' : $("#post_id_for_view_comment").val()} 
     $.ajax({ 
      url: "<?php echo base_url().'index.php/'; ?>post_comment/get_all_comments", 
      type: 'POST',       
      data: sec, 
      success: function(msg) { 
       alert(msg); 
      } 
     }); 
    }); 
</script> 

형태Ajax 호출은 CodeIgniter의

public function get_all_comments() 
{ 
    echo 'OK'; 
} 

Ajax 호출이 제어기에 제공되지

<form id="view_comment" method="post" > 
    <input type="hidden" name="post_id_for_view_comment" id="post_id_for_view_comment" value="<?php echo $row->post_id; ?>" /> 
    <input type="submit" id="post_button" value="View Comments" /> 
</form> 

컨트롤러에 주어지지 않는다. 나는 한 페이지에 하나 이상의 양식을 가지고있다.

+0

var sec 세미콜론을 놓치거나 잘못 입력 했습니까? –

+0

세미콜론을 넣을 위치는 어디입니까? –

+0

'var id = $ ("# post_id_for_view_comment") .Val();' '/////////////// 'data : {post_id_for_view_comment : id}' –

답변

1

여기 당신이 필요로하는 무엇을 달성 할 수있는 새로운 방법입니다. 당신이 컨트롤러에서 지금

:

function get_all_comments() 
{ 
//getting your posted sec token. 
    $sec = $this->input->post('post_id_for_view_comment'); 
    $data['res'] = "ok";// return anything you like. 
// you should use json_encode here because your post's return specified as json. see ** 
    echo json_encode($data); //$data is checked in the callback function in jquery. 
} 

는 정말 도움이되기를 바랍니다.

0

죄송합니다. jquery가 아직 준비되지 않았습니다.$.postjquery에서를 사용하여 - 아약스 호출의 유형 :

$('#post_button').click(function(e){ 
e.preventDefault; 
var sec = $('#post_id_for_view_comment').val(); 
//no need to mention index.php when using site_url() function 
$.post('<?php echo site_url("post_comment/get_all_comments")?>', 
{"post_id_for_view_comment": sec }, 
     function(data.res == "ok"){ // simple test if it returned ok 
     //here you can process your returned data. 
     }, "json"); //** 
}); 

힌트 :

$(function(){ 
     $('#view_comment').submit(function(e) { 
     var id = $("#post_id_for_view_comment").val(); 
     $.ajax({ 
      url: "<?php echo base_url()?>index.php/post_comment/get_all_comments", 
      type: "POST",       
      data: {post_id_for_view_comment:id} , 
      success: function(msg) { 
        alert(msg); 

      } 
     }); 
      e.preventDefault(); 
     }); 
    }); 
+0

다음 코드를 $ (document) .ready (function()? –

+0

예. http://stackoverflow.com/questions/1191833/how-to-run-a-function-in-j-ery에 쓸 수 있습니까? –

관련 문제