Reputation: 501
I am not a big Jquery guy but am using a plugin which is an image uploader. It has a button to remove images with correlates with the following code.
function remove_unwanted_file(id,file)
{
if(confirm("If you are sure that you really want to remove the file "+file+" then click on OK otherwise, Cancel it."))
{
var dataString = "file_to_remove=" +file;
$.ajax({
type: "POST",
url: "remove_unwanted_files.php",
data: dataString,
cache: false,
beforeSend: function()
{
$("#vpb_remove_button"+id).html('<img src="images/loadings.gif" align="absmiddle" />');
},
success: function(response)
{
$('div#fileID'+id).fadeOut('slow');
}
});
}
return false;
}
However, none of the code within the PHP file is ever executed. The alert does pop up and asks you if you want to delete the file with the correct file name. The PHP code is what actually deletes the file however the file does not delete from the file system. I have tried putting other code in the PHP file like sending me an email and none of it executes. Can anyone see anything wrong with this JQuery code?
THIS IS THE PHP CODE
<?php
$to = '[email protected]';
$subject = 'the subject';
$message = 'file reached';
$headers = 'From: [email protected]' . "\r\n" .
'Reply-To: [email protected]' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
mail($to, $subject, $message, $headers);
include('ASApiClientServer.php');
include('ASApiClient.php');
$uniquekey=$_SESSION['uniquekey'];
$postFields = array(
'referralCode'=>$uniquekey,
'image'=>strip_tags($_POST["file_to_remove"]),
),
);
$ApiClient = new ASApiClient();
try{
$ApiClient->requestResponse('removePicture', $postFields);
//handle the call
}
catch(Exception $e){
print_r($ApiClient->getRawResponse());
}
if(isset($_POST["file_to_remove"]) && !empty($_POST["file_to_remove"]))
{
$uploaded_files_location = 'uploaded_files/'.strip_tags($_POST["file_to_remove"]);
@chmod($uploaded_files_location,0777);
@unlink($uploaded_files_location);
//Here you can also delete the file from your database if you wish assuming you also saved the file to your database during upload
}
?>
The email stuff at the beginning is in there just trying to get it to do something. THe path to the file is correct.
Upvotes: 0
Views: 110
Reputation: 40076
First I would make sure the AJAX--to--PHP
system is working. You can do that test with two small changes:
1) At the very top of your PHP file, just make it echo out a simple string and die. For example:
<?php
die("I got to here");
2) In your AJAX success function, put an alert() to capture/display the output from the PHP file. For example:
success: function(response)
{
alert("AJAX Recd from PHP: " + response);
//$('div#fileID'+id).fadeOut('slow');
}
Doing these two simple changes will immediately show you where the error is. If the error is in your PHP file, then post another question and ask about that (posting the PHP code, of course).
The next test, I would suggest, is (a very slight modification to the first test) to make the PHP file echo back out the data received via AJAX:
<?php
$f = isset($_POST['file_to_remove']) ? $_POST['file_to_remove'] : '';
die('PHP recd from AJAX: ' . $f);
Upvotes: 1
Reputation: 11749
First make sure your url
is correct...
And that remove_unwanted_files.php
is the correct path, and not something like example/directory/remove_unwanted_files.php
Then...if it is infact a post
request....try this..
$.ajax({
type: "POST",
url: "remove_unwanted_files.php",
data: {file_to_remove:file},
cache: false,
beforeSend: function()
{
$("#vpb_remove_button"+id).html('<img src="images/loadings.gif" align="absmiddle" />');
},
success: function(response)
{
$('div#fileID'+id).fadeOut('slow');
}
});
If its not a Post
...
Try this...
var dataString = "file_to_remove=" +file;
$.ajax({
type: "GET",
url: "remove_unwanted_files.php",
data: dataString,
cache: false,
beforeSend: function()
{
$("#vpb_remove_button"+id).html('<img src="images/loadings.gif" align="absmiddle" />');
},
success: function(response)
{
$('div#fileID'+id).fadeOut('slow');
}
});
Upvotes: 0