Mateus
Mateus

Reputation: 2668

Update values from MYSQL table without reloading the page?

I've build a small login system for a home project,so,when the user do the login,he is redirect to the userspage.php.In the page i get some data from my mysql table,but the problem is,one of the values i fetch is display to the user with a button near it,when the user press those button i need to perform an php action to increase the value which is display with the button(as an example==> myvalue = 10 | When the user clicks on the button: myvalue = 11 ).I've already build this part,it is working perfectly,but i need to increase the value and update the mysql column without refreshing the page?I know its possible but every tutorial from the web i've tried doesn't work.

Thanks is advance!

Upvotes: 1

Views: 7618

Answers (2)

Anil
Anil

Reputation: 588

The following code has a button which onclick calls updatedb.php and shows its output in div element called 'result'

<html>
<head>
<script type="text/javascript">
function UpdateDb()
{
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("result").innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","updatedb.php",true);
xmlhttp.send();
}
</script>


   </head>
    <body>

<button onclick='UpdateDb()'>Update</button>
<p> <span id="result"></span></p>

</body>
</html>

Then write updatedb.php

<?php
do mysql update here.
and echo the result you want to display.
?>

Upvotes: 3

Andreas Linden
Andreas Linden

Reputation: 12721

AJAX is what you're looking for. Best lib for that is jQuery with it's $.ajax() method http://api.jquery.com/jQuery.ajax/

Upvotes: 4

Related Questions