I want to update a DB field. I can achieve it through the below code yet this requires the page to be refreshed and I am unable to display a message about the action outcome.
I tried using some JQ scripts without success (its so cryptic to me, however, it is on my learning plan just not there yet).
Would you please help me achieve this using ajax / jquery?
index.php
include ("functions.php");
if (isset($_GET['user_id'])) {
$user_id = $_GET['user_id'];
update_time($user_id);
}
?>
<html>
<head>
<title>Update Time</title>
</head>
<body>
<div id="msg"></div>
<a id="update_link" href="index.php?user_id=user_id">Update Time</a>
</body>
</html>
functions.php
<?php
function user_exist(){
//run an SQL query to check if the user exist
if (exist)
return true;
else
return false;
}
function update_time($user_id){
if(user_exist())
//display a message in #msg that the time was updated
//update the database
else
//display a message in #msg that the user does not exist
}
?>
Script
$("a#update_link").click( function() {
$.post( $(this).attr("href"),
function(data) {
if (data == "Time Updated") {
window.location = "index.php";
}
$("div#msg").html(data);
});
$(this).click( function() {
return false;
});
});