ajax to refresh database.

Associate
Joined
11 Oct 2008
Posts
268
At the moment when I click my link

<a href="/?movement=up">Move up</a>

it loads the following code which updates my mysql database.

PHP:
<?php

    if ($movement == "up")
{
    $sql = 'UPDATE movement SET pos_x = pos_x-25 WHERE id=1';   
    $statement = $dbh->prepare($sql); 
    $statement->execute();
    echo $load;
}

?>
it then refreshes the page, thats what the $load does. However that gets in the way of a music file I have playing in the background forcing it to restart everytime the database is updated.

I know you can use ajax/jquery to update the database without having to refresh the whole website but I have no ajax or jquery experience what so ever.

If its not too much work, if someone would be willing to code it for me, I will gladly pay for their time with paypal.
 
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
You need to move that php code to its own file (url) then call that url with ajax.

here's some example jquery code that does an ajax call can refreshes the content of container with id 'Details'
Give your link an id or a class. I have used a class called 'refresh' for this example.
Code:
$(document).ready(function() {
	$('.refresh').click(function(){
		//this function executes when your button is clicked.
		$.ajax({
			url: "path to your php file",
			type: 'GET', 
			complete: function (r) {
				//this function executes when the results come back from the url. variable r is the returned value.
				$('#Details').html(r); //set the content of the container to the results from the url 
			}
		});
		return false; //you need to return false to cancel the default click action of the refresh button
	});
})
 
Last edited:
Soldato
Joined
28 Oct 2006
Posts
12,456
Location
Sufferlandria
had some time to re-read this properly. I think you'll need to edit your php to return just the database results formatted into whatever html tags your using (tables?) Without any other stuff like headers, footers, menus, etc. Assuming that's what the 'echo $load' line is doing.
Once you've done that, you won't need to move that code to i different file like i said before, just use the '/?Movement=up' url.
 
Back
Top Bottom