views:

195

answers:

1

I am trying to pass some JavaScript variables that I have assigned to form variables into a query string used by a jQuery plug in called fancybox. If I hard code in the string that I want lightbox works great, however I need it to pass some form values in there to make it work as desire.

<script type="text/javascript">
    $(document).ready(function(){

            var item = document.itemFinder.item;
            var zip = document.itemFinder.zip;
            var radius = document.itemFinder.radius;
            var dataString = "item=" + item + "&zip=" + zip + "&radius=" + radius;
            $("#various3").fancybox({
                    ajax : { type   : "POST", data  : dataString},
                    'scrolling'         : 'auto',
                    'overlayOpacity'    : '0'           
                    }               
                );
    });
    </script>

What am I doing wrong?

+1  A: 

The data parameter expect an object. You're passing a string. Change your call like so:

ajax : { type: "POST", data  : {'item': item, 'zip': zip, 'radius': radius}} 

That should get it to properly serialize your values for you.

-- Updated --

<script type="text/javascript">
  $(document).ready(function(){
     $('#someElement').change(function () {
        var item = document.itemFinder.item;
        var zip = document.itemFinder.zip;
        var radius = document.itemFinder.radius;
        var dataString = "item=" + item + "&zip=" + zip + "&radius=" + radius;
        $("#various3").fancybox({
                ajax : { type   : "POST", data  : dataString},
                'scrolling'         : 'auto',
                'overlayOpacity'    : '0'           
                }               
            );
  });
});
</script>

In my example I'm not sure which event you actually want to bind to (your call), and I'm not sure what other elements are available, but hopefully that makes sense anyway. Thanks,

g.d.d.c
Actually data is calling for a string in the way this function is built. Here's an example from the fancybox site.$("#various3").fancybox({ ajax : { type : "POST", data : 'mydata=test' } });
codeisforeva
Interesting. I haven't used the fancybox plugin myself, but if it builds in the existing AJAX implementations in jQuery then an object should get serialized automatically. If they've reimplemented it in some other way then my answer may be incorrect.
g.d.d.c
Theoretically, you could also do something like this: var data = {'item': item, 'zip': zip, 'radius': radius}; var dataStr = $.serialize(data);Then you could pass dataStr to the method.
g.d.d.c
Yea that should work but for some reason it doesn't want to.
codeisforeva
Odd. Do you have Firebug or some other JS Debugging utility available? If so, do you get anything meaningful in your console there?
g.d.d.c
Ok so I figured it out sort of. What's going on is the values aren't being passed in to the functions after I select them. So it's grabbing the initial blank values from the page instead of the form values. If I select values, refresh the page with the values still selected then click my ajax function link. Bam it works.
codeisforeva
Ahh, ok, that makes sense. What you probably want to do then is wrap your entire function in some event that triggers when the lightbox happens. See my updated answer.
g.d.d.c