Morlas
Morlas

Reputation: 121

How to post to php value of href in a

I've been able to retrieve all <a href=""> with class various.

I need to write some script that grabs href from these a (everyone has different) and post it to popup.php.

Tricky part is that href has gallery_popup.php??id=XX, and the only thing that I need to post is that ID.

I decided to go with variables, so first:

var href = $('.various').attr('href');

I grab the href. Then I woul like to rip all unnecessary things and leave only ID number and save it to Var = IDnumber

And then

$(".various").click(function () {
   $.post('gallery_popup.php', IDNumber;
});

Upvotes: 2

Views: 677

Answers (4)

Shijin TR
Shijin TR

Reputation: 7768

You can save Id number in "rel" like,

  <a href="gallery_popup.php??id=XX" rel="xx" class="various" >gallery</a>

Then you can access id,

    var href = $('.various').attr('rel');

If you want use

    var href = $('.various').attr('href');

You can split your url with 'id='

   eg: var temp = href.split("id=");
        alert(temp[1]);

Upvotes: 0

tymeJV
tymeJV

Reputation: 104775

Just do it in one line:

$.post('gallery_popup.php', $(this).attr("href").split("=")[1]);

Upvotes: 0

Animesh Nandi
Animesh Nandi

Reputation: 458

or you can do

var myString = $(this).attr('href');
var myArray = myString.split('=');

$.post('gallery_popup.php', myArray[myArray .length-1]);

Upvotes: 1

Samuel Caillerie
Samuel Caillerie

Reputation: 8275

You can use something like this :

$(".various").click(function () {
   var url = $(this).attr('href')),
       matches = url.match(/gallery_popup\.php\?\?id=(\d+)/),
       id;

   // If an id has been found, matches will contain 2 elements :
   //   1 - the whole chain matched (here "gallery_popup.php??id=XX")
   //   2 - the group captured (here XX)
   if(matches.length > 1) id = matches[1];

   $.post('gallery_popup.php', id);
});

Upvotes: 0

Related Questions