I need help with PHP AJAX

dannywebnoob

New member
Registered
Joined
Aug 25, 2021
Messages
2
Points
1
hey guys
I need help with AJAX. I want to UPDATE my DB without refreshing. I don't really know how to do this. I have found piece of code online but for some reason is not working. Another thing is : how to make run PHP code after 5 minutes. For example: someone push the button and after 5 minutes my DB will be updated. Any Ideas?
 

Dopani

Well-known member
Registered
Joined
Mar 11, 2014
Messages
239
Points
18
It is easy mate!

Check example here https://www.w3schools.com/php/php_ajax_database.asp

Their codes

<html>
<head>
<script>
function showUser(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","getuser.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>

<form>
<select name="users" onchange="showUser(this.value)">
<option value="">Select a person:</option>
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Joseph Swanson</option>
<option value="4">Glenn Quagmire</option>
</select>
</form>
<br>
<div id="txtHint"><b>Person info will be listed here...</b></div>

</body>
</html>
In getuser.php file, you need to make the Mysql query to return the string and display it in txtHint div by ID

Hope that helps!
 

S4 Hosting

Well-known member
Moderator
Hosting Provider
Joined
Mar 5, 2021
Messages
177
Points
16
Another thing is : how to make run PHP code after 5 minutes. For example: someone push the button and after 5 minutes my DB will be updated. Any Ideas?
I'm curious, what is the use case for this, what are you trying to achieve?

I don't see any reason why you would want to update something in a database with that much delay. I can think of possible reasons to not show something being updated to frontend viewers for a while, but not to actually not update it, it introduces all sorts of potential issues with the server going down or other failure during the time period between click and update.
 
Latest Threads
Replies
0
Views
123
Replies
1
Views
79
Similar Threads

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top