Forums

Data from JSON

palacietes 19 Jun, 2015
Hi, I'm newbie on chronoforms and i want to get data from a url with a result like {"Number":"1020","Description":"SOCKS"}

I want to get "Description" data and put the value in a text box.

is it possible? I'm trying with

<?php
$result = //my url;
$table = json_decode($result, true);

but nothing works

can anyone help me??

Thanks
GreyHead 19 Jun, 2015
Hi palacietes,

Please add a Custom Code action in the On Load event (if the data is in the page URL) and add code like this
<?php
$form->data['json'] = json_decode($form->data['param_name'], true);
?>
then the decription should be in $form->data['json']['Descriptiom']

It's not clear to me where the json data comes from though?

Bob
palacietes 22 Jun, 2015
Thank Bob for your reply. The data is in other server, i want to get it through JSON.
In the On Load event i have this code:
<?php
$result = http://server:8080/Api/Product/1020;
$table = json_decode($result, true);
?>


Now, how can i get this data in a text field?

I don't know where i must put this code to get field "Description":

$form->data['Description'], true;

Thanks
GreyHead 22 Jun, 2015
Hi palcietes,

I'm pretty sure that your code won't work - this line isn't good PHP
$result = http://server:8080/Api/Product/1020;
When you have got that working correctly we can see what needs to be done with the result.

You can add echo $result; to test what output you are getting.

Bob
palacietes 22 Jun, 2015
Hi, I forgot put the quotes, but it still doesn't work.

$result = "http://server:8080/Api/Product/1020";

If I try in a browser I get the string:

{"Number":"1020","Description":"SOCKS"}

I've tried demo-dynamic-dropdown sample with echo but I have the same result, the combo target doesn't get any data

Regards
GreyHead 22 Jun, 2015
Hi palacietes,

Then please try this version of the code I posted earlier:
<?php
$result = 'http://server:8080/Api/Product/1020';
$form->data['json'] = json_decode($result, true);
?>

Bob
palacietes 22 Jun, 2015
Hi, finally I get the data with this code in Form Code, outside of events:


<?php
$result = 'http://server:8080/Api/Product/1020';
$obj = json_decode(file_get_contents($result), true);
echo $obj['Description'];
?>


However, I don't know how to put the data in a text field when a button is pushed

Thanks again
GreyHead 22 Jun, 2015
Hi palacietes,

When what button gets pushed? Where is this button exactly and what needs to happen when it is clicked?

Bob
palacietes 22 Jun, 2015
When I push a form button I need to check the item number and put the item description in a text field. This button isn't a Submit button, Submit button is used to write to DB.

Regards
palacietes 22 Jun, 2015
I forgot to comment the item number is in a text field and when the button is pushed I check the number in the url to get its description
This topic is locked and no more replies can be posted.