0

I am trying to get a PHP variable to send through javascript and I am having trouble. I am using a jQuery popup I found online and when a user clicks an item, I need that item_id variable to be fetchable in the popup where I want to add a form.

My PHP section.

<a href="#?w=500&item_id='.$stock['id'].'" rel="popup1" class="poplight">

and the Javascript.

$(document).ready(function(){

    //When you click on a link with class of poplight and the href starts with a # 
    $('a.poplight[href^=#]').click(function() {
        var popID = $(this).attr('rel'); //Get Popup Name
        var popURL = $(this).attr('href'); //Get Popup href to define size

        //Pull Query & Variables from href URL
        var query= popURL.split('?');
        var dim= query[1].split('&');
        var popWidth = dim[0].split('=')[1]; //Gets the first query string value
        var itemID = dim[0].split('=')[2]; //Gets the second query string value

        //Fade in the Popup and add close button
        $('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="/layout/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');

        //Define margin for center alignment (vertical + horizontal) - we add 80 to the height/width to accomodate for the padding + border width defined in the css
        var popMargTop = ($('#' + popID).height() + 80) / 2;
        var popMargLeft = ($('#' + popID).width() + 80) / 2;

        document.getElementById('popup1').innerHtml = itemID;

        //Apply Margin to Popup
        $('#' + popID).css({ 
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
        });

        //Fade in Background
        $('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
        $('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer 

        return false;
    });


    //Close Popups and Fade Layer
    $('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
        $('#fade , .popup_block').fadeOut(function() {
            $('#fade, a.close').remove();  
    }); //fade them both out

        return false;
    });


});

I added what I thought you work, but it does not var itemID = dim[0].split('=')[2]; //Gets the second query string value and document.getElementById('popup1').innerHtml = itemID;

Chuck
  • 998
  • 8
  • 17
  • 30
user1888260
  • 47
  • 2
  • 11
  • What exactly doesn't work? Did you take a look at your debugger? Could it be, that `#popup1` doesn't exist? – MCL Feb 17 '13 at 18:58

2 Answers2

0

Use a sophisticated function to retrieve URL query parameters:

function getParameterByName(name, locationObject)
{
  name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
  var regexS = "[\\?&]" + name + "=([^&#]*)";
  var regex = new RegExp(regexS);
  var results = regex.exec(locationObject.search);
  if(results == null)
    return "";
  else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

[Adopted from here]

Simply call it like so:

getParameterByName('item_id', someLink);  

Working Fiddle: http://jsfiddle.net/ADpYT/

Community
  • 1
  • 1
MCL
  • 3,985
  • 3
  • 27
  • 39
  • Okay, I threw that in my functions file and tried to call it here. echo ''; But it didn't do anything. – user1888260 Feb 17 '13 at 18:32
  • @user1888260 Look at the function, I edited it. Example call is also edited. EDIT: Sorry, you just need to pass the link itself. Edited again. – MCL Feb 17 '13 at 18:35
  • Okay, I guess what I am trying to ask, is what can I do to return that value in text format inside my popup HTML? – user1888260 Feb 17 '13 at 18:46
  • @user1888260 I don't quite get what you want. Do you want to display the query parameter inside your popup? – MCL Feb 17 '13 at 18:50
  • Yeah, say the item_id=2, I want it to display HTML in the popup as But as of now, I cannot figure out how to extract it from the URL and get that, because you click the link and a popup comes up, no change of URL. – user1888260 Feb 17 '13 at 18:54
  • @user1888260 In this case, it would certainly be helpful, if you provided your HTML, too. – MCL Feb 17 '13 at 18:57
  • @jagzviruz Correct me if I'm wrong, but you just showed how the query parameter can be retrieved. How does this help regarding OP's new problem? – MCL Feb 17 '13 at 19:03
  • Okay, my HTML is as follows: Click this link: `Item` This is the pop HTML where I want the form: `` – user1888260 Feb 17 '13 at 19:11
  • @MCL OP was getting null in itemID which I helped him fix. Now if he wants to set the value of a hidden input element to this value, he can easily do this by using .val(). – jagzviruz Feb 17 '13 at 19:13
0

You have a simple oversight in your original code .. When you obtain your dim variable, you get an array with 2 items split at & . In the above code you get ["w=500", "item_id=some_value"] .So in order to get the itemID you have to use dim[1]. var itemID=dim[1].split('=')[1];

jagzviruz
  • 1,453
  • 12
  • 27
  • Ah thanks, I got it to append to the popup. like so $('#popup1').append(itemID); But i really need it to be able to be fetched in a form. Maybe I can append the form to the end? – user1888260 Feb 17 '13 at 19:06