Thursday 15 September 2011

php - jQuery AJAX fails to update database after full page is finished loading -



php - jQuery AJAX fails to update database after full page is finished loading -

this first time using jquery ajax bear me. trying create button website similar facebook button. user must logged on via facebook pull userid database. when user clicks on button, inserts row database user info , page info. if there entry user , page, update row.

my code runs fine on localhost. can click on button day , image alter , forth unlike , back. database updates every time no problem.

when upload live server, script runs fine point. 1 time page finishes loading completely, database no longer updates click. images alter usual , receive alert('success') no changes made actual database.

<?php $tempstat = '1'; if ($stmt = $mysqli -> prepare("select count(id) votes businessid=? , status=? ")) { $stmt -> bind_param('ss', $businessid, $tempstat); $stmt -> execute(); $stmt->bind_result($likecount); $stmt->fetch(); $stmt->close(); } if ($likecount == '') { $likecount = 0; } ?> <style> #karmabar { padding-left: 4px; } #counter { margin: 0 auto; text-align: center; width: 50px; height: 25px; background-image: url('/images/karma-counter.png'); } p#counter { font-size: 12px; padding-top: 5px; } .karma { margin: 0 auto; font-size: 16px; color: blue; text-align: center; padding: 5px; } </style> <?php if(login_check($mysqli) == true) { $userid = $_session['userurl']; if ($stmt = $mysqli -> prepare("select status votes userid=? , businessid=? ")) { $stmt -> bind_param('ss', $userid, $businessid); $stmt -> execute(); $stmt -> bind_result($like_status); $stmt->fetch(); $stmt->close(); } if ($like_status == '' or $like_status == null) { $like_status = '0'; } ?> <script type="text/javascript"> $(document).ready(function() { $('#like_post').click(function() { $.ajax({ url: "/includes/like.php", type: "get", data: 'userid=<?php echo $userid; ?>&businessid=<?php echo $businessid; ?>&like_status=1', success: function() { alert("success"); }, error: function() { alert("something went wrong"); } }); $('#counter').html(function(i, val) { homecoming val*1+1 }); $('#like_post').hide(); $('#unlike_post').show(); }); $('#unlike_post').click(function() { $.ajax({ url: "/includes/like.php", type: "get", data: 'userid=<?php echo $userid; ?>&businessid=<?php echo $businessid; ?>&like_status=0', success: function() { alert("success"); }, error: function() { alert("something went wrong"); } }); $('#counter').html(function(i, val) { homecoming val*1-1 }); $('#unlike_post').hide(); $('#like_post').show(); }); }); </script> <div id="karmabar"> <table cellpadding="0px"> <?php if ($like_status == '0') { ?> <tr><td><a href="javascript:;" id="unlike_post" class="hide"><img src="/images/karma-active.png" title="undo karma" /></a><a href="javascript:;" id="like_post"><img src="/images/karma-inactive.png" title="spread karma" /></a><td><p id="counter"><?php echo $likecount; ?></p></td></tr> <?php } else { ?> <tr><td><a href="javascript:;" id="unlike_post"><img src="/images/karma-active.png" title="undo karma" /></a><a href="javascript:;" id="like_post" class="hide"><img src="/images/karma-inactive.png" title="spread karma" /></a></td><td><p id="counter"><?php echo $likecount; ?></p></td></tr> <?php } ?> </table> </div> <?php } else { ?> <div id="karmabar"> <table cellpadding="0px"> <tr><td><a href="#" onclick="alert('login facebook spread karma');" ><img src="/images/karma-inactive.png" title="spread karma" /></a><td><p id="counter"><?php echo $likecount; ?></p></td></tr> </table> </div>

$businessid , $userid both defined higher on page.

like.php

<?php include_once $_server['document_root'].'/includes/db_connect.php'; include_once $_server['document_root'].'/includes/functions.php'; $userid = $_get['userid']; $businessid = $_get['businessid']; $ip = $mysqli->real_escape_string(getclientip()); $like_status = $_get['like_status']; if ($stmt = $mysqli -> prepare("select count(id) votes userid=? , businessid=? ")) { $stmt -> bind_param('ss', $userid, $businessid); $stmt -> execute(); $stmt ->bind_result($count); $stmt ->fetch(); $stmt ->close(); } if ($count == '1') { if ($stmt = $mysqli -> prepare("update votes set ip=?, status=? userid=? , businessid=? ")) { $stmt -> bind_param('ssss', $ip, $like_status, $userid, $businessid); $stmt -> execute(); $stmt -> close(); $mysqli -> close(); } } if ($count == '0') { if ($stmt = $mysqli -> prepare("insert votes (userid, businessid, ip, status) values ( ?, ?, ?, ? ) ")) { $stmt -> bind_param('ssss', $userid, $businessid, $ip, $like_status ); $stmt -> execute(); $stmt -> close(); $mysqli -> close(); } } ?>

i appreciate help guys can give me. said, i'm new kind of coding , looking pointers may have.

mike, i'm not sure what's going on, maybe can seek using type of ajax request / php returns problem shoot:

replace entire ajax function this:

$(document).ready(function() { $('#like_post').click(function() { $.post("/includes/like.php", {userid:<?php echo $userid; ?>, businessid:<?php echo $businessid; ?>, like_status:"1"}, function(data){ alert(data); //check see beingness returned if(data == 1){ //later, can echo "1" in php file on success //this success alert("success"); $('#counter').html(function(i, val) { homecoming val*1+1 }); $('#like_post').hide(); $('#unlike_post').show(); } else { //this failure alert("failure"); } }); }); $('#unlike_post').click(function() { $.post("/includes/like.php", {userid:<?php echo $userid; ?>, businessid:<?php echo $businessid; ?>, like_status:"0"}, function(data){ alert(data); //check see beingness returned if(data == 1){ //later, can echo "1" in php file on success //this success alert("success"); $('#counter').html(function(i, val) { homecoming val*1-1 }); $('#unlike_post').hide(); $('#like_post').show(); } else { //this failure alert("failure"); } }); }); });

and here php - notice, changed $_gets $_posts , added echo @ end. explain why after.

<?php include_once $_server['document_root'].'/includes/db_connect.php'; include_once $_server['document_root'].'/includes/functions.php'; $userid = $_post['userid']; $businessid = $_post['businessid']; $like_status = $_post['like_status']; $ip = $mysqli->real_escape_string(getclientip()); if ($stmt = $mysqli -> prepare("select count(id) votes userid=? , businessid=? ")) { $stmt -> bind_param('ss', $userid, $businessid); $stmt -> execute(); $stmt ->bind_result($count); $stmt ->fetch(); $stmt ->close(); echo "got count: " . $count; } if ($count == '1') { echo "the count 1"; if ($stmt = $mysqli -> prepare("update votes set ip=?, status=? userid=? , businessid=? ")) { $stmt -> bind_param('ssss', $ip, $like_status, $userid, $businessid); $stmt -> execute(); $stmt -> close(); $mysqli -> close(); echo "and post successful (1)"; } } if ($count == '0') { echo "the count 0"; if ($stmt = $mysqli -> prepare("insert votes (userid, businessid, ip, status) values ( ?, ?, ?, ? ) ")) { $stmt -> bind_param('ssss', $userid, $businessid, $ip, $like_status ); $stmt -> execute(); $stmt -> close(); $mysqli -> close(); echo "and post successful (0)"; } } echo "finished"; ?>

basically - i've added in bunch of echos able 'listen' $.post ajax phone call when returns data. if successful, should sentence returned saying "got count: 1thecount 1 , post successful(1)finished". of echos should fire when post successful. if don't 1 of sentences, has gone wrong.

try problem shoot , allow know comes back.

php jquery ajax mysqli

No comments:

Post a Comment