I have an issue at the moment regarding PHP, Jquery and Ajax.
I have a a div at the bottom of my page that has data in it from a database, now for every iteration of the data a new div is formed, the div has an id of 'pagestatus' and has an auto increment next to it so the id changes for each div like so: 'pagestatus0', 'pagestatus1' etc.
Now I'm not completely new to Jquery as I have used it to make pages more interactive and I've used Ajax to update a MySQL database.
I'm having trouble with the code though, I would like it go something like this:
- Button is clicked in div
- Button fades in (or div, which ever is easier)
- A hidden div with a loading gif appears underneath
- Ajax calls to the php file to make changes to the database
- jquery then fades the loading gif back out and fades the button back in
I have wrote some code for it but I think I am going wrong somewhere, could someone see what I am doing wrong and let me know how to fix it.
Here is my Jquery:
$(document).ready(function(){
for(i=0;i<$('#changestatusoff').val();i++){
(function(x){
$('#changestatusoff'+x).click(function(){
$('#changestatusoff'+x).fadeOut('slow',function(){
$('#loadingstatus').fadeIn('slow',function(){
$.ajax
({
type: "POST",
url: '.php',
data: {'changestatusoff': changestatusoff},
success: function(data) {
return data;
},
error: function() {
alert('Error occured');
}
$('#loadingstatus').fadeOut('slow',function(){
$('#changestatusoff'+x).fadeIn('slow',function();
});
});
});
});
});
});
}
})(i);
});
And here is the button in the div:
<input type='button' value='Offline' id='changestatusoff".$count."' style='background: none repeat scroll 0% 0% rgb(0, 118, 188); color: rgb(255, 255, 255); border: 1px solid rgb(0, 0, 0); font-weight: bold; cursor: pointer; margin:5px;'/>
Any help is appreciated
changestatusoffvariable? And instead of looping over all the IDs, why don't you use a class?