<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调用。我在单页上有多个表单。
答案 0 :(得分: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"); //**
});
提示:使用jquery中的$.post
- 是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.
}
真的希望我帮忙。
答案 1 :(得分: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();
});
});