How to submit value to database with jquery?

95
August 14, 2018, at 8:10 PM

so basically i want to make a webpage to detect how many times user clicked on it and send that value to database. I know i should use ajax but just can't figure out how to use it.

var count = 0;
$("body").click(function() {
$("#track").text("you clicked " + count + " times");
count++;
});
$.ajax({
url: "index.html",
cache: false,
success: function(html){
$("#results").append(count);
}
});
Answer 1

Assuming the following things. You:

  • have AMP Stack installed with PHP & MySQL.
  • have a working DataBase in MySQL (most commonly used with PHP).
  • are running the web server and looking at HTTP version and not file version in Chrome.

I would go by this method:

  1. On load, I'll get the current count from DataBase using a PHP backend.
    1. In the PHP backend, I'll write a code to query MySQL and get the current value.
    2. Use a single file, say count.php as a file to get and set the counts.
    3. Using GET method, the file responds with the count.
    4. Use an AJAX code and get the count data.
    5. Using jQuery with the AJAX's response, update the DOM with the current count.
  2. Once you load the page and update the value, set the event listener on click.
    1. Update the current count in the UI by adding one more.
    2. Use the same code as you have to update the UI to increment the count.
    3. Fire a POST request using AJAX to the count.php and send the new value.
    4. In the count.php, write an UPDATE query to update the count.
    5. Send a success message.
  3. When you reload the page or look at the database, the count will be preserved.
READ ALSO
How to do give expire time to specific session in laravel 5.6 ?

How to do give expire time to specific session in laravel 5.6 ?

I want to create session and it should be remove at 3 days later

102
WooCommerce AJAX Pagination no plugin

WooCommerce AJAX Pagination no plugin

I am trying to look into how I could create my own WooCommerce AJAX Pagination functionAll the tutorials I've found use a plugin and I would prefer not to

169