So I am trying to open af php file that updates something in my database. I want to do this using AJAX since I dont want my entire page to update. After the php file has been ran I want to change an image on the page that triggered the php file.
How exactly is this done?
Hope you are having jquery library
do an ajax call like this:
$('#upvote').click(function(){
$.ajax({
url : 'scripts/upvote.php', // give complete url here
type : 'post',
success : function(data){
alert('success');
}
});
});
Hope this will help you
Using jquery, simply as
$.ajax({url: "yourpage.php"});
Check reference for more options http://api.jquery.com/jQuery.ajax/
As shown in the JQuery docs:
$.ajax({
type: "POST",
url: url,
data: data,
success: function(result) {
// Change image here
},
dataType: dataType
});
This is one way to do an AJAX call. You don't really need to set it to POST:
$.ajax({
url: 'anyphpfile.php',
dataType: 'type', //the type of data you're expecting
success: function(result){
//do what you want to update here
}
});
The structure of when this is done depends on how you make your AJAX call. In the tags you've included jQuery, so I'm going to assume you're using the .ajax()
function. For now I'll assume the use of the .then()
callback (show your code if it's different). In that case you'd "change the image" here:
$.ajax({
url: 'someurl.php'
}).then(function() {
// change the image here
});
According to the documentation, .then()
will always be called after the AJAX call completes. There are other, more specific functions such as .done()
or .fail()
which you can use as well. The comment in the code above indicates where you'd perform your action responding to the AJAX call. What action you perform isn't entirely clear. Are you just changing the src
of an img
? Something like this, then:
$('#theImage').prop('src', someUrlValue);
Where you get someUrlValue
is up to you.