I am trying to delete a file using an AJAX call. It should be simple enough, but for some reason I'm getting an error when trying to access $_SERVER['DOCUMENT_ROOT']
when using the AJAX
I set up a HTML form and there is no problem. Let me show the code
This is the PHP which should delete the image.
$photo_name = $_POST['photo'];
$cat_id = $_POST['cat_id'];
$original_path = $_SERVER['DOCUMENT_ROOT'].$photo_name;
if(file_exists($original_path))unlink($original_path);
$data['success']=true;
Running the jquery below returns syntax error: Unexpected Token
$.ajax({
type : 'POST',
url : "includes/json_delete_photo.php",
dataType : 'json',
data: {
'photo': $('#image_name').val(),
'cat_id': $("#category_id").val(),
},
success : function(data){
alert("done");
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
alert("problem: " + errorThrown);
}
});
However, just using a HTML form to submit to json_delete_photo.php without AJAX works, and if I remove the use of $_SERVER['DOCUMENT_ROOT']
from my PHP, the AJAX doesn't return an error (but my image doesn't delete!!)
I've been scratching my head on this one, and I'm not sure how to send the root to the page.
Thanks for any help.
Should you have the trailing comma at the end of this line?
'cat_id': $("#category_id").val(),
As a workaround , try outputting the $_SERVER
variable directly in your ajax call
data: {
'photo': $('#image_name').val(),
'cat_id': $("#category_id").val(),
'document_root': <?php echo $_SERVER['DOCUMENT_ROOT']; ?>
},
and referring to the passed along $_POST['document_root']
variable in your php script.
To find out the exact cause of your syntax error, use a debugging tool such as "Chrome >> Tools >> Developer Tools >> Network Tab"
, run your script and view your ajax invoked file. You can see the exact headers sent and the response.