2

I have a relational database and a thread/comment system. I'm having trouble thinking of a way of tackling upvoting, as the page would have to be refreshed for the server to notice any changes.

I've created a new table, UPVOTESTHREAD which consists of two foreign keys, user ID and thread ID, and a primary key which is the combination of the two. The only way I could actually insert into this though is if I submit some POST data. I'm afraid that this would feel clunky to the user, and it would also mean that the user would have to scroooollll down back to where they were just looking at. This can't be the correct solution, right? Is there a better way of doing this?

theupandup
  • 335
  • 3
  • 14
  • 5
    Yes there is a better solution. It's called [AJAX](https://stackoverflow.com/questions/6009206/what-is-ajax-and-how-does-it-work). It sends a post or get request to the server and receives the ouput without reloading the page. The script on the server that receives the ajax request can (in your case) increment a vote counter in the db. – kscherrer Nov 24 '17 at 14:18
  • 3
    In addition to what @Cashbee said, it can also return a reponse which you can use to update a DOM element in your view on the fly, for instance with innerHTML, so you can give visual feedback without the refresh. – Robert Wade Nov 24 '17 at 14:23
  • Thanks Cashbee. I'll look into AJAX implementation for this. nearly all of the senior design projects our CS department gives us require webdev or mobile dev, and we have no development courses available to us... so I'm a bit of a noob. – theupandup Nov 24 '17 at 14:24

1 Answers1

2

Yes there is a better solution. It's called AJAX. It sends a post or get request to the server and receives the ouput without reloading the page.

The script on the server that receives the ajax request can (in your case) increment a vote counter in the db.

The (usually PHP-) script can also output something (usually json or a status code) that the success handler of the ajax function will receive. You can then modify your page with javascript accordingly. In your case you could receive the current vote count and update the count next to the upvote button if there is a count.

kscherrer
  • 5,486
  • 2
  • 19
  • 59