We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 46577
    • 54 Posts
    I'm porting an old PHP/MySQL app over to MODX/PDO and got a bit stumped over an AJAX search form.

    The form takes a single input, searches a database once the user has input at least 4 characters and returns results that are LIKE the input.

    I have it working fine when my MODX form queries an external PHP file like this:
    xmlhttp.open("GET", "assets/php/bookingsearch.php?q=" + str, true);


    and then in my external PHP file:
    $q=$_GET["q"];


    and in the external PHP query:
    WHERE `name` LIKE '%$q%'


    BUT - I would like to accomplish the PHP part in a snippet so the whole is entirely in MODX

    So I'm calling a snippet from a resource and if I hard-code the value of $q in my snippet it works fine but if I try to pass the parameter 'q' nothing is returned.

    In my MODX form (tried various combinations of things):
    xmlhttp.open("GET", "[[~17]]? &q=" + str, true);


    and in my snippet:
    $q=$_GET['q'];


    and in my snippet query:
    WHERE `name` LIKE "%'.$q.'%"


    and to repeat if I hard-code $q for example $q="smit"; it works and returns all the Smiths in the database as expected. So I'm guessing my syntax is awry but I can't figure out where... can somebody point me in the right direction?

    This question has been answered by BobRay. See the first response.

      • 4172
      • 5,888 Posts
      did you check your browser-console, if you pass the correct query-params?
      I think, it should be:

      xmlhttp.open("GET", "[[~17]]?q=" + str, true);
      


      and make sure, to sanitize the users input, before you pass it to a sql-query.
      At least, you should use XPDO to build the query.
        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 46577
        • 54 Posts
        Thanks for your suggestion Bruno, unfortunately that is what I started off with. I tried it again to be sure, but no success.

        In my form I have a spinner image which appears whilst waiting for a server response if(xmlhttp.readyState == 4 && xmlhttp.status == 200). The image shows and doesn't go away, so it would seem there is no response.

        I don't see anything relevant in the browser console either...

        A couple of other possible clues:

        In my form if I use
        xmlhttp.open("GET", "[[~17]]", true);
        AND on my snippet I hard-code a value for $q, then I get the correct response from the server.

        BUT if I try to send any GET parameter from my form, and if my snippet $q is still hard-coded, I never get a response.

        ALSO if I try to send a GET parameter from the form, and use GET in the snippet (as it should be), no response.
          • 46577
          • 54 Posts
          UPDATE:
          I noticed when using
          xmlhttp.open("GET", "[[~17]]?q=" + str, true);
          as my AJAX call in the browser console I see an error
          Failed to load resource: http://www.mysite.com/subfolder/index.php?id=17?q=smit the server responded with a status of 500 (Internal Server Error)
          which would seem to be the incorrect link, so I changed the AJAX call to
          xmlhttp.open("GET", "[[~17]]&q=" + str, true);
          Now the link appears in the console as
          http://www.mysite.com/subfolder/index.php?id=17&q=smit
          which seems correct, but still getting a 500 server error.

          Thought enabling friendly URLs might help, but no joy there either. As soon as I enable FURLs I get a 500 error when trying to load any page. I have followed the various troubleshooting guides re FURLs but the problem persists.

          This is the second installation of MODX on the site, the main one is in root and FURLs work there without any problem. On that subject, I have a .htaccess file in both root and subfolder with RewriteEngine On and RewriteBase pointing the appropriate place i.e. / for root and /subfolder for subfolder.
            • 46577
            • 54 Posts
            now this is odd, after enabling/disabling friendly URLs, the previous errors now appear as 404 not found.

            another thing, if I type the following directly into the browser
            http://mysite.com/subfolder/index.php?id=17
            the resource and snippet return all the records in the table as expected.

            but if I type
            http://mysite.com/subfolder/index.php?id=17&q=smit
            I'm redirected to the index page, which I presume is the equivalent of a 404 not found?

            so something about the way MODX handles URL parameters? Am I just approaching this in entirely the wrong way? Any clues? Anyone?

            probably should have mentioned:
            MODX Revolution 2.5.5-pl
            Apache 2.2.24
            PH 5.4.45
            MySQL 5.5.51-38.2 [ed. note: grogorio last edited this post 7 years, 1 month ago.]
            • discuss.answer
              • 3749
              • 24,544 Posts
              Do you have friendly URLs turned on? If so, MODX uses "q" in the rewrite rule:

              # The Friendly URLs part
              RewriteCond %{REQUEST_FILENAME} !-f
              RewriteCond %{REQUEST_FILENAME} !-d
              RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
              


              Using a different variable in your query string might solve it. You can also get MODX to use a different variable, but I can't remember how.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
                • 46577
                • 54 Posts
                no I turned friendly URLs off again due to the 500 errors... a topic for another thread I guess

                BUT

                I replaced $q with $data AND IT WORKS!

                So kudos and many thanks to you Sir Bob for your most excellent hunch and suggestion
                  • 3749
                  • 24,544 Posts
                  I'm glad you got it sorted. smiley
                    Did I help you? Buy me a beer
                    Get my Book: MODX:The Official Guide
                    MODX info for everyone: http://bobsguides.com/modx.html
                    My MODX Extras
                    Bob's Guides is now hosted at A2 MODX Hosting