I have some problems with Jquery and PHP. I want to delete an image from the server. I think I did a little mess with the code... Can someone give me some tips on how to pass variables from PHP Jquery and vice versa?
JQuery
$(document).ready(function() {
$('#load').hide();
});
$(function() {
$(".delete").click(function() {
$('#load').fadeIn();
var container = $(this).parent();
var id = $(this).attr("id");
var string = 'id='+ id ;
var name = $(this).parent().attr("title");
$.ajax({
type: "GET",
url: "delete.php",
data: 'string',
cache: false,
success: function(){
container.slideUp('slow', function() {$(this).remove();});
$('#load').fadeOut();
}
});
$.ajax({
method:'GET',
url: "delete.php",
data:'name',
success:function(response){
if (response === 'deleted') {
alert('Deleted !!');
}else{
document.write(name);
}
}
});
return false;
});
});
PHP (delete.php)
if (isset($_GET['name']))
{
if (unlink($_GET['name']))
{
echo 'deleted';
}else{
echo "The file ".$_GET['name']." was not deleted";
}
}else {
echo "The name of the file was not set!";
}
echo $_GET['id'];
HTML
$dirname = "../images/portraits";
$images = scandir($dirname);
$ignore = Array(".", "..", ".DS_Store");
$count=1;
foreach($images as $img){
if (!in_array($img, $ignore)) {
if(!strripos($img, "_t")){
echo "<div title='$dirname/$img' class='thumb'>$img<a href='#' id='".$count."' class='delete'>x</a>
<div class='clear'></div><img width='100px' src='$dirname/$img' /></div>";
}
}
}