Reputation: 436
I am trying to create vote up in my codeigniter named "like". I use this method, that i just found in http://www.technicalkeeda.com/codeigniter-tutorials/voting-system-using-jquery-ajax-and-php-codeigniter-framework
And update is run successfully on database, But the problem is the result of like in view is not showing and the output is text from localhost "Sorry unable to update". And when i refresh the page, the result of "like" showing up.
This is the Controller:
function voteme(){
$id_vote= $this->input->post('id_vote');
$upOrDown= $this->input->post('upOrDown');
$status ="false";
$updateRecords = 0;
if($upOrDown=='upvote'){
$updateRecords = $this->home_model->updateUpVote($id_vote);
}
if($updateRecords>0){
$status = "true";
}
echo $status;
$data['id_vote'] = $id_vote;
redirect ('home',$data);
}
This is Model:
function updateUpVote($id_vote){
$sql = "UPDATE vote set like = like+1 WHERE id_vote =?";
$this->db->query($sql, array($id_vote));
return $this->db->affected_rows();
}
Here is the view:
<button class="btn btn-success voteme" id="3_upvote"><span id="<?php echo $data['id_vote']?>_upvote_result" ><?php echo $data['like']?></span> <b>Like</b></button>
This is javascript:
<script type="text/javascript">
$(document).ready(function(){
$(".voteme").click(function() {
var id_vote = this.id;
var upOrDown = id_vote.split('_');
$.ajax({
type: "post",
url: "http://localhost/ngelol/home/voteme",
cache: false,
data:'id_vote='+upOrDown[0] + '&upOrDown=' +upOrDown[1],
success: function(response){
try{
if(response=='true'){
var newValue = parseInt($("#"+id_vote+'_result').text()) + 1;
$("#"+id_vote+'_result').html(newValue);
}else{
alert('Sorry Unable to update..');
}
}catch(e) {
alert('Exception while request..');
}
},
error: function(){
alert('Error while request..');
}
});
});
});
How to get this result of like without refreshing page?
Many Thanks....
Update:
I have new problem guys, does anyone know how to redirect page in javascript.. I mean, to use vote we need to login first, then i replace
alert('Sorry Unable to update..');
with
url: "localhost/latihan/login";
but it didn't work... thanks for help before
Upvotes: 2
Views: 1236
Reputation: 1981
To redirect with javascript, you can use 'window.location.href' :
window.location.href = "localhost/latihan/login";
Upvotes: 0
Reputation: 260
Try this:
function voteme(){
$id_vote= $this->input->post('id_vote');
$upOrDown= $this->input->post('upOrDown');
$status ="false";
$updateRecords = 0;
if($upOrDown=='upvote'){
$updateRecords = $this->home_model->updateUpVote($id_vote);
}
if($updateRecords>0){
$status = "true";
}
return $status
}
Upvotes: 0
Reputation: 40639
Problem is in your controller function voteme
after echoing status
you should not use redirect
, if you use redirect then it will return your home page html with true
status.
So, you can use is_ajax_request(), if ajax request is true then use exit;
otherwise redirect()
it like,
function voteme(){
$id_vote= $this->input->post('id_vote');
$upOrDown= $this->input->post('upOrDown');
$status ="false";
$updateRecords = 0;
if($upOrDown=='upvote'){
$updateRecords = $this->home_model->updateUpVote($id_vote);
}
if($updateRecords>0){
$status = "true";
}
echo $status;
// check for ajax request
if(! $this->input->is_ajax_request()){ // if not ajax request
$data['id_vote'] = $id_vote;
redirect ('home',$data);// redirect it
}
exit;
}
Even your referral link is not using redirect()
Upvotes: 1