1

I'm new to Drupal development. I have inherited a Drupal 6 site. I would like to add to the content-profile fields a "live search" text field that will give the user options from a database based on the letters typed. I already have the following:

  1. Database
  2. Profile field with onkeyup function in the .js file.
  3. Function that gets the correct values from the database
  4. any number of tutorials that explain how to use ajax for connecting the onkeyup function and the database query.

What I don't understand is how to put the search results into the "allowed values" of the text field? All the examples define an additional < div > to put the results in. Here it's part of the text field. What code should be in the allowed values so that it will show the correct values? Where to put the retrieved values?

Any help would be appreciated!

0

You're asking how to make an Ajax search request from a textfield.

I'm not going to answer your exact question but you need some javascript to fill in the results from your sql query into the search field. Here is an example which fills in an HTML <options> list from a Drupal sql search:

jQuery(function($) {

  $('#edit-field-survey-program-und option:gt(0)').remove();
  $('#edit-field-survey-program-und').attr('disabled', 'disabled');

  // When a country is selected. Find the Academic Programs within that
  // country from an SQL lookup. Then fill in the options values and
  // make that options list enabled for the site visitor.
  $("#edit-field-survey-country-und").click(function() {
    var selected_country = $("#edit-field-survey-country-und").val();
    if (selected_country != '_none') {
      // Do our SQL lookup from a service enpoint with a search parameter.
      $.ajax({
        type:'GET',
        url: '/eap-ajax/eap-country-programs/' + selected_country,
        data: '',
          success:function(data){
          // Replace values of target form element with found results.
          // Enable for enduser.
          $('#edit-field-survey-program-und option:gt(0)').remove();
          $.each(data, function(key, value) {
            $('#edit-field-survey-program-und').append(
              '<option value="' + key + '">' + value + '</option>'
            );
          });
          $('#edit-field-survey-program-und').removeAttr('disabled');
        }
      });
    }
  });
});

The above example is the same 3 pieces for any AJAX thing.

  1. Make a click event listener function
  2. Do your SQL business logic
  3. Inject your results somewhere back into the page

For a full writeup feel free to see my more complete writeup: http://blog.arvixe.com/drupal-ajax-autocomplete-dependant-form-elements-using-jquery/

I suggest googling how to fill in results to an <input> text field using jQuery.

  • However, it still doesn't work (also after changing some things to fit with drupal 6). Do you have any recommendations how to debug? I can't use the devel module for this as it is in the user registration form, before signing in so devel doesn't kick in (unless I'm doing something wrong?) I checked that going to the url returns the answer, it does. How can I check it is being called? Thank you! – Danielle Nov 12 '15 at 8:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.