We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 6561
    • 139 Posts
    Is it possible to have a database with -for example- cities, and as the user types in his/her city, results are fetched from the database and the input box shows a list with possible choices?

    Maybe eForm has something like that? Sorry, I did not have the opportunity to try out
    eForm yet, been busy with trying out a lot of other stuff first!

    Anyway, I’d like to know.
      • 24719
      • 194 Posts
      you could do this best with some ajax.

      if you’re using a javascript framework like jquery, google ’jquery autocomplete’. you’ll need to modify the ajax php script they provide to interface with your db table (see ajaxsearch for an example of how to do this).

      if ajax is beyond you, see:
      http://www.pengoworks.com/workshop/jquery/autocomplete.htm

      you could prefill the array with a template variable
        • 6561
        • 139 Posts
        Thanks. And thank you for the workshop link as well.

        Yeah, Ajax is still somewhat beyond me. Actually, it’s another reason
        why I find Modx so useful... I can use Ajax plug-ins without having to know
        too much about Ajax internals. Provided they’re available of course.
          • 22303 MODX Staff
          • 10,725 Posts
          I actually just implemented this autocomplete on a MODx site the other day (it’s private/secure or I’d show you). Might be worth looking at though...

          http://www.brandspankingnew.net/specials/ajax_autosuggest/ajax_autosuggest_autocomplete.html

            • 7455
            • 2,204 Posts
            Quote from: OpenGeek at Apr 03, 2007, 10:45 AM

            I actually just implemented this autocomplete on a MODx site the other day (it’s private/secure or I’d show you). Might be worth looking at though...

            http://www.brandspankingnew.net/specials/ajax_autosuggest/ajax_autosuggest_autocomplete.html

            Could you post the snippet that produces the xml or json file?

            Dimmy
              follow me on twitter: @dimmy01
              • 22303 MODX Staff
              • 10,725 Posts
              Sure, though, as you’ll see, this snippet is authored against a custom xPDO data model (xPDOAdmin extends xPDO and encapsulates the connection to the database), and might have some PHP 5 only syntax, though that could be easily remedied.
              <?php
              $output= '';
              $team_contact= '';
              if (isset ($_GET['search'])) {
                  $team_contact= $_GET['search'];
              }
              if ($team_contact) {
                  include_once ($modx->config['base_path'] . 'assets/snippets/admin/xpdoadmin.class.php');
                  $xpdo= & new xPDOAdmin($modx);
                  $results= array ();
                  $criteria= new xPDOCriteria($xpdo, "SELECT * FROM {$xpdo->getTableName('TeamContacts')} WHERE `name` LIKE {$xpdo->quote($team_contact . '%')} ORDER BY `name` ASC LIMIT 0,20");
                  if ($collection= $xpdo->getCollection('TeamContacts', $criteria)) {
                      foreach ($collection as $itemKey => $item) {
                          $results[]= array (
                              "id" => $itemKey,
                              "value" => $item->get('name'),
                              "info" => implode(', ', $item->get(array('city','state')))
                          );
                      }
                  } else {
                      return '';
                  }
                  foreach ($results as $result) {
                      $jsonResults[]= $xpdo->toJSON($result);
                  }
                  $modx->setPlaceholder('autosuggest-results', implode(",", $jsonResults));
              }
              $output.= $modx->getChunk('autosuggest.json.outer');
              return $output;
              

              And here is the chunk autosuggest.json.outer for reference...
              { results: [
              [+autosuggest-results+]
              ] }
                • 7455
                • 2,204 Posts
                Thanks Jason,

                This is so nice all this great code all able to be used in modx.

                So much to learn and so limited time ahhh.

                  follow me on twitter: @dimmy01