Title: Autocompleting form using PHP and jQuery
Last modified: August 30, 2016

---

# Autocompleting form using PHP and jQuery

 *  [david221](https://wordpress.org/support/users/david221/)
 * (@david221)
 * [10 years, 6 months ago](https://wordpress.org/support/topic/autocompleting-form-using-php-and-jquery/)
 * Hello,
 * I’ve created a request form for my WordPress site, and I want it to autofill 
   every field from a record once I enter the customer code into the text field.
 * I’ve created a PHP file to store my record, and I’m using jQuery to communicate
   to my PHP and refer to variables for each text field on the form.
 * The form itself is created with a WP plugin called Gravity Forms.
 * **autofill.js**
 *     ```
       jQuery(function(){
       	var ac_config = {
       		source: "record.php",
       		select: function(event, ui){
   
       			$("#custom_code").val(ui.item.cust_code);
   
       			$("#custom_name").val(ui.item.cust_name);
   
       			$("#address").val(ui.item.address_1);
   
       			$("#suburb").val(ui.item.suburb);
   
       			$("#state").val(ui.item.state);
   
       			$("#post_code").val(ui.item.post_code);
       		},
       		minLength:3
       	};
       	jQuery(".custom_code").autocomplete(ac_config);
       });
       ```
   
 * **record.php**
 *     ```
       <?php
   
       // Data could be pulled from a DB or other source
       $cust_codes = array(
       	array('custom_code'=>'44LDM', custom_name=>'John Doe, address=>’44 Lakeshore Drive', suburb=>'Melbourne', state=>'VIC', post_code=>'3034'),
       );
   
       $term = trim(strip_tags($_GET['term']));
   
       //Search record
       $matches = array();
       foreach($cust_codes as $cust_code){
       	if(stripos($cust_code['cust_code'], $term) !== false){
       		// Add the necessary "value" and "label" fields and append to result set
       		$cust_code['value'] = $cust_code['cust_code'];
       		$cust_code['label'] = "{$cust_code['cust_code']}, {$cust_code['cust_name']} {$cust_code['address']} {$cust_code['suburb']} {$cust_code['state']} {$cust_code['post_code']}";
       		$matches[] = $cust_code;
       	}
       }
   
       // Truncate, encode and return the results
       $matches = array_slice($matches, 0, 5);
       print json_encode($matches);
       ```
   
 * So what is the problem?
 * 1) It doesn’t look like Gravity Forms allows you to change the input ids; at 
   the moment, the input fields don’t match with the variables I’ve got in jQuery
   and php files, and;
    2) I keep getting a TypeError on the ‘$’ symbol in the jQuery
   file.
 * Is there a more efficient way to do this, or am I on the right track?
    I’m quite
   stuck at the moment.
 * Thanks.
 * [https://wordpress.org/plugins/gravity-forms-addons/](https://wordpress.org/plugins/gravity-forms-addons/)

The topic ‘Autocompleting form using PHP and jQuery’ is closed to new replies.

 * ![](https://ps.w.org/gravity-forms-addons/assets/icon-256x256.jpg?rev=996787)
 * [Gravity Forms Directory](https://wordpress.org/plugins/gravity-forms-addons/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/gravity-forms-addons/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/gravity-forms-addons/)
 * [Active Topics](https://wordpress.org/support/plugin/gravity-forms-addons/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/gravity-forms-addons/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/gravity-forms-addons/reviews/)

## Tags

 * [form](https://wordpress.org/support/topic-tag/form/)
 * [jquery](https://wordpress.org/support/topic-tag/jquery/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 0 replies
 * 1 participant
 * Last reply from: [david221](https://wordpress.org/support/users/david221/)
 * Last activity: [10 years, 6 months ago](https://wordpress.org/support/topic/autocompleting-form-using-php-and-jquery/)
 * Status: not resolved