Reputation: 1065
<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
<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>
Controller
public function get_all_comments()
{
echo 'OK';
}
Ajax call is not given to controller. I have more than one forms on single page.
Upvotes: 0
Views: 118
Reputation: 8528
Here's a new way to achieve what you need:
$('#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"); //**
});
HINT: using $.post
from jquery - is type of ajax calling.
now in you controller:
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.
}
Really hope that I helped.
Upvotes: 1
Reputation: 5342
i'm sorry, jquery is not ready yet.
$(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();
});
});
Upvotes: 0