Graver
Graver

Reputation: 127

How do I pass a PHP array to a jQuery function?

I have a jQuery function that opens a modal popup and I have in the same file a variable in PHP like $url="iteminfo.php?ID=".$i['itmid'];($i['itmid'] is the id of some products from MySQL). The jQuery function looks like this:

<script type="text/javascript">

  $(document).ready(function(Surl) {

    var source="iteminfo.php?ID=<?echo $i['itmid']?>";
    var width = 920;
    var align = "center";
    var top = 100;
    var padding = 10;
    var backgroundColor = "#FFFFFF";
    var borderColor = "#000000";
    var borderWeight = 4;
    var borderRadius = 5;
    var fadeOutTime = 300;
    var disableColor = "#666666";
    var disableOpacity = 40;
    var loadingImage = "js/popup/loading.gif";

    $(".modal").click(function() {

        modalPopup( align,
            top,
            width,
            padding,
            disableColor,
            disableOpacity,
            backgroundColor,
            borderColor,
            borderWeight,
            borderRadius,
            fadeOutTime,
            source,
            loadingImage );

    }); 

    $(document).keyup(function(e) {
        if (e.keyCode == 27) {
            closePopup(fadeOutTime);
        }
    });

  });

</script>

It opens the respective links but the function open all of them like in a loop. How can I pass the $url into the jQuery function to open the respective link for the respective product?

Upvotes: 2

Views: 264

Answers (2)

fliim
fliim

Reputation: 2189

Did you said that when you click in the element .modal, the right modal appear with the good content ?

And what do you mean by 'them' in 'the function open all of them' ?

Maybe you should take a look in your 'modalPopup' function.

You can store the corresponding url for every .modal element as an attribute with a php loop which browse your array.

Upvotes: 0

Control Freak
Control Freak

Reputation: 13233

For starters it would look more like this, but without seeing your HTML code its hard to say how you would be pulling out the ID of each different object with an ID.

  <script type="text/javascript">

    $(document).ready(function(Surl) {


      var source="iteminfo.php?ID=";
      var width = 920;
      var align = "center";
      var top = 100;
      var padding = 10;
      var backgroundColor = "#FFFFFF";
      var borderColor = "#000000";
      var borderWeight = 4;
      var borderRadius = 5;
      var fadeOutTime = 300;
      var disableColor = "#666666";
      var disableOpacity = 40;
      var loadingImage = "js/popup/loading.gif";


      $(".modal").click(function() {

        //get the id of what you're opening on each click event.
        var myid = ...

          modalPopup( align,
              top,
              width,
              padding,
              disableColor,
              disableOpacity,
              backgroundColor,
              borderColor,
              borderWeight,
              borderRadius,
              fadeOutTime,
              source + myid,
              loadingImage );

      }); 


      $(document).keyup(function(e) {
          if (e.keyCode == 27) {
              closePopup(fadeOutTime);
          }
      });

    });

  </script>

Upvotes: 2

Related Questions