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.
The topic ‘Autocompleting form using PHP and jQuery’ is closed to new replies.