Auto-Complete AJAX onChange (Works w/Standalone Form, NOT for IFRAME ... Help?)

This Auto-Complete example works for a standalone application/form, but NOT IF that form is within an IFrame (like when using a Menu Application … WHY?)

Form: Single Record
Auto-Complete Field: “co_account”, type = “Text”, with Ajax event “onChange” contains the following
(Also reads {userid} field for data limiting in sql statement, so more than 1 user can have the same account numbers!)

The form has many fields, and the Auto-Complete can be added to more than 1 field (each with seperate/individual ‘onChange’ events) … Use a “$sql_field” to specify the fields extracted from a table … Use a “$field_array” to specify the names to loop through on the form for auto-complete (example, you can have “to_account” with auto-complete for “to_account,to_name,to_city,to_state” AND have “from_account” with auto-complete for “from_account,from_name,from_city,from_state”, etc.) … Auto-complete field can utilize additonal ‘Capture/Link’ to lookup field data.

AJAX EVENT:
co_account_onChange()

$table = "company_orders";
$sql_where = "WHERE co_account = '{co_account}' AND userid = {userid}";
$sql_order = "";
$sql_limit = "LIMIT 1";
$sql_field = "co_account,co_dept,co_name,co_company,co_address,co_city,co_state,co_zipcode,co_country,co_email,co_phone,co_fax";
$field_array = array('co_account','co_dept','co_name','co_company','co_address','co_city','co_state','co_zipcode','co_country','co_email','co_phone','co_fax');

// sql_field and field_array should be the same names and count of fields,
// but sql_field can be modified to rename/map field names to match the names on the form
// like: $sql_field = "co_account,co_dept AS department,co_name AS name", etc.

$sql = "SELECT DISTINCT " . $sql_field . " FROM " . $table . " " . $sql_where . " " . $sql_order . " " . $sql_limit . ";";
sc_lookup(dataset,$sql);
$field_array_cnt = count($field_array, COUNT_RECURSIVE);

if ({dataset} === false) {
 sc_error_message("ERROR: Unable to access " . $table . " table. Please report this error to the system administrator.");
} elseif (empty({dataset})) {
 sc_error_message("No Match for Auto-Complete.");
} else { 
// BEGIN WORKS IN STANDALONE FORM
 $i = 0;
 while ($i < $field_array_cnt) {
  {field_name} = $field_array[$i] ;
  {field_data} = {dataset[0][$i]} ;
  sc_master_value(''.{field_name}, ''.{field_data}) ;
  $i++;
 }
// END WORKS IN STANDALONE FORM

// BEGIN TEMPORARY SOLUTION FOR IFRAME
  {co_account} = {dataset[0][0]};
  {co_dept} = {dataset[0][1]};
  {co_name} = {dataset[0][2]};
  {co_company} = {dataset[0][3]};
  {co_address} = {dataset[0][4]};
  {co_city} = {dataset[0][5]};
  {co_state} = {dataset[0][6]};
  {co_zipcode} = {dataset[0][7]};
  {co_country} = {dataset[0][8]};
  {co_email} = {dataset[0][9]};
  {co_phone} = {dataset[0][10]};
  {co_fax} = {dataset[0][11]};
// END TEMPORARY SOLUTION FOR IFRAME
} 

The above shows how it can work within an IFrame, BUT ONLY IF YOU SPECIFY EACH FIELD (without looping)
Like:
{co_account} = {dataset[0][0]};
{co_name} = {dataset[0][1]};

I want to use it within an IFrame, but I don’t want to write each field every time, I want to use a “$field_array” and go thru a loop to populate the fields! (Like in the // WORKS IN A STANDALONE FORM section)
Like:
sc_master_value(’’.{field_name}, ‘’.{field_data}) ;


HELP !!! … Why does the Loop section work in a standalone form, but not if the form is loaded within a Menu Application (or within an IFrame) ?? … I have tried everything to make this work, but I can only get it to work in a standalone form application.

Anyone?

PS. I hope someone gets some use out of this Auto-Complete code!! It was a real PITA to figure out!

Re: Auto-Complete AJAX onChange (Works w/Standalone Form, NOT for IFRAME … Help?)

Thank you very much for this excellent posting which helped me to save a lot of time!

The Documentation of this feature in SC is really, really bad!

I can confirm what you said regarding the iFrame. But I assume that this is not a bug because the macro seems to be intended for communication between a slave and a master application - not for communication within the same application.

Best regards
Arno