0

The code below is supposed to grab the ?cs=bla&cat=bla values from the link attribute and post it to my .load query...does anyone know where i am going wrong?

//Remove tab info and add gallery
    $(".more").click(function () {
        var $gallery = $(this).closest('.tab').find('.gallery-holder'),
        cat = $(this).attr('href').split('cat=')[1];
        if ($gallery.is(':empty')) {
        $gallery.load('/public/themes/lbd/js/imagegallery.php', {'cat': cat}, function(){
            $(this).fadeIn(function(){
                $('a.customGal').zoomimage(); 
            });
        });
        }

        $gallery.siblings().fadeOut(function(){
            $gallery.fadeIn();
        });     
        return false;
    });
Andy
  • 2,991
  • 9
  • 43
  • 62

1 Answers1

0

Where your approach goes wrong is the split, when you do this: .split('cs='), you're not getting "bla" as the result, you're getting "bla&cat=bla", the rest of the string.


You could just pass the data as the same string if it's in a known format (the original querystring pairs), like this:

var data = this.href.split('?').pop();
if ($gallery.is(':empty')) {
  $gallery.load('/public/themes/lbd/js/imagegallery.php', data, function() {
    $(this).fadeIn(function() {
        $('a.customGal').zoomimage(); 
    });
  });
}

Note this would produce a GET request rather than a POST (which happens then .load() gets an object as the data argument).

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • does that send each one as $_POST['cs'] e.t.c? – Andy Dec 02 '10 at 15:16
  • There is an error in this line? var data = this.href.split('?').pop(); – Andy Dec 02 '10 at 16:25
  • @Andy - What error are you getting? If this isn't an anchor you'll need `$(this).attr("href").split('?').pop();` for the invalid attribute :) – Nick Craver Dec 02 '10 at 16:43
  • $(".more").click(function () { var $gallery = $(this).closest('.tab').find('.gallery-holder'), cat = $(this).attr('href').split('cat=')[1]; cs = $(this).attr('href').split('cs=')[1]; if ($gallery.is(':empty')) { $gallery.load('/public/themes/lbd/js/imagegallery.php', {'cat': cat, 'cs': cs}, function(){ $(this).fadeIn(function(){ $('a.customGal').zoomimage(); }); }); } – Andy Dec 02 '10 at 18:29
  • @Andy -.... *what* error are you getting? you're not using my answer at all, you're still splitting invalidly and you'll have the same error you did with the code in the question... – Nick Craver Dec 02 '10 at 18:35
  • Sorry Nick, check out my edit in the main question where i include the whole function, now you'll notice why i couldn't use the original answer without causing an error. I should have included this at the start. Apologies. – Andy Dec 02 '10 at 18:40
  • i just removed the comma and added a semi colon where it says find('.gallery-holder'), its working now :) Thanks for all your help! – Andy Dec 03 '10 at 10:03
  • @Andy - Ah, I see what you're after, look here for a function to get a querystring out: http://stackoverflow.com/questions/901115/get-querystring-values-with-jquery/901144#901144 just replace `window.location.href` with your own `href` attribute. – Nick Craver Dec 03 '10 at 10:03