Forums

The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums Design Trapping and storing Json data

  • This topic is empty.
Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #257229
    Gadgee
    Participant

    It’s a long shot but I thought I would ask anyway…

    I am using Cognito’s excellent form builder and am now trying to take advantage of it’s Json facility whereby it transmits the values from a completed form to a given web address.

    Although I can see an ideal application which in my ideal world works something as follows;

    Users enter data through a Cognito form.

    The form (once submitted) ‘triggers a call’ to a special webpage on my site using a Json.

    Something on that receiving page understands Json and is able to extract the ‘payload’.

    That something then stores the raw Json data in a file on my site.

    I then use my archaic scripting language to parse that raw datafile and use the contents to update a DBF database.

    I have been struggling for weeks trying to find a way to make this dream work.

    All the solutions I have looked at assume the use of PHP / Sql etc. and some reasonable understanding of Javascript – none of which I have.

    It seems such a simple thing to be able to do that I thought I would take a chance and ask you folk as a last resort before looking for an alternative to the Cognito approach. I have studied their documentation at

    http://help.cognitoforms.com/webhooks

    and even tried to teach myself PHP, installed a local SQL facility but no matter what I tried I could not get hold of any data.

    Hope some of that made some sense!!

    #257241
    JeroenR
    Participant

    I would start with a simple empty PHP page that examines the POST data.

    <?php  
        $json = file_get_contents("php://input");  
    
        if (empty($json)) {  
            echo "No data payload";  
            die;  
        }
    
        $contact = json_decode($json);  
        if ($contact == null && json_last_error() !== JSON_ERROR_NONE) {  
            echo "Error reading JSON: " . json_last_error();  
        }
    
        var_dump($contact);
    ?>
    

    From there see what the contents really is and do with it what you like. For all things you describe should be a solution that you can easily find by googling the separate functions like file writing and storing data somewhere.

    If you get no result from the code above you can also just start with this:

    <?php print_r($_POST); ?>
    

    See if there is anything posted.

    Save that PHP page somewhere in your domain and enter the URL to that page in ‘Post JSON data to a website’.
    According to the documentation that should work, assuming your provider supports PHP.
    For checking that, you can use:

    <?php phpinfo(); ?>
    

    Save it as a .php file, upload it and navigate to it on your website.

    Did you try the request bin example by any chance? Because that should show you there is indeed posted data. Maybe that is easier to start with.

    #257282
    Gadgee
    Participant

    Many thanks for your interest.

    I used the requestbin facility as suggested and as far as I can see the ‘payload’ contains the test data which I input.

    https://requestb.in/z9gmi5z9?inspect

    I also checked that PHP is running on my server and did some Googling as regards file handling and came up with some additional code as follows;

    It seems that the Json call to the web page on my server is happening (triggered by the submission of the Cognitoform) and as a result of it opening the web page I can see that json_data.txt is being created but it is empty.

    I suspect I may be showing my lack of knowledge and am off at a tangent – on the other hand I could be quite close!

    If I am getting close then fine but please do not spend too much time on this. Although I am impressed with Cognito forms and would prefer to use them, there are alternatives which I suspect offer a better range of tools and options when it comes to accessing and handling the form submitted data.

    #257283
    JeroenR
    Participant

    Just google the thing that doesn’t work and see if your code corresponds with the offered examples. If you created the file with your PHP code, you have the needed rights to do that.
    Look here for an example: https://www.w3schools.com/php/php_file_create.asp

    #257288
    Gadgee
    Participant

    Thanks as ever..

    Have got my code looking like the examples you mention and submitted another form to trigger a Json call to the page.

    This time “Mickey Mouse” appears in the json_data.txt file but nothing else.

    Just to see what happens, I put the fopen statement at the head of the PHP block and inserted these lines to see if the error condition was written to file…

    if ($contact == null &amp;&amp; json_last_error() !== JSON_ERROR_NONE) {  
        $txt= "Error reading JSON:\n";
      fwrite($myfile, $txt);
    }
    

    The file only contains “Mickey Mouse” which suggests that there was no error condition encountered.
    #

    #257360
    Gadgee
    Participant

    Just in case there are others out there struggling as I was, I eventually got PHP to do what I need.

    Excuse the PHP coding quality – I have had to learn the basics over the past week or so…

    I can now accept and store incoming Json calls to this simple PHP web page and at the end of the call I now have a file with all the Json data in it which I can then parse to isolate the field names and the associated field values.

    There are probably neater and better ways but for the moment this suits me.

    Thanks for your help.

Viewing 6 posts - 1 through 6 (of 6 total)
  • The forum ‘Design’ is closed to new topics and replies.