Execute simple mysqli query on click with AJAX

戏子无情 提交于 2020-12-13 11:56:22

问题


My AJAX/Java knowledge is very limited so this might sound like a stupid and very easy question, but here I go.

I am trying to execute a very simple one line mysqli query after clicking a link on a php page. This query has to be executed in the background, so without reloading the page, so I would need AJAX to do that.

EDIT: This is the code I have now.

The PHP page (notes_checked.php) with my query:

if ($user_ok == true) {
        mysqli_query($db_conx, "UPDATE users SET notescheck=now() WHERE username='$log_username' LIMIT 1");} 
 else {
    exit();
}

The Javascript function I execute when clicking:

<script>

function notesChecked() {

  document.getElementById("notbadge").style.background = "#B0B0B0";
  document.getElementById("notbadge").innerHTML = "0";

  $.ajax({
    type: "POST",
    url: 'php_includes/notes_checked.php',
    data: data,//post data
    success: function()
    {
     alert("You just checked your notes!");
    }
});

}


</script>

The link on my main page:

<a onclick="notesChecked()">Checked</a>

So this script sets the counter back at 0 and changes the background color of the span when the user clicks on it. Now I have to execute the mysqli query so this number stays 0 when the user refreshed the page or comes back later.

Anyone who can tell me if there's an easy way to do this? Thanks!


回答1:


You have to create another php file for the mysql query to execute(Example:- yourquery.php) which will execute in the background.

Then put an AJAX call to the file :

$.ajax({
  type: "POST",
  url: "yourquery.php",
  success: function(response) {
     // show response for success
   }
});



回答2:


Basically, you can write:

HTML

<button class="i-like-trains">DO YOU LIKE TRAINS?</button>

JS

$(document).ready(function(){
  $('.i-like-trains').on('click', function(){
    var name = 'BOB';
    $.ajax({
        type: "POST",
        url: 'myphpdoc.php',
        data: { 'name' : name },
        success: function()
        {
          alert(msg); // alerts the response from myphpdoc.php
        }
    });
  });
});

MYPHPDOC.PHP

<?php
    // create $db_connection //

    $name = $_POST['name']; // post variable has the same name as in the ajax data array

    mysqli_query($db_connection, "UPDATE users SET notescheck = now() WHERE username='$log_username' LIMIT 1");

    echo 'YES, YOU LIKE TRAINS TOO <3'; // example response
?>

hope I helped :-)




回答3:


$(document).ready(function() {
   $('#buttionid').click(function(){
     postajax();
   });
});

function postajax()
{
//When the button is clicked do this
$.ajax({
    type: "POST",
    url: 'yourlinktomysql.php',
    data: data,//post data
    success: function()
    {
     //do something if the ajax-action is a success
    }
});
}


来源:https://stackoverflow.com/questions/34242211/execute-simple-mysqli-query-on-click-with-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!