How to implement simple Webhook using php?

  • hlajotform
    Asked on July 25, 2017 at 11:37 PM

    I cannot, for the life of me setup a simple webhook that receives a simple notification saying "Submission Received" when a submission is received.

    I have followed every single tutorial JotForm has released on Webhooks. 

    This is my code:

     

     PHP Test

     

     

    include "includes/JotForm.php";

    $result = $_POST['rawRequest'];

    $obj = json_decode($result, true);  

    echo '

    ' . $obj . '

    ';

     

    ?> 

     

     

     

    I have added the URL that points to this PHP file (http://ftp.gptq.qld.edu.au/ezyval/testsite.php) as a webhook on a this test form (https://form.jotform.co/72001396205850). When I visit the testsite.php website that I created, I get an error that states: 

    PHP Notice: Undefined index: rawRequest in E:\FTP DATA\EzyVal\testsite.php on line 11

    Please help!? This is driving me nuts.

    Thanks

  • Charlie
    Replied on July 26, 2017 at 3:42 AM

    May we know what your PHP file actually does? Will it trigger an email or anything? Note that Webhook will not redirect the user to the .php file of yours. It will trigger the PHP file silently on the background once the form is submitted, the user then is directed to the "Thank You" page you have setup. 

    You can follow this guide and the sample Webhooks: https://www.jotform.com/help/245-How-to-Setup-a-Webhook-with-JotForm

    Try first getting the correct POST parameter names. Try it like this:

    $result = $_REQUEST['rawRequest'];

    $obj = json_decode($result, true);  

    echo '<p>' . $obj . '</p>';

     

    If that doesn't work. Try the actual PHP variables mentioned in this guide: https://www.jotform.com/help/51-How-to-Post-Submission-Data-to-Thank-You-Page. That guide will allow you to see your PHP variables. Take note of them and apply them in your Webhook instead, see if that will work.

  • hlajotform
    Replied on July 26, 2017 at 9:33 PM

    Eventually, I would like the PHP file to trigger another method that pulls the form data and creates a graph from it. I want to make a poll-like website, so I need real-time triggers when a user submits the form.

    I have followed the fist link in every possible way with no luck, unfortunately. 

    When I change my code to the snippet you sent I get the same exact error as before ("Undefined index: rawRequest").

    I followed the second URL you sent, and I used the submission_id variable to test my PHP. I simply replaced this line of code: $result = $_REQUEST['rawRequest']; with this line: $result = $_POST['submission_id'];. I also tried using REQUEST instead of POST.

    I recevie virtually the same error: PHP Notice: Undefined index: submission_id

    Thanks for helping.

  • liyam
    Replied on July 27, 2017 at 6:05 AM

    Most likely, there is no element rawRequest in the REQUEST var when you tried to assign it to $result, this is why you are getting the error Undefined Index: rawRequest.

    To be able to debug this, can you just capture the raw data without any element or variables in it? 

    Example: $result = $_REQUEST;

    This way, when you print $result (print_r($result);), it will show all the data in the array that is available in it and find out which elements are ok to get.

    If you need further assistance, please let us know.

  • hlajotform
    Replied on July 27, 2017 at 7:22 PM

    Hi Liyam,

    I implemented your suggestion, my entire code now is simply these two lines:

    $result = $_REQUEST;
    print_r($result);

    The result I get when I refresh the page is: Array().

    I have a feeling it has something to do with the data not being sent to my testsite.php when a response is submitted. Is my testsite.php supposed to update and re-pull the REQUEST in real time as a response is submitted?? (And yes, I have added the URL to my testsite.php website to the form's webhook integration).

    I figured something out that might help you to understand the issue more. I went into form settings and set the Send POST data to Thank You page to ON. Then I went to the Thank You page and changed it so it Redirected to my testsite.php website. Now when I submit a form, the result I get is this:

    Array ( [submission_id] => 3770065662028278367 [formID] => 72001396205850 [ip] => 119.161.***.*** [topic1] => aawdfq fads )

    So it seems the two lines of code to request the data IS working, however, the Webhook part isn't working, in the sense that my website doesn't get notified when data is submitted.

     

    Thanks for the help!

     

  • liyam
    Replied on July 28, 2017 at 3:48 AM

    Sorry I missed explaining the part where the webhook runs behind the process. So in order for you to be able to see how the webhook works, you will need to be able to save the data that is printed. Perhaps if you are processing an insertion to the database, you will be able to see that the data is inserted, provided you have the correct script to do the insert.

    So I would suggest inserting this:

    $results = print_r($result, true);
    file_put_contents('filename.txt', print_r($b, true));

    Then ensure that your filename.txt or any text is writable and can be opened by your php file. This way, processes on your webhook will be on the file name.

    Note, you can also make use of RequestBin (http://requestb.in/) to check posts sent via webhook. 

    Also, the Send POST data to Thank you page is for redirecting your thank you page to a custom thank you page that has PHP enabled like the one you just did. For webhooks, there is no need to use that.

    May I also suggest to check out this help guide to know more on how to make use of webhooks on your form: https://www.jotform.com/help/245-How-to-Setup-a-Webhook-with-JotForm 

    In any case that you should need further assistance, please let us know. :)

     

     

  • hlajotform
    Replied on July 30, 2017 at 9:30 PM

    Hi Liyam,

    Okay so we made progress, good stuff! Now when I submit a response it saves that response to my txt file, and I can see it on the server.

    I now have another question; is there a way I can use this webhook as a trigger to trigger off Javascript Methods?

    The idea is that I want to save these responses to my Firebase Database (which uses Javascript to interact with my site). So as the webhook receives a response I want to use javascript to push that response to my Database.

    The code I have tried (and it does not work) is:

    <script>

    function pushData(data) {

    return firebase.database().ref('TEST').push(data);  

    }

    </script> 

     

    <?php

    $result = $_REQUEST[rawRequest];

    echo "<script>pushData(" . $result . ");</script>";

    ?>

     

    Any ideas?

    Thanks so much for the help!

     

     

  • liyam
    Replied on July 31, 2017 at 12:47 AM

    Yes, that is possible. You just need to assign your PHP result to a javascript variable by printing or using echo, and then use that variable for processing your javascript code. Conceptually, this is how AJAX is done.

    On your code, I think you just need to strike out [rawRequest] to get the array to use it for your javascript code.

    Let us know if you need further assistance. :)

  • hlajotform
    Replied on July 31, 2017 at 2:44 AM

    Nothing seems to get triggered when I submit a new response. I have tried the following variations of code and none of them work.

    Attempt 1:

    <script>

    function pushData(data) {

    return firebase.database().ref('TEST').push(data);  

    }

    </script> 

    <?php

    $result = $_REQUEST;

    echo "<script>pushData(" . $result . ");</script>";

    ?>

     

    Attempt 2:

    </script> 

    <?php

    $result = $_REQUEST;

    echo "<script>alert('hello')</script>";    // Just to see if it gets run when submitted (and it doesn't)

    ?>

    Attempt 3:

    function pushData(data) {

    console.log(<?php $result = $_REQUEST; echo $result; ?>);

    }

     

    I don't know how to trigger a javascript method...

  • Charlie
    Replied on July 31, 2017 at 4:08 AM

    Hi,

    I'm not really sure why you are using "echo" or console.log in Webhooks. It will not update your PHP site with that as far as I know. This is because they are on separate pages and there's no redirect. 

    I'm not sure if this is what you are expecting:

    Windows Tab 1: Your JotForm form with Webhook integration.

    Windows Tab 2: Your website link http://ftp.gptq.qld.edu.au/ezyval/testsite.php 

     

    Now if you fill out your Form and submitted it, are you expecting windows tab 2 to suddenly trigger "echo" or "console.log" and update based on the submission made on windows tab 1? If that is the case, I believe that is not how it works, unless of course you will first save the submission data directly on your own database, but that also means that windows tab 2 needs to reload or refresh to get an updated list of data from your database. Unless your website page auto refreshes itself. 

    You are also mixing PHP and JS on your Webhook. I am not really sure how that will be handled. If you can push the submission data to your database by just using PHP, then that would the recommended way. Here's a guide that walks you through on how you can save your data to your database. This is also using the POST method so Webhooks should still work here, although this guide is more focused on MySQL: https://www.jotform.com/help/126-How-to-send-Submissions-to-Your-MySQL-Database-Using-PHP

    I see that Firebase has a PHP SDK, so you can just write it in full PHP to save your data in your Firebase database: https://firebase-php.readthedocs.io/en/1.x/writing-data.html

    I also suggest that you separate your website pages.

    Example:

    http://ftp.gptq.qld.edu.au/ezyval/save-to-database.php => direct link that will be entered in Webhooks that will exclusively handle save the POST data to your database. 

    http://ftp.gptq.qld.edu.au/ezyval/show-data-to-users.html => direct link that will display data fetched from your Firebase database using your preferred method, either JS, PHP or any other. 

     

    I hope that helps. If I misunderstood your concern, please let us know.