1

I am trying to add auto-complete to my custom fields on gravity forms.But its not working.Data returns but not showing on field.I am doing like this:

(function( jQuery ) {
    jQuery(function() {
        var url = ajaxurl + "?action=my_search";
        jQuery( "#input_22_2" ).autocomplete({
            source: url,
            minLength: 2
        }); 
    });

})( jQuery );

PHP Code :

function my_search() {
   global $wpdb;
        $term = strtolower( $_GET['term'] );



         $query = $wpdb->get_results( "SELECT * FROM wp_vhh4zdnv0r_disease WHERE scifi_name like '%".$_GET['term']."%' OR hospital_code like '%".$_GET['term']."%' OR layman_language like '%".$_GET['term']."%'");
    if ( $query )
    {
        foreach ( $query as  $valuek) {

           if(strpos($valuek->scifi_name,$_GET['term']) !== false){
            $row[] = $valuek->scifi_name;
           }


        }
    }

        $response = json_encode( $row );
        echo $response;
        exit();

}

    add_action( 'wp_ajax_my_search', 'my_search' );
0

I have solved my problem using below code.So may be helpful for someone.I have tried by reference style for autocomplete

<script>

    var locations = '<?php echo $json;?>';
var locations_array = JSON.parse(locations);
 locations_array.sort();


    function monkeyPatchAutocomplete2() {
            jQuery("#result").remove();
          // Don't really need to save the old fn, 
          // but I could chain if I wanted to
          var oldFn = jQuery.ui.autocomplete.prototype._renderItem;

          jQuery.ui.autocomplete.prototype._renderItem = function( ul, item) {
              var re = new RegExp( "\\b" + this.term, "i") ;
              var t = item.label.replace(re,"<span class='highlighted'>" + this.term + "</span>");
              return jQuery( "<li></li>" )
                  .data( "item.autocomplete", item )
                  .append( "<a>" + t + "</a>" )
                  .appendTo( ul );

          };
      }


      jQuery(document).ready(function() {

        jQuery('.medication input').live('keyup', function (event) {

    if(this.value.length < 2){
        jQuery("#result").remove();
    } 

          monkeyPatchAutocomplete2();
          //jQuery("#result").remove();
           jQuery( ".medication input" ).autocomplete({

            matchContains: true,
            multiselect: true,            
             minLength: 2,
              source: function(req, responseFn) {

                  //addMessage("search on: '" + req.term + "'<br/>");
                  var re = jQuery.ui.autocomplete.escapeRegex(req.term);
                  var matcher = new RegExp(  "\\b" + re, "i" );
                  var a = jQuery.grep( locations_array, function(item,index){
                      //addMessage("&nbsp;&nbsp;sniffing: '" + item + "'<br/>");
                      return matcher.test(item);
                  }); 


                  responseFn( a );
              },
              focus: function (event, ui) {

                jQuery("#result").remove();
                return false;
            },
          });


});


 </script>

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.