Jquery ajax call to publish - unpublished my article
The php admin area of my website displays a list of all my articles, coming from a mysql table. There's a column in that table where a row can have the value 0 if the article is unpublished or 1 if it's published.
I'd like to be able to click on a image to change the status of any article, from published to unpubli开发者_如何学运维shed and reverse, and having the image changed in the same time so i can see which article is published or not.
Do you have any lead where i can find a code wich could help me do that?
This is basically what you need to do -
1) Bind a function to onclick event of the image and send an ajax request of type post
to a a server side code (php file for eg) using jquery (since you have specified a jquery tag) passing the article id and whether you wish to publish or unpublish the article. You will also need to keep a flag that will store the status of the article in js
2) Write code in the php file that will connect to the database and update the record as per the post variable passed to it and send a response back
3) Depending upon the response, change the flag and also the image in js
Since you asked for a link/code, this explains about sending ajax request using jquery and handling the response from the server - http://www.tutorialized.com/view/tutorial/jQuery-AJAX-tutorial/50833
$('.unPublished').click( function() {
$.ajax({
url: "index.php?publish=" + pkey,
success: function(msg){
$('.unPublished').hide();
$('.Published').show();
}
});
$('.Published').click( function() {
$.ajax({
url: "index.php?unpublish=" + pkey,
success: function(msg){
$('.Published').hide();
$('.unPublished').show();
}
});
Something like that should do. Give the images a class of unPublished and Published. You may need to give them an id as well so you can grab the primary key and send it if necessary
精彩评论