Here I want to fetch record and want to show in view with the use of ajax. Here I couldn't get the json data. And If I get, what will be the proper method to pass to the view. Through this, I want to get username and comments from the table and want to show in view on click event using ajax. I want to do like Facebook, when user comments, the comment shows without the page load.
Controller:
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();
}
View:
<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);
}
});
});
});