Wednesday, December 22, 2010 10:22 PM
Labels: Ajax , Html email , images , Jquery , PHP
The PHP
if(isset($_POST['image']))
{
$to = 'errors@yourdomain.com';
$from = 'automailer@yourdomain.com';
$subject = 'Broken Image';
$content = "The website is signaling a broken image!\n\nBroken Image Path: ".stripslashes($_POST['image'])."\n\nReferenced on Page: ".stripslashes($_POST['page']);
$result = mail($to,$subject,$content,'From: '.$from."\r\n");
die($result);
}
I keep the email short and to the point; it contains the broken image's src attribute and the page it was requested by.
The jQuery JavaScript
$(document).ready(function() {
$('img').error(function() {
$.post('ajax-image-error-jquery.php', {
image: $(this).attr('src'),
page: window.location.href
}, function() {
//hide the image?
});
});
});
For every image, we listen for the error event. When a broken image is discovered, an AJAX request is sent to the above PHP script.
Of course, if the page experiences high traffic before you can fix the image path, you'll have quite a few emails. You may prefer to store the error in a database table and check that often.



Comments (0)
Post a Comment