Using JQuery UI for auto-completing form in WordPress

Using JQuery UI for auto-completing form in WordPress

Hi everyone,

I've been trying to embed jQuery and jQuery UI for my WordPress site, but the UI isn't loading at the moment. But first, I thought it would be a good idea to describe in detail what is it exactly that I'm trying to do.

At the moment, I've created a request form where as soon as you enter a number into a field, it will grab the data from a PHP file and the form will autocomplete. 

I've created a file called autofill.js;

  1. jQuery(function(){
  2. var ac_config = {
  3. source: "record.php",
  4. select: function(event, ui){

  5. $("#custom_code").val(ui.item.cust_code);

  6. $("#custom_name").val(ui.item.cust_name);

  7. $("#address").val(ui.item.address_1);

  8. $("#suburb").val(ui.item.suburb);

  9. $("#state").val(ui.item.state);

  10. $("#post_code").val(ui.item.post_code);
  11. },
  12. minLength:3
  13. };
  14. jQuery(".custom_code").autocomplete(ac_config);
  15. });

and here is a PHP file that I called record.php;

  1. <?php

  2. // Data could be pulled from a DB or other source
  3. $cust_codes = array(
  4. array('custom_code'=>'44LDM', custom_name=>'John Doe, address=>’44 Lakeshore Drive', suburb=>'Melbourne', state=>'VIC', post_code=>'3034'),
  5. );

  6. $term = trim(strip_tags($_GET['term']));

  7. //Search record
  8. $matches = array();
  9. foreach($cust_codes as $cust_code){
  10. if(stripos($cust_code['cust_code'], $term) !== false){
  11. // Add the necessary "value" and "label" fields and append to result set
  12. $cust_code['value'] = $cust_code['cust_code'];
  13. $cust_code['label'] = "{$cust_code['cust_code']}, {$cust_code['cust_name']} {$cust_code['address']} {$cust_code['suburb']} {$cust_code['state']} {$cust_code['post_code']}";
  14. $matches[] = $cust_code;
  15. }
  16. }

  17. // Truncate, encode and return the results
  18. $matches = array_slice($matches, 0, 5);
  19. print json_encode($matches);

and finally, this is what I have written in my functions.php file.

  1. if (is_page('Form')) :
  2. wp_enqueue_script('autofill', get_stylesheet_directory_uri(). "/js/autofill.js", array('jquery'), $version, true);
  3. wp_register_style('jquery-style', 'http://af-design.com/styles/jquery-ui-1.8.1.custom.css');
  4. wp_enqueue_style('custom-css');
  5. wp_register_script('jquery-ui', get_stylesheet_directory_uri(). "/js/jquery-ui.min.js", array('jquery'), null, true); 
  6. wp_register_script('jquery-ui', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.1/jquery-ui.min.js', array('jquery'), null, true);
  7. wp_enqueue_script('jquery-ui');
  8. wp_register_script('jquery', 'http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js', false, '1.4.2');
  9. wp_enqueue_script('jquery');
  10. endif;  

When I test the form and I check the console using the element inspector, it says that the record has loaded once I enter a number, but my UI won't appear at all.

Is there something wrong with the way I've written the code embedding for the jQuery in my functions file?

Thanks.