1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Capturing Json Data

Discussion in 'JavaScript' started by Tinker99, May 25, 2017.

  1. #1
    Hello - hope somebody can point me in the right direction.

    I am using Cognito Forms (which are excellent) and trying to capture the results of form submissions using a Json facility which they offer. For the life of me I cannot make it work and suspect that I never will!

    Each time a form is submitted Cognito they call a specific page on my site which they expect will grab the Json data.

    The relevant part of their instructions are as follows;

    "Configure your web server so that this script can receive an HTTP POST and insert the data into your database. For example, create a new script on your web server called, save_contact.php, so that when a POST is made to http://mysite.com/save_contact.php the script is executed."

    Firstly I don't use PHP so that a non-starter but given that they talk about HTTP POST then I assume that the POST will pass across values for named variables.

    If that is the case then I could grab these variables (if I knew what they were called) using my scripting language on the called page to identify the Json input form data and simply save that chunk to a file for later parsing and processing.

    Just thought I would ask here just in case anybody was doing something similar using JavaScript or who had any experience of the Json process.

    Apologies if some of this sounds clueless - I know what I want to do but just don't know how to do it!!!
     
    Tinker99, May 25, 2017 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    Your webserver will be using some sort of server side scripting language and the default with shared hosting is PHP although you could have ASP or Ruby.

    You can't save anything into your database without some sort of server side script to handle the call - so something has to catch that json and convert it into SQL and save it.

    Check with your hosting company and find out if you do actually have PHP and then we can help you get the database side of things working.
     
    sarahk, May 26, 2017 IP
  3. akhileshbc

    akhileshbc Active Member

    Messages:
    98
    Likes Received:
    1
    Best Answers:
    5
    Trophy Points:
    75
    #3
    Data submitted to your server via HTTP POST will be available in an associative array named $_POST[]
    So if you don't know the array key names (indexes), you can simply do a var_dump().

    Example:
    <?php
    var_dump( $_POST );
    ?>
    PHP:
    Once you know the key names, you can simply access the array elements like this:
    <?php
    $user_email = $_POST['email'];
    $user_fullname = $_POST['full_name'];
    
    //...etc
    ?>
    PHP:
    You can then insert this data into a database, or you could even compose a new email with all the data and send it to your personal/official inbox!

    Hope this helps!

    If you are still finding it difficult and is looking for someone to do the work for you, you can drop me a message. I will be able to do it(paid work) for you! My username is same as my Skype id.

    Hope this helps!
     
    akhileshbc, May 26, 2017 IP
  4. Tinker99

    Tinker99 Greenhorn

    Messages:
    3
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    21
    #4
    I really am grateful for your input.

    I use an ancient script language called MivaMia which evolved about the same time as the dinosaurs got wiped out - suited me all these years ago and never had the kick to update my rudimentary skills. It is pretty limited and probably moribund now so I suppose I will have to persuade the few remaining brain cells to start sparking again and learn this new fangled PHP thingie.

    I am supposed to be retired but a little form creation job done as a favour(!) for my golf club needs me to be able to capture the form's user input, store it and do some additional bits and pieces with it.

    If I can get the JSON data into a file using PHP then I will probably go back to my comfort zone thereafter and use MivaMia to unpick it and load a DBF database!

    Following your note I have so far managed to get a simple PHP script running on my server and do some rudimentary bits including writing to a file.

    Now that that seems to work I will start to fiddle about with JSON using your suggestions.

    And I thought my learning days were behind me!!

    Again, many thanks for your help.
     
    Tinker99, May 27, 2017 IP
  5. Tinker99

    Tinker99 Greenhorn

    Messages:
    3
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    21
    #5
    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.


    <?php 
        $myfile = fopen("json_data.txt", "w") or die("Unable to open file!");
    
        $json = file_get_contents("php://input"); 
        if (empty($json)) { 
            $txt= "No payload\n";
            fwrite($myfile, $txt);
               die; 
        } else {
            $txt= "Json not empty\n";
            fwrite($myfile, $txt);
        }
    
        $tours = json_decode($json,true); 
    
        if ($tours == null && json_last_error() !== JSON_ERROR_NONE) { 
            $txt= "Error reading JSON:\n";
         fwrite($myfile, $txt);
        } else {
            $txt= "Json read OK\n";
            fwrite($myfile, $txt);
        }
        fwrite($myfile, json_encode($tours,JSON_UNESCAPED_UNICODE));
        fclose($myfile);
    ?>
    PHP:
     
    Last edited by a moderator: Aug 3, 2017
    Tinker99, Aug 3, 2017 IP
    sarahk likes this.