Thanks to some helpful guidance, I was able to get this working. I post my working code here for the benefit of anyone who might want to do something similar.
The requirement: User enters a key value; js and php do a lookup to see if the value is in my database, returning some information if yes, or a not found message. Page logic uses the result to determine if the user is directed to the page to give details (if not found), or bypass details if found.
I have a folder SEI under the standard hooks folder. In it are the files:
custom_code_SEI.js, custom_code_SEI.php, and ajax-loader.gif
The form has custom js enabled, with the URL https://showentriesinfo.com/smartform/hooks/SEI/custom_code_SEI.js
The php does the lookup of the key value from the user input, and returns results:
<?php
ini_set('display_errors',1);
require('../../config.php');
require('../../includes/helper-functions.php');
require('../../includes/db-core.php');
$dbh = mf_connect_db();
$query = "SELECT Dogname, DogID FROM vQESEIDogList WHERE AKCRegNo = ?";
$params = array($_POST['input_search_from']);
$sth = mf_do_query($query,$params,$dbh);
$row = mf_do_fetch_result($sth);
if($row != FALSE){
die(json_encode($row));
}else{
$nodata = array("Dogname" => "Not on file at ShowEntries",
"DogID" => "0");
die(json_encode($nodata));
}
?>
The javascript file:
$(function(){
var form_db = "SEI";
var input_search_from = "#element_8";// AKC Number input
var input_to_populate_1 = '#element_10';// Dog
var input_to_populate_2 = '#element_53';// DogID
var x_timer;
$(input_search_from).keyup(function (e){
clearTimeout(x_timer);
x_timer = setTimeout(function(){
makeLookUp($(input_search_from).val());
}, 1000);
});
function makeLookUp(input_search_from){
$(input_search_from).next('img').remove();
$(input_search_from).after('<img src="hooks/'+form_db+'/ajax-loader.gif" style="margin-left:1%"/>');
$(input_to_populate_1).val(" ");
$(input_to_populate_2).val("0");
$.post('hooks/'+form_db+'/custom_code_'+form_db+'.php',{
'input_search_from':input_search_from
},
function(data) {
$(input_search_from).next('img').remove();
$(input_to_populate_1).val(data.Dogname);
$(input_to_populate_2).val(data.DogID);
},
"json"
);
}
});