0

I have a webpage with some broken images, these images are being show by a database. I am using the following jQuery to hide the images that are broken.

//images are wrapped in an anchor
$("img").error(function() {
    $(this).parent().hide();
});

I would like to utilize the "status" column in the database to set all of the broken images to "hidden". Every anchor that wraps an image on the page has a "id" attribute that matches the primary database key "id".

$("img").error(function() {
    var error = $(this).parent().attr('id');
    $.ajax({
        type: "POST",
        url: "changestatus.php",
        data: "status=hidden&id=".error.""
    });
});

// changestatus.php

<?php 
mysql_connect("localhost", "stackoverflowexampleuser", "stackoverflowexamplepass") or die(mysql_error());
mysql_select_db("stackoverflowexampledatabase") or die(mysql_error());
$id = $_POST['id'];
$status = $_POST['status'];
$query="UPDATE stackoverflowexampletable SET status = '".$status."' WHERE id ='".$id."'";
mysql_query($query) or die ('error');
mysql_close();
header( 'Location: MYSOURCE' ) ;
?>

This is my first stab at ajax, and I know I got some stuff seriously wrong. I saw a couple of examples using KEY VALUE pairs but I don't know what the $_POST['var'] should be.

Can you even request something like this "when the page loads"? I tried wrapping it in an arbitrary button and it didn't work.

Since this just needs to be used once I'm not really focused on using AJAX.

1
  • No, although I'm new to debugging ajax with firebug. Nothing is really happening at all. Commented Feb 22, 2011 at 18:14

1 Answer 1

1

Try this:

$("img").error(function() {
    var error = $(this).parent().attr('id');
    $.ajax({
        type: "POST",
        url: "changestatus.php",
        data: {
             status: "hidden",
             id: error
        }
    });
});

If you're wanting to initiate it on page load, be sure to include it in the $(document).ready() function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.