Grab the sample api.php from here:
pastebin.com/hacvyzUr
The file is a fairly simple HTML POST implementation, and does not include any sort of authentication. I'll leave that to you. This is not RESTful. That's a whole other sort of implementation. This is just normal $_POST processing, but returning JSON instead of plain text. You could, of course, use this to return plain text, if you wanted.
It includes a few helper functions:
ajaxResponse() - creates a JSON response for returning results
ajaxError() - creates a JSON response for returning error messages
ajaxSuccess() - creates a JSON response with just "ok" as the result
The processing starts at the very bottom of the file as I like to define functions at the top. The first thing it does is call evalCommand(). That's where all the main processing happens. The evalCommand() function will get the "cmd" $_POST variable to see which command to process. Follow the flow to see how that works. I usually put each command's process into a separate function, or in a larger system, even other objects. But keeping it simple here, just separate functions.
To call it, you'd do an ajax call similar to what you're already doing. You'll need to make sure you set a dataType parameter to 'json', though, so that the browser knows the returned data is in JSON format. The data parameter will have at least one element called "cmd", with the name of the command. You can call the command whatever you'd like, but it has to match one of the ones you're looking for in the evalCommand() function in api.php.
Here's an example of what a call might look like to "like" a post. Notice, the dataType is "json", and data has three parameters, "cmd", "postid" and "userid". The cmd is set to "like-post". The postid is set to whatever the id of the post is, and the userid is set to the id of the user liking the post. In api.php, there is a stub function with pseudocode that is there for you to fill out to update the number of likes for a post.
var postid = 1;
var userid = 1;
$.ajax({
url: 'api.php',
type: 'POST',
dataType: 'json',
cache: false,
data: {
cmd: 'like-post',
postid: postid,
userid: userid
},
success: function(response) {
if (response.error) {
console.log(response.error);
return;
}
$('#likes-count').text(response.response+ ' likes');
}
});
Hope this helps, and hope it makes some sense to you. It might take you some time to get it working the way you want.
Piece of advice: if your ajax call doesn't return anything, change dataType to 'html', then put console.log(response) as the first thing in the success function and look at the developer console in the browser to see if there are any PHP error messages. If so, take care of them. When those messages go away and you get the result in the console, then switch the dataType back to 'json' and remove that console.log line.
Joe Clark
Full-stack developer specializing in healthcare IT
There's no end of your script after returning your likes, so it's returning not only your likes, but also the entire script.
First, why are you echoing out all the likes?
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { echo $row['likes']; }Don't you want to count them and echo a count?
Second, end your script at the end of your this if statement:
if (isset($_POST['liked'])) { . . . // Make sure you close your database stuff properly, then: exit(); }Third, seriously consider talking JSON in your Ajax responses. You'll be a whole lot more compatible with other stuff down the road.
Fourth, while it's very tempting to do everything in one script, you might want to consider creating an api.php that handles all your Ajax-y stuff. If you want some help with how to structure that, let me know. I can give you a template to use that might make your programming life way easier.