jQuery + Ajax + PHP + database
Hi guys, I really need a hand over here!
So I have a WordPress HTML form and i have a SSN field with autocomplete, and based on the value that user inputs in that field I need to populate the rest of the form from the database which works fine when the user selects what autocomplete returns, but if the user inputs or copy pastes the SSN number into that field other fields aren't populated.
I've tried with Ajax but I have no experience in it and it doesn't seem to work.
Some help please :(
Here's some of my code
- $("#SSN").autocomplete({
- source: "search.php",
- select: function(event, ui) {
- $("#SSN").val(ui.item.SSN);
- $("#Name").val(ui.item.Name);
- $("#Surname").val(ui.item.Name);
- },
- minLength: 0
- });
- $('#SSN').live('change', function() {
- //ajax request
- $.ajax({
- type: "GET",
- url: "search.php",
- data: {
- 'SSN': $('#SSN').val()
- },
- dataType: 'json',
- success: function(data) {
- $("#SSN").val(data);
- $("#Name").val(ui.item.Name);
- $("#Surname").val(ui.item.Name);
- }
- });
- });
The PHP script works fine for the autocomplete and basically doesn this (just part of the script)
- foreach($field as $data){
- if(stripos($data['SSN'], $term) !== false){
- $data['value'] = $data['SSN'];
- $data['label'] = "{$data['SSN']}, {$data['Name']} {$data['Surname']}";
- $matches[] = $data;
- }
- }
-
- $matches = array_slice($matches, 0, 5);
- echo json_encode($matches);