'AJAX Callback returns empty string with or without json encode
I know this question has come up a lot in various forms but no answers so far have worked in my situation. I'm new to all this so please bear with me if I'm missing something obvious. I'd really appreciate help figuring this out.
The below AJAX function is returning an empty string as an alert and I can't understand why - the php pages it relates to are working and I've tried various datatypes and ways of echoing out json encoded results from php but nothing works.
AJAX FUNCTION (Note: if I change the datatype to json and then start encoding/decoding the php callback no alert shows up whatsoever. A blank alert shows up when I set it to "text")
$(document).ready(function() {
var buttonname = $("#thlikebtn").attr('name'); //get theme name
$.ajax({
url: "backgr/thloadlikes-inc.php",
type: "POST",
data: {buttonname: buttonname},
dataType: "text",
success: function(response) {
alert(response);
}
});
});
index.php includes the button mentioned in the AJAX function above: (Note: I checked that the ajax function and the php pages are being passed the button "name" as expected so that doesn't seem to be the issue)
<button type="button" name="moroccanmagic" id="thlikebtn"> <?php echo "♥";?> <span id="thlikecount"> </span></button><br>
thloadlikes-inc.php:
(Note: This page is successfully registering that "buttonname was posted and it received the data posted from AJAX so the php code doesn't seem to be the issue)
<?php
include_once "functions-inc.php";
include_once "dbconn-inc.php";
include_once "userloggedin-inc.php";
//AJAX Function to update the page with running total of likes
if (isset($_POST["buttonname"])) {
$tkeyword =$_POST["buttonname"];
echo json_encode(themetopiclikecount($conn,$tkeyword));
exit();
}
functions-inc.php includes the function being called in the above php page: (Note: This function definitely works if I call the function directly in index.php it runs as expected. Also I tried to just put the function straight into the above thloadlikes-inc.php page but that made no difference and ajax still gives a blank alert regardless)
function themetopiclikecount($conn,$tkeyword) {
$sql="SELECT tLikes
FROM themetopictracker
WHERE tKeywordphp=?"; //get all likes from perkslikes table
$stmt = mysqli_stmt_init($conn); //create a prepared statement i.e. initialise
if (!mysqli_stmt_prepare($stmt,$sql)) { //if the function above fails
echo mysqli_error($conn);
} else {
//if the statement doesn't fail, then send data input from singlechat
mysqli_stmt_bind_param($stmt,"s",$tkeyword); //first pass the parameter of the statement used, then say what kind of characters are used
mysqli_stmt_execute($stmt); //execute the desired statement
$result = mysqli_stmt_get_result($stmt); //create a variable called result that uses the connection to the db to run the above statement
while ($row =$result->fetch_assoc()) {
return $row["tLikes"];
}
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
