Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

thanks for reading.

I have a database table containing organisation names. When a user registers on the website, they use a text field I added, which autocompletes with existing organisation names, as a way of attaching them to existing organisations. This used to work fine, but doesn't seem to now, presumably after one of the updates. Now it simply doesn't load anything when typing in, though the little circle appears to indicate that it is connected.

Here's the code:

$items['org/autocomplete'] = array(
  'page callback' => '_org_autocomplete',
  'access callback' => TRUE,
  'type' => MENU_CALLBACK
);

...and...

function _org_autocomplete($string = '') {
  $matches = array();
  $query = db_select('org_orgs', 'c'); // I've checked that the table exists, contains data, and is of this name - that's fine
  $return = $query
    ->fields('c', array('name'))
    ->condition('c.name', '%' . db_like($string) . '%', 'LIKE')
    ->range(0, 10)
    ->execute();

  foreach($return as $row) {
    $matches[$row->name] = $row->name;
  }
  if ($_SERVER['HTTP_REFERER'] == url('user/register', array('absolute' => TRUE))){
    drupal_json_output($matches);

    // The if statement surrounding this line prevents users
    // from viewing the full list by going directly to the
    // /org/autocomplete URL. In removing the if statement, I can
    // test to see if it works at the URL, and it does just fine

  }
}

I've checked that the table exists, contains data, and is of this name - that's fine. The if statement surrounding the drupal_json_output() line prevents users from viewing the full list by going directly to the URL. In removing the if statement, I can test to see if it works at the URL, and it does just fine.

function org_form_user_register_form_alter(&$form, &$form_state, $form_id) {
  $form['field_organisation']['und'][0]['value']['#autocomplete_path'] = 'org/autocomplete';
  // Though no results appear, the 'org/autocomplete' path
  // is confirmed as the correct path because changing it to
  // anything else removes the little autocomplete circle.
  $form['field_organisation']['und'][0]['value']['#description'] = 'If your organisation appears in the above predefined dropdown menu, please select it.';
  $form['#validate'][] = 'org_form_validate';
  $form['#submit'][] = 'org_form_submit';
}

Though no results appear, the 'org/autocomplete' path is confirmed as the correct path because changing it to anything else removes the little autocomplete circle.

Do you have any ideas of what's gone wrong, or if not, what I can do to investigate?

Additionally, in searching for the solution, I found this drupal update: http://drupal.org/node/1901518 Could this be the cause? And if so, what can I do to correct it?

Thanks

share|improve this question
add comment

2 Answers

What you say suggests it's this line that's the issue:

 if ($_SERVER['HTTP_REFERER'] == url('user/register', array('absolute' => TRUE))){

so there's presumably some difference between the referrer URL and the one generated by the url() function. You might add lines above it to record the two values using watchdog: that would confirm you are reaching that point in the code and help diagnose the issue.

share|improve this answer
    
Alfred, I'm sorry, I think I may have rushed writing this and so may not have been as clear as I could have. The line you've highlighted simply prevents users from looking at the autocomplete json from the /user/register URL. They can only look at results via the textfield when typing in a query. When that line is added, the URL at /user/register returns blank, and the form field (should) work, when removed, the /user/register URL returns with results, and the form field (should) work. I've accidentally written the wrong URL in my PHP comment above, will edit now. –  Carl Mar 5 '13 at 13:16
    
To further clarify from my comment above, the addition or removal of that line has, and should have no effect on the workings of the autocomplete on the registration page, but instead is there to stop users from viewing the full json data at the /user/register URL. –  Carl Mar 5 '13 at 13:23
    
I understand why the line is there. It just seemed that it might be the case that it's not working as intended, if for example the actual referrer url being sent does not match what is expected. If that's the case then it'll prevent anything being returned on the registration page as well as on others. –  Alfred Armstrong Mar 5 '13 at 13:45
    
No, I'm afraid that's not it :( Removing the if statement all together and then clearing the cache doesn't cause the autocomplete to work. –  Carl Mar 5 '13 at 14:14
    
How puzzling. Have you tried using Firebug or the Chrome equivalent to check what's going over the network? –  Alfred Armstrong Mar 5 '13 at 14:28
show 3 more comments

I've discovered that the issue was caused by the reCAPTCHA module. Disabling the AJAX API in the configuration pages resolved this.

share|improve this answer
    
I have a similar issue but I do not have the reCAPTCHA module installed. Is there anything else you might think of thats causing the issue ? –  Jayaram Mar 13 '13 at 14:02
    
Jayaram, I'd advise disabling and re-enabling all of your modules, one at a time to see if it's one of them. –  Carl Apr 2 '13 at 14:00
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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