This is especially true on views, but the first time the enter key is pressed it clears the drop down options and then the second time submits the form, the javascript needs to be a little more aggressive about submitting the form when enter is pressed and submit the form anytime the enter key is pressed and the form OR dropdown has focus.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

drunken monkey’s picture

Project: Search API » Search API Autocomplete
Component: Search pages » Code

I think you are talking about autocomplete, not the base Search API module?
Anyways, this is a flaw in the autocomplete code in core, little we can do about that (except implement autocompletion all over again).

micheas’s picture

I am using the search api module, and the form works as expected when I a use the ajax search of the form.

If I understand correctly, then the bug would be part of the core autocomplete and that is where to look for a solution to the bug.

I will start looking there. Let me know if I am misunderstanding.

drunken monkey’s picture

Category: bug » support
Status: Active » Fixed

Yes, this is due to the core autocomplete not automatically submitting the form. However, as a form field often is only part of the form, you generally also don't want this. Having the option to add it (when creating the autocompletion in the code, via the Form API) would be a possible feature, though, I guess. You could suggest that in the Drupal core issue queue.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

gittosj’s picture

Category: support » bug
Priority: Normal » Critical
Status: Closed (fixed) » Active

I think this is a critically important issue - I'm marking it accordingly & not something we should be using core hack workarounds to fix.

Its a major problem because:
1) User selects choice from autocomplete dropdown and hits return
2) Nothing happens - no code, guide or error message gives any feedback
3) User concludes site is useless or borked and goes elsewhere

The user's behaviour is entirely consistent and sensible - there's no autocomplete search I've seen which expects the users to hit return twice - try Google for example or even Bing - they spend millions on user testing and interface design and they use / require one submit / return to produce results.

Would it be possible it include the JS override from here: http://drupal.org/node/309088#comment-5231542 into this module or does some other solution work better? Happy to offer my time on testing / usability but no ability with code I'm afraid!

gittosj’s picture

For the moment, I've disabled autocomplete since I can't get any of the javascript core overrides to work but keen to help debug and testing if there is a patch. Apache Solr autocomplete does not seem to have the same issue but I like search_api a lot - lot of advantages despite this issue...

drunken monkey’s picture

Category: bug » feature
Priority: Critical » Normal
Status: Active » Needs review
FileSize
2.16 KB

Thanks for the link, with this I was able to patch this relatively quickly (although the proposed variant didn't work right away).
Please test the attached patch!

drunken monkey’s picture

Title: Search form requires enter to be hit twice. » Add auto-submit for the autocompletion
BarisW’s picture

Status: Needs review » Reviewed & tested by the community

Awesome, works just as expected. Thanks very much for fixing this.

BarisW’s picture

Thomas, would you please commit this?

dags’s picture

I can also confirm that the patch works and was a needed fix.

BarisW’s picture

Just one check; on some sites I've applied the patch all works fine. On other sites it only works with keyboard navigation. When I click on a given word in the dropdown with my mouse, no form submit is done.

Not sure what causes this, will let you know if I manage to resolve this.

gittosj’s picture

Thanks DM and sorry for the delay in testing - been away for some weeks. Patch applied fine against 7.x-1.x-dev but doesn't work for me (Drupal 7.15 and Search_Api 7.x-1.2). Can't see anything in the console / debugger to indicate why its failing - still having to keyboard submit / mouse click twice to submit the search. not sure how to give further useful feedback - any suggestions?

Anonymous’s picture

@#14 you might try refreshing Drupal caches.

To auto_submit when mouse-clicking a keyword from the list, I did this (for submitting with Search API Ajax):

      // Auto-submit main search input after autocomplete
      if ( typeof Drupal.jsAC != 'undefined') {
        Drupal.jsAC.prototype.select = function(node) {
          this.input.value = $(node).data('autocompleteValue');
          if ($(this.input).hasClass('auto-submit')) {

            // Use Search API Ajax to submit
            Drupal.search_api_ajax.navigateQuery($(this.input).val());
          }
        };
      }

Notice that I first check that Drupal.jsAC is loaded:
if ( typeof Drupal.jsAC != 'undefined') {
Otherwise javascript breaks (in my case).

I see that the patch from #8 is for onkeyup, does it also handle the select event when mouse-clicking on a keyword?

gittosj’s picture

Thanks morningtime - caches were cleared but thanks for suggestion. Added your function to search_api_autocomplete.js and mouse clicks / selections now work perfectly - thanks. Sadly keyboard submit / return still not working until second submit / return.

BarisW’s picture

Would be worth to implement this patch too: http://drupal.org/node/254477#comment-5203326

It's a core bug in the autocomplete function that when you hit search while the autocompletion hasn't finished yet, you'll get a Javascript error. This only happens with JS aggregation enabled.

By adding the following code to the search_api_autocomplete.js file, the error is gone. I've just copied the Drupal.ACDB.prototype.search function from core into your JS file and applied the patch from #254477: Javascript autocomplete does not cancel properly when clicking on a submit button.

/**
 * Performs a cached and delayed search.
 */
Drupal.ACDB.prototype.search = function (searchString) {
  var db = this;
  this.searchString = searchString;

  // See if this string needs to be searched for anyway.
  searchString = searchString.replace(/^\s+|\s+$/, '');
  if (searchString.length <= 0 ||
    searchString.charAt(searchString.length - 1) == ',') {
    return;
  }

  // See if this key has been searched for before.
  if (this.cache[searchString]) {
    return this.owner.found(this.cache[searchString]);
  }

  // Initiate delayed search.
  if (this.timer) {
    clearTimeout(this.timer);
  }
  this.timer = setTimeout(function () {
    db.owner.setStatus('begin');

    // Ajax GET request for autocompletion. We use Drupal.encodePath instead of
    // encodeURIComponent to allow autocomplete search terms to contain slashes.
    $.ajax({
      type: 'GET',
      url: db.uri + '/' + Drupal.encodePath(searchString),
      dataType: 'json',
      success: function (matches) {
        if (typeof matches.status == 'undefined' || matches.status != 0) {
          db.cache[searchString] = matches;
          // Verify if these are still the matches the user wants to see.
          if (db.searchString == searchString) {
            db.owner.found(matches);
          }
          db.owner.setStatus('found');
        }
      },
      error: function (xmlhttp) {
        if (xmlhttp.status) {
          alert(Drupal.ajaxError(xmlhttp, db.uri));
        }
      }
    });
  }, this.delay);
};
BarisW’s picture

Status: Reviewed & tested by the community » Needs work

Changing status back before this is committed ;)

Andreas Radloff’s picture

For this to work for me I had to add a check for Drupal.search_api_ajax since I don't use AJAX, also the class for auto submit is auto_submit for me, not auto-submit as in your script. Why is that?

  // Auto-submit main search input after autocomplete
  if ( typeof Drupal.jsAC != 'undefined') {
    Drupal.jsAC.prototype.select = function(node) {
      this.input.value = $(node).data('autocompleteValue');
      if ($(this.input).hasClass('auto_submit')) {
        
        if (typeof Drupal.search_api_ajax != 'undefined') {
          // Use Search API Ajax to submit
          Drupal.search_api_ajax.navigateQuery($(this.input).val());
        } else {
          this.input.form.submit();
        }
        
      }
    };
  }

Otherwise the patch and auto submit works good!

mErilainen’s picture

Status: Needs work » Needs review
FileSize
4.18 KB

I have tested all the above mentioned patches and fixes and everything works nicely! I added everything into a single patch, please review.

gittosj’s picture

Tested #20 and seems to work perfectly - thank you and much appreciated. Can we commit this now?

Anonymous’s picture

Status: Needs review » Reviewed & tested by the community

+1 from me, it works for me too.

deepbluesolutions’s picture

+1 working for me too

gittosj’s picture

Upgraded to 7.16 and still works fine but now find that I'm getting a JS error popup "An AJAX HTTP request terminated abnormally" etc when hitting submit before suggestions load. Reading here:

http://drupal.org/node/1232416

... it seems clear that core JS files are still being refactored / worked on and that the patch suggested there breaks autocomplete functionality completely. Workaround is here:

http://drupal.org/node/1232416#comment-6168158

add
window.alert = function() {};

to the top of any loaded JS file and error is suppressed - not a problem since its not a real error bit might not help your debugging elsewhere

drunken monkey’s picture

Status: Reviewed & tested by the community » Needs review
FileSize
2.12 KB

@ mErilainen: Thanks for integrating everything into one patch to test!

However, I don't think we should add these other changes. In detail:

Just one check; on some sites I've applied the patch all works fine. On other sites it only works with keyboard navigation. When I click on a given word in the dropdown with my mouse, no form submit is done.

To auto_submit when mouse-clicking a keyword from the list, I did this

I don't think that submitting the form when the suggestion is only clicked is intuitive. When the Enter key is pressed, yes, then I'd assume the form will be submitted. But when I just click?
You also have to take scenarios into account where the keywords field isn't the only input field and users will want to edit those as well after inputting the keywords. In this case, as a user, I'm pretty sure I'd not assume that clicking a suggestion will submit the form.

What do others say here?

Also, I don't like the style of that change at all. I'm pretty sure that should go into an object with an attach function.

Would be worth to implement this patch too: http://drupal.org/node/254477#comment-5203326

It's a core bug in the autocomplete function that when you hit search while the autocompletion hasn't finished yet, you'll get a Javascript error. This only happens with JS aggregation enabled.

Maybe, but in a different issue, please.
Also, this bug doesn't occur for me anymore, so maybe it's been fixed in core already? #24 would also suggest that this could soon be fixed (but, strangely, that it isn't now) so we probably shouldn't interfere with core there.

Please test the attached re-rolled version of #8 and tell me if it works for the core issue here. (And, of course, also your opinion for auto-submitting when clicking.)

queenvictoria’s picture

For my money I prefer the patch in #20. Reason being (in deference to @drunkenmonkey) Google submits on click (I checked--thrice). And if Google does it the user expects that behaviour everywhere. Whatever the rest of us believe. (In fact I now recall that I discarded search_api_autocomplete as a solution not two weeks ago because it *didn't* apply this behaviour).

I did test both #25 and #20. (Manually as it seems as though they don't apply cleanly?). Both work well for the "press return" function. #20 Works well for click function too. Neither seem to produce errors.

Re: the extra bits and pieces. Perhaps it isn't the right way to do things. Split them up?

I've rerolled #20 for reference.

gittosj’s picture

@drunken monkey - Agree with you that the 'submit with keyboard' functionality is the key functionality here. 'Click term with mouse' function is nice but not vital. Struggled to get you latest patch to apply cleanly but #26 by @queenvictoria applies fine but works well.

If you have ajax switched on in the underlying view then you get a javascript popup (terminated abnormally etc) when you press submit / return before the autocomplete has loaded - workaround - turn off ajax on the the view for the moment.

I'd strongly support merging 'submit with keyboard' into dev - having to re-patch with each new release is getting boring!

exlin’s picture

Status: Needs review » Needs work

Patch doesn't work with Internet Explorer versions 9 and 10, with IE8 it works just fine.
For some reason onKeyUp doesn't get the keycode from enter on first time, meaning you need to press enter twice.

Fixed it by changing in search_api_autocomplete.js file

Drupal.jsAC.prototype.onkeyup = function (input, e) {

to

Drupal.jsAC.prototype.onkeydown = function (input, e) {

And in misc/autocomplete.js (core) file (line 48):

 .keydown(function (event) { return ac.onkeydown(this, event); })

to

    .keydown(function (event) { ac.onkeydown(this, event); })
gittosj’s picture

Still can't get #25 to apply - tried manual apply but chaos! #26 still applies well and works with latest updated dev - would it be possible to submit this to latest dev?

checker’s picture

Confirn patch #26 works. Tested in current FF and IE 8-9
+1 submit on click

yechuah’s picture

Version: 7.x-1.x-dev » 7.x-1.0-beta2
FileSize
937 bytes

base 26 on 1.0-beta2

yechuah’s picture

forgot search_api_autocomplete.js

asak’s picture

+1 for #32 - works great. +1 on the "submit on click" as well... ;)

drunken monkey’s picture

Version: 7.x-1.0-beta2 » 7.x-1.x-dev
Status: Needs work » Fixed

@ yechuah: Please always create patches against the current dev version, not some year-old Beta.

@ queenvictoria: Thanks for the re-roll!

@ all: OK, if everyone else is in favor, then let's also auto-submit for clicks. I committed the patch in #26 now.
Thanks for testing and voting, everyone, and sorry I took so long for committing!

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

sgurlt’s picture

I wrote a little patch that allows the users to deactivate the auto submit function. By default its activated, but the users are now able to disable it by unsetting a checkbox set is attaced to the autocomplete settings form.

sgurlt’s picture

Status: Closed (fixed) » Needs review
drunken monkey’s picture

Status: Needs review » Fixed

Thanks a lot for your contribution, the patch already looks quite good. I just had a few alterations to explain the option better in the UI, and to clean up the code a bit (hopefully).

However, please don't resurrect two years old issues, especially when it's obviously about a related, but separate, feature request.
I created a new issue, please discuss there: #2493919: Add the option to disable auto-submit.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

gauravjeet’s picture

Just a follow-up for someone looking up to this :

  • I override the Drupal.jsAC.prototype.select function from core autocomplete.js to my custom JS file under my module's behavior
  • Patch #19 worked for me, though I only added this.input.form.submit(); out of this
  • I have a fully functional autocomplete that does not require an additional button click

I didn't really touch any core file though !