1 2 Previous Next 17 Replies Latest reply on Mar 21, 2016 6:49 PM by duncanbaker

    FM PHP API functionality being interfered with by jQuery

    duncanbaker

      Hey folks

       

      I have a small interaction on a webpage with a FM database. The page loads and it calls back to the database to get some data to display, and it also triggers a script.

       

      I'm testing on a new webpage template and while the data is being retrieved fine, the script is not triggering. The piece of code in blue, sitting at the bottom of some Google Maps javascript code, is resulting in an error in Chrome's developer console:

      Uncaught TypeError: $ is not a function

       

      I think this is because of jQuery being loaded by other functionality of the new webpage and there's a conflict. Now this might be more javascript related than API, but it's preventing the functionality from working. Anyone come across this or know how to fix?

       

      Thanks for any tips.

       

      function initialize() {

                    

          var myLatlng = new google.maps.LatLng(lat, long);

          var mapCanvas = document.getElementById('map-canvas');

          var mapOptions = {

           center: new google.maps.LatLng(lat, long),

             zoom: 6,

             mapTypeId: google.maps.MapTypeId.ROADMAP

          }

          var map = new google.maps.Map(mapCanvas, mapOptions);

         

          var marker = new google.maps.Marker({

           position: myLatlng,

           map: map,

           title: 'Test',

           icon: 'http://google.com/mapfiles/ms/micons/fishing.png',

          animation: google.maps.Animation.DROP

            });

          

          var params = lat + "|" + long + "|" + labelid + "|" + "<?php $ip=$_SERVER['REMOTE_ADDR']; echo "$ip"; ?>" + "|" + "<?php $ip2=$_SERVER['HTTP_X_FORWARDED_FOR']; echo "$ip2"; ?>";

          //alert(params);

          $( "#fm-map" ).load( "fmperformscript.php?params=" + params + "&-findall" );

           }

        • 1. Re: FM PHP API functionality being interfered with by jQuery
          beverly

          since the FM API for PHP is PHP classes, I doubt there is a JS conflict. however, using any 'canned' JS can produce errors if you also have any other JS that you (or someone else) created. variables love to step on themselves (in JavaScript and elsewhere!) JS can (and should) also be picky if every nuance is not correct. miss a comma? sorry! so debug we must!

           

          Something that helps me is the browsers that have developer tools and can analyze a page and tell you things you didn't know where in it! See if this can help:

          https://developers.google.com/web/tools/chrome-devtools/?hl=en

           

          beverly

          • 2. Re: FM PHP API functionality being interfered with by jQuery
            duncanbaker

            Thanks Beverly. Yeah as I said it's more of a javascript/jQuery issue I think. jQuery hooks into the $ symbol and I've played around with NoConflict but not been successful. I used the Chrome dev tools to figure out what line was throwing the error

            Uncaught TypeError: $ is not a function

            and it's the one highlighted in blue...

            • 3. Re: FM PHP API functionality being interfered with by jQuery
              user19752

              How did you "loaded by other functionality of the new webpage"?

              $ should be global, so usually loaded in header of html.

              • 4. Re: FM PHP API functionality being interfered with by jQuery
                electon

                duncanbaker wrote:

                 

                    var params = lat + "|" + long + "|" + labelid + "|" + "<?php $ip=$_SERVER['REMOTE_ADDR']; echo "$ip"; ?>" + "|" + "<?php $ip2=$_SERVER['HTTP_X_FORWARDED_FOR']; echo "$ip2"; ?>";

                    //alert(params);

                    $( "#fm-map" ).load( "fmperformscript.php?params=" + params + "&-findall" );

                    }

                 

                My first hunch is it's the quoted php echo variable.

                 

                var params = lat + "|" + long + "|" + labelid + "|" + "<?php $ip=$_SERVER['REMOTE_ADDR']; echo "$ip"; ?>" + "|" + "<?php $ip2=$_SERVER['HTTP_X_FORWARDED_FOR']; echo "$ip2"; ?>";


                I've highlighted them in bold.


                You're setting a parameter with a php code that returns a value.

                It looks like it will return "$ip" as text, not the IP.

                That probably fails on first read and it's the $ character what it's complaining about.


                Maybe try removing the quotes around the ip vars.

                Or maybe that's the way to write PHP... no expert in PHP I'm afraid.


                HTH,

                Thomas.


                A little EDIT:


                Still thinking about the quotes. Maybe you need to escape the before passing on:

                echo \"$ip\"


                But basically you'd not need the quotes anyway. If you're not using any other characters you can just echo the variable.

                echo $ip

                • 5. Re: FM PHP API functionality being interfered with by jQuery
                  user19752

                  Hmm, that is PHP source, so the quote at

                  "<?php

                  (and pair of that at ?>") should be removed?

                   

                  and if there are specical characters in params, they should be URLEncoded.

                  • 6. Re: FM PHP API functionality being interfered with by jQuery
                    electon

                    Not sure about removing the quotes around the <?php?> declaration.

                    It's not that easy to run PHP inside Javascript. PHP runs server-side.

                    Some say it can't be done :-)

                     

                    I'd rather use an Ajax request to run php code.

                     

                    I'm thinking OP passes this to another php code for some reason and it gets evaluated there.

                    Or at least that's what it seems it would do.

                     

                    So is it the text code you send or do you try to evaluate it right there so you'll get the client's IP?

                    • 7. Re: FM PHP API functionality being interfered with by jQuery
                      duncanbaker

                      Thanks all. Just to be clear, the code works fine on a straight html/php page. It's working without issue on the current live site. But on a new test website template that contains additional javascript/jQuery elements, it's throwing an error at the code highlighted in blue starting with $. The parameters are not an issue. If I enable the 'alert' line this shows the correct values.

                      • 8. Re: FM PHP API functionality being interfered with by jQuery
                        electon

                        It could well be that the parser wasn't too picky up to this point and evaluated it just as well.

                        Here's some examples from FileMaker:

                         

                        Screen Shot 2016-03-21 at 03.17.29.png

                         

                        Complaining about the quotes.

                         

                        Screen Shot 2016-03-21 at 03.18.01.png

                        Not complaining after quote escaping

                         

                        Screen Shot 2016-03-21 at 03.18.19.png

                        Not complaining after not using the quotes.

                         

                         

                        We don't manage the site and your code snippet is all we can have a go at.

                         

                        I'm not even sure why you use this piece of code as parameter since it can be done on the server php side.

                        It's the server that generates the $_SERVER array and it seems you pass this code to fmperformscript.php as parameter.

                        Why not do it there in the first place?

                        • 9. Re: FM PHP API functionality being interfered with by jQuery
                          user19752

                          Ah, my misread, as the code is php page, <?php ?> is already evaluated on server, surrounded and internal "s are not problem.

                           

                          The problem may be jQuery is not loaded before reached at blue $.

                          • 10. Re: FM PHP API functionality being interfered with by jQuery
                            beverly

                            It can be done (placing any web app code to be evaluated inside JavaScript). I do it all the time (not just with PHP). Placement of the quoting is delicate. nested quotes (single or double) are a no-no. swapping out single quotes inside double quotes and vice versa is the key.

                             

                            However, duncanbaker says the code works until new jQuery is introduced. I'd see if user19752 is on to something. the what-gets-evaluated-and-when is a huge factor with JavaScript interspersed with other code.

                             

                            beverly

                            • 11. Re: FM PHP API functionality being interfered with by jQuery
                              electon

                              Beverly,

                               

                               

                              beverly wrote:

                               

                              It can be done (placing any web app code to be evaluated inside JavaScript). I do it all the time (not just with PHP). Placement of the quoting is delicate. nested quotes (single or double) are a no-no. swapping out single quotes inside double quotes and vice versa is the key.

                               

                              That's exactly the point I'm trying to make.

                              The function seems to fail at the JavaScript, not the PHP side. 

                              My guess is incorrect use of quoting.

                              The fact that it worked and now it doesn't can be due to other code or this one in combination with the new one.

                               

                              Onlyduncanbaker has the entire code, so I'm out of other ideas. :-)

                               

                              It's just that

                              "<? something here  and quote again "$variable" and close the main quote ?>"

                              evaluates to

                              "<? something here  and quote again"   +   $variable  +    "and close the main quote ?>"


                              So where's the variable / function called $variable

                               

                              It's not that difficult to test.

                              • 12. Re: FM PHP API functionality being interfered with by jQuery
                                duncanbaker

                                Thanks everyone. Have to run out but will post more code later. Watch this space!

                                • 13. Re: FM PHP API functionality being interfered with by jQuery
                                  duncanbaker

                                  Ok, I have attached two files. These contain the top part of the source code of the webpages. I've removed identifying info. The Current Site file is very simple, not much going on before the Google Maps API section kicks in - this works fine. The New Site file has a lot more being referenced and loaded before the Google Maps API section - this fails at the point the FM script is called, as per the first post.

                                   

                                  Hope this helps give more context and perhaps some clues on how to solve? Many thanks for the time taken with this.

                                  • 14. Re: FM PHP API functionality being interfered with by jQuery
                                    user19752

                                    New Site uses jquery-noconflict.js, so $ does not work there.

                                    Preceding code uses

                                    jQuery(window).on('load',  function() {

                                    style.

                                    https://api.jquery.com/jquery.noconflict/

                                    1 2 Previous Next