-
Notifications
You must be signed in to change notification settings - Fork 0
AJAX Experiences
1/27/2020
Current in version 1.02, comment sections are finished. Yesterday, I was fiddling with using fetch()
instead of the usual long and tedious XHR
. I finally found that I had to set the Content-Type to www-application...
in order for the POST data to be sent successfully. I also do not want to use JSON.stringify()
and instead just send it as a query parameter string.
7/9/2019
Borum has come a very long way and I am very happy. The release date will be the first anniversary of Borum, November 13, 2019. We are currently in version 0.9.0, pre-release beta
5/4/2019
Today marks the day that I got to the adequate features for voting on questions and answers. How did I do this? Well, it took lots of research and time. I had to create an additional table that logged everyone's votes, who voted, what they voted on, and what their vote was. Before, people could vote as many times and as often as they want. This made no sense. Once this table was created and integrated, I added a few more arguments to loadXMLDoc()
, and the code for voteup changed to this:
<?php
// Connect to the db
file_exists('../../mysqli_connect.inc.php') ? require_once('../../mysqli_connect.inc.php') : require_once('../../Users/VSpoe/mysqli_connect.inc.php');
// Find the current number of votes before any changes occur
function getVotes() {
global $dbc;
$result = mysqli_query($dbc, "SELECT votes FROM messages WHERE id = {$_REQUEST['message_id']}");
$rows = mysqli_fetch_array($result, MYSQLI_NUM);
return $rows[0];
}
// Check whether the user already voted on this message
$firstq = "SELECT id FROM `user-message-votes` WHERE user_id = {$_GET['user_id']} AND message_id = {$_GET['message_id']} AND vote = 1";
$firstr = mysqli_query($dbc, $firstq);
if (mysqli_num_rows($firstr) === 1) {
// If they already voted this question up
echo getVotes();
// TODO: Implement undoing the vote up
} else {
$q2 = "INSERT INTO `user-message-votes` (user_id, message_id, vote) VALUES ({$_GET['user_id']}, {$_GET['message_id']}, 1)";
$r2 = mysqli_query($dbc, $q2);
$q3 = "UPDATE messages SET votes = votes + 1 WHERE id = {$_REQUEST['message_id']}";
$r3 = mysqli_query($dbc, $q3);
$result = getVotes();
echo getVotes();
}
?>
Similarly, votedown.php just changed all the positives to negatives.
4/26/2019
Recently I took on the task of enabling voting functionality. Little did I know that I would be taking on using code for AJAX, a very powerful form of Asynchronous JavaScript. Using mysqli_query
by itself was not enough because it would be run automatically. So, I almost had to use AJAX. This was a good thing because it also enabled me to let the votes appear without refreshing the page. The AJAX function is called as an event handler in the onclick event handler for the vote up and vote down button. The function can be found in footer.html. This performs an XHR using the GET method to another php file that strictly performs the vote up or vote down queries and returns the new output. It worked perfectly! I shall now proceed to doing the same thing with answering questions.