Populate fields from API data

comfix 18h ago

Hi, I'm trying to get info from an API filled out into the fields of my form.After submit, I do see the array getting the info (debug). But the form disappears the page is empty.

On Submit this php code and need it to populate it on page1:

// VIN ophalen
$vin = $_POST['chassis'] ?? '';

// API
$url = "https://www.xxx.com/api/tracking?chassis=" . urlencode($vin);

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$response = curl_exec($ch);
curl_close($ch);

$data = json_decode($response, true);

// Debug
// echo "<pre>"; print_r($data); echo "</pre>"; return;

if(!empty($data)){
    $this->data['POL']        = $data['POL'];
    $this->data['POD']        = $data['POD'];
    $this->data['vessel']     = $data['vessel'];
    $this->data['voyage']     = $data['voyage'];
    $this->data['ETS']        = $data['ETS'];
    $this->data['ETA']        = $data['ETA'];
    $this->data['status']     = $data['status'];
    $this->data['brand']      = $data['brand'];
    $this->data['model']      = $data['model'];
    $this->data['local_agent'] = $data['local_agent'];
}

return;
Max_admin 17h ago

Hi comfix

You have this code inside a PHP action at the TOP of the Load event of your form page ?

Can you post a screenshot of the page actions and the debug ?

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
comfix 17h ago

No it is at the submit button action, because 1st need to put in the VIN number so you get results returned back.

Alle fields are created and have the correct naming/value.

Max_admin 17h ago
Answer
1 Likes

if you want to redisplay the form after the Submit then you need to use the "Reload page" action:

Populate fields from API data image 1

is this what you need ?

Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
comfix 16h ago

This seems to solve the issue and the fields are populated that is great! thanks )

Another question: when closing the page and opening it again, the data stays filled in. How to start with empty fields? So no caching or session saving whatsoever. Also would like to register how many time it has been used.

Max_admin 15h ago

Great! 😊

At the top of the Load event, you can add a PHP action and use this code:

$this->data = [];
$this->vars = [];

that should clear the data for a new try, but you need to use if statement to check there was no POST:

if(empty($_POST)){
$this->data = [];
$this->vars = [];
}
Max, ChronoForms developer
ChronoMyAdmin: Database administration within Joomla, no phpMyAdmin needed.
ChronoMails simplifies Joomla email: newsletters, logging, and custom templates.
You need to login to be able to post a reply.