1

I have a PHP class in which there is a delete function. When I click on the image I want to call delete, but this line is not working:

echo "<img src='icon/del.gif'  onClick='.delete().'/>"

I have tried using the href tag but it doesn't work.

4
  • 3
    PHP runs on the server... The onclick event runs on the client... Commented Mar 14, 2011 at 2:39
  • THANKS Macmade but if i want to do it for php how can i manage it Commented Mar 14, 2011 at 2:41
  • You have to send a request to the server that is running your PHP. The server can't magically see what events occur on the client unless you send it this information. Commented Mar 14, 2011 at 2:43
  • <a href='delete.php?id=123'><img src='icon/del.gif' /></a> Commented Mar 14, 2011 at 4:05

2 Answers 2

2

Considering you can use jQuery, you need to do the following:

first give your img a class, for example:

<img src="/icon/del.gif" class="delete" />

then bind this using jquery:

$('.delete').click(function(){
   $.ajax({
      type: 'POST',
      url: '/url/to/your_php_script.php',
      data: {'id':'45'}
      }
   });
});

your php file gets executed and $_POST['id'] gets send to it.

does this help? can you do the rest yourself? if not, give us more code :)

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

Comments

0
  • Create cross browser XHR.
  • Request a URL which calls delete().

Repeat with me: JavaScript runs on the client; PHP runs on the server.

PHP can echo JavaScript and JavaScript can make HTTP requests.

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.