Home > Software engineering >  How to store the number of likes from a HTML "icon button"
How to store the number of likes from a HTML "icon button"

Time:02-19

I am a beginner in webdevelopment, i am doing for fun an website. Now I achieved everything I want, but one thing is still open. I have searched a lot (read about DB/Ajax etc) but did not found anything which is working out for me.

I have a specific site which are only images. I would like to that a person can Like an image without registration, but I need to store this like in a Database or something like that. What my code is now:


<div >
<i onclick="Toggle(this)" id="btn1" ></i>
</div>

This is the class, when you click on the button, it should change the color to a red. (This is already achieved). But if I refresh the page, the like is not kept. Can someone give me tips with an example?

CodePudding user response:

Generally, an external database should be used to save the data.

In your simple case, you can use local client storage to save the data. This article may be helpful: https://developer.mozilla.org/en-US/docs/Learn/JavaScript/Client-side_web_APIs/Client-side_storage

CodePudding user response:

hi you can use AJAX for request to save in database. first search about AJAX in JS or Jquery. then you need backend language eg : PHP,python,ASP... and with language can save your data in database (database eg : mysql,mariadb,...)

maybe that links help you: Ajax with Jquey: https://learn2torials.com/a/what-is-ajax


Ajax with JS : https://www.w3schools.com/xml/ajax_intro.asp


use database with PHP : https://www.geeksforgeeks.org/how-to-insert-form-data-into-database-using-php/

CodePudding user response:

You'll need some JavaScript to a varying degree of complexity depending on what your end goal is.

1. Serving each user on an individual basis only

i.e. What user A liked doesn't affect what's shown to user B

It's more of Save to favorites type of scenario, in which case you can simply store the like counts in a given user's localStorage. This MDN page would be a good starter for that: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

2. Persisting the accumulative values to the open web

i.e. What user A liked counts to what user B sees

This is closer to a social media scenario, where everyone gives a like which is visible to everyone else. As others mentions, you'd need a certain type of database or storage online. With the JAMstack approach there are simpler solutions than traditional DBs with traditional servers. I'd recommend simple cloud DB solutions like Firebase https://firebase.google.com/

  • Related