Nalaka526
Nalaka526

Reputation: 11464

How to call PHP page with Javascript & jQuery when user clicks on a href link?

In a PHP Project I have hyperlink :

<a href="addid.php?id='. $Id . '">| Name |</a>';

When a user click on the link I need to add the selected "id" to session

addid.php Code :

session_start();
$_SESSION['id'] = $_GET['id'];

I need to accomplish this without reloading the page (need to add the "id" to session in background).

How to call addid.php with Javascript & jQuery?

NOTE: I tried this code, but it does load the addid.php in browser

$('a').click(function(){
      $.ajax(); 
        return false; 
});

Upvotes: 0

Views: 1089

Answers (3)

jONGsKiE777
jONGsKiE777

Reputation: 156

$('a').click(function(){
   $.get($(this).attr('href'));
   return false;
});

Upvotes: 2

AD7six
AD7six

Reputation: 66169

Give your link an id:

<a id="something" href="addid.php?id='. $Id . '">| Name |</a>';

and then you can easily refer to it:

$('a#something').click(function(e) {
    e.preventDefault();
    $.get($(this).attr('href')); // no handler fire and forget
});

The code you tried is invalid - have a look at the api for the jquery function you're using. url is a mandatory argument.

Check using developer tools/firebug/whatever you have installed that the request is sent to the right place, and the response if what you expect.

Upvotes: 1

MarkR
MarkR

Reputation: 187

Look at jquery:

http://api.jquery.com/jQuery.get/

Use ajax and set the id as a parameter in the data string.

Upvotes: 0

Related Questions