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>  

形式

<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> 

控制器

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

Ajax 调用未提供给控制器。我在单页上有多个表格。

4

2 回答 2

1

这是实现您需要的新方法:

$('#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"); //**
});

提示:使用$.post来自jquery - 是 ajax 调用的类型。

现在在你的控制器中:

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.
}

真的希望我有所帮助。

于 2013-01-07T01:21:31.837 回答
0

对不起,jquery 还没有准备好。

    $(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();
        });
     });
于 2013-01-06T05:25:53.350 回答