pat27 pat27 - 1 year ago 72
PHP Question

How to execute Ajax code when I click on the link?

I can't figure out where the problem is :

here is the script :


var obj = $(this);

type: "GET",
url: "supprimer.php",
data: 'id=' + obj.attr('rel')

success: function(html){



and here is the html code associated :

<a href="#" class="icross" title="Supprimer" rel="80"><i class="fa fa-times"></i></a>

I want ajax to execute supprimer.php?id=80 when I click on the link but it doesn't work.

Answer Source

You're missing a comma after this line:

data: 'id=' + obj.attr('rel'),

Also, if you don't have your script tag appearing after the element then you need a DOM ready wrapper:

    // code here

Side note: unless you have good reason to, I recommend using a data object instead of concatenating like that, because by passing an object, jQuery will handle the URL encoding for you.

data: { id: obj.attr('rel') },
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download