Ajax Startup Script to Fill Fields (v2019)

Following the examples in the PHPMaker v 2019.0.10 Help → Lookup Table → “Ajax by API and Client Scripts”…
I have made the following code and it works:

$(“#x_regid”).change(function() {
var data = { “action”: “lookup”, “linkTable”: “registrations”, “linkField”: “regid”, “displayFields”: [“partfname”], “lookupValue”: encodeURIComponent($(this).val()) };
var row = ew.ajax(data);
if (row && row[“partfname”])
$(“#x_partfname”).val(row[“partfname”]); // Set the result (manipulate it first ifnecessary) to the target field
});

Now the only problem is that I want to fill about 7 more fields from the same record in the lookup table. (i.e. not just ‘partfname’ but also ‘partlname’ ‘partaddress’ ‘partcity’ ‘partst_prov’ ‘partcountry’ ‘partphone’ etc.)

How do I modify this code to retrieve and ultimately fill multiple fields?

Thank you,

You may refer the code in “Example 4 - Auto-Fill asynchronously by custom API action and client script” under the “Lookup Table” topic from PHPMaker Help menu.

Thank you for the reply.
In reading Example 4, I understand the retrieval of the array, but not the assigning of the proper value to the proper field.
Example 4 shows how to retrieve the entire row from the lookup table, but the example it shows for inserting the “unitprice” is as follows:

$(“#x_ProductID”).change(function() {
var url = ew.API_URL, action = “getUnitPriceByProductID”, id = encodeURIComponent($(this).val());
//$.get(url + “/” + action + “?ProductID=” + id, function(res) { // URL format if URL Rewrite enabled
$.get(url + “?action=” + action + “&ProductID=” + id, function(res) { // Get response from custom API action
if (res)
$(“#x_UnitPrice”).val(res); // Set the result (manipulate it first if necessary) to the target field
});
});

The line that reads:
$(“#x_UnitPrice”).val(res); // Set the result (manipulate it first if necessary) to the target field
…leaves out an example of how to assign the rest of the values to their respective fields. If the above example explains how to auto fill the ‘unitprice’ what would the example be to auto fill the ‘size’ and ‘color’ field values as well?

Thank you again for helping make this more clear.

If you want to return a whole row as JSON,you may use WriteJson() in combination with ExecuteRow() (see Server Events and ClientScripts),e.g.

WriteJson(ExecuteRow("SELECT * FROM products WHERE ProductID = " . AdjustSql($productId))); // Output the row (array) as JSON

Thanks again for replying.
I searched this forum for different uses of the ‘.change(function)’ and found a combination that works:

//this is the Global code (see Ajax by API adn Client Scripts in Help File)

$API_ACTIONS[“getreginfo”] = function(Request $request, Response $response) {
$regid = Param(“regid”); // Get the input value from $_GET or $_POST
if ($regid !== NULL)
WriteJson(ExecuteRow("SELECT * FROM registrations WHERE regid = " . AdjustSql($regid))); // Output the row (array) as JSON
};

//
//
//

// And this is the client Startup Script for Add Page

$(“#x_regid”).change(function() {
var url = ew.API_URL, action = “getreginfo”, id = encodeURIComponent($(this).val());
//$.get(url + “/” + action + “?ProductID=” + id, function(res) { // URL format if URL Rewrite enabled
$.get(url + “?action=” + action + “&regid=” + id, function(res) { // Get response from custom API action
if (res)
$(“#x_partfname”).val(res[‘partfname’]); // Set the result (manipulate it first if necessary) to the target field
$(“#x_partlname”).val(res[‘partlname’]);
$(“#x_numassocmembers”).val(res[‘numassocmembers’]);
$(“#x_partassocmember”).val(res[‘partassocmember’]);
$(“#x_partaddress”).val(res[‘partaddress’]);
$(“#x_partcity”).val(res[‘partcity’]);
$(“#x_partcountry”).val(res[‘partcountry’]);
$(“#x_partst_prov”).val(res[‘partstprov’]);
$(“#x_partzip”).val(res[‘partzip’]);
$(“#x_partphone”).val(res[‘partphone’]);
$(“#x_partemail”).val(res[‘partemail’]);
$(“#x_gtoaanumber”).val(res[‘gtoaanumber’]);
$(“#x_partclub”).val(res[‘partclub’]);
});
});


…hope that helps.
It seems to be working for me. The reason I’m using the global function and calling it from client script is so that I can take advantage of both dynamic lookup field filtering (i.e. selecting a ‘country’ will filter the available options in the ‘state-province’ field) - and an auto-fill that occurs when another select field option is chosen. I found that combining field filtering with auto fill creates a conflict of some kind, and attempting to auto fill a filtered field (say that 10 times really fast) will result in the filtered field being left out (i.e. null) after auto fill. So the Ajax Global Code + Client Startup Script produces that auto fill effect while I can still use the traditional field filtering.
Thanks again for all the responses, it helped point be in the right direction.
Cheers,