JotForm User Guide / Advanced Features /

Webhook PHP Script Examples

Webhook PHP Script Examples

If you're already familiar with JotForm Webhook, here are example scripts that you may want to try.

Note: Make sure to update the field names on these scripts with the field names from your form.

Instant Email Notification - Get Instant Email Notification Using Your Email Sending Server

<?php
//convert json data to php
$result = $_REQUEST['rawRequest'];
$obj = json_decode($result, true);

//Change with your emails
$emailfrom = "john@example.com"; //Sender or From Email
$emailto = "paul@example.com"; //Recipient, you can predefine or use a field value e.g. $obj['q4_email']
$subject = "You've got a new submission"; //Email Subject Title

//Do not edit
$id = $_POST['submissionID']; //Get submission ID
$submissionURL = 'https://www.jotform.com/submission/'.$id; //Construct submission URL

$headers = "From: " . $emailfrom . "\r\n";
$headers .= "Reply-To: ". $emailfrom . "\r\n"; //Optional
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=utf-8\r\n";

//New method, get data from the submissions page
$html = new DOMDocument;
$html->loadHTML(file_get_contents($submissionURL));
$body = $html->getElementsByTagName('body')->item(0);
//get html code after the body tag
foreach ($body->childNodes as $child){
    $html->appendChild($html->importNode($child, true));
}
//make the table responsive so it appears nicely on email
$body = $html->getElementsByTagName('table');
foreach ($body as $width) {
    $width->setAttribute('width', '100%');
}
$body = $html->saveHTML();

//Send email
@mail($emailto, $subject, $body, $headers);
?>

SMS Notification - Send SMS After Form Submission Using Your SMS Provider's API

<?php
//Catch form field values
$result = $_REQUEST['rawRequest'];
$obj = json_decode($result, true);

//Replace your authentication key & credentials
$authKey = "YourAuthKey";
$senderId = "102234";
$route = "default";

//Replace your form field names
$mobileNumber = $obj['q1_mobileNo']; //mobile no. from form data
$message = urlencode($obj['q2_message']); //message from form data

//Prepare you post parameters
$postData = array(
'authkey' => $authKey,
'mobiles' => $mobileNumber,
'message' => $message,
'sender' => $senderId,
'route' => $route
);

//Replace your API endpoint
$url="http://mysmsapiproviders.com/sendhttp.php";

// init the resource
$ch = curl_init();
curl_setopt_array($ch, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_POSTFIELDS => $postData
//,CURLOPT_FOLLOWLOCATION => true
));

//Ignore SSL certificate verification
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

//get response
$output = curl_exec($ch);

//Print error if any
if(curl_errno($ch)){
    echo 'error:' . curl_error($ch);
}

curl_close($ch);
echo $output;
?>

Code: http://pastebin.com/embed_iframe/0A558v0y

Form To MySQL - Send Form Data To Your MySQL Database

<?php
//Replace with your DB Details
$servername = "localhost";
$username = "YOUR_USERNAME_HERE";
$password = "YOUR_PASSWORD_HERE";
$dbname = "YOUR_DBNAME_HERE";
$dbtable = "YOUR_DBTABLE_HERE";

//Create connection
$mysqli = new mysqli($servername, $username, $password, $dbname);

//Check connection
if ($mysqli->connect_error) {
    die("Connection failed: " . $mysqli->connect_error);
}

//Get field values from the form
//Get unique submissionID - nothing to change here
$sid = $mysqli->real_escape_string($_REQUEST['submissionID']);

//Get form field values and decode - nothing to change here
$fieldvalues = $_REQUEST['rawRequest'];
$obj = json_decode($fieldvalues, true);

//Replace the field names from your form here
$fname = $mysqli->real_escape_string($obj['q15_yourName'][first]);
$lname = $mysqli->real_escape_string($obj['q15_yourName'][last]);
$email = $mysqli->real_escape_string($obj['q16_yourEmail16']);
$message = $mysqli->real_escape_string($obj['q17_yourMessage']);

$result = $mysqli->query("SELECT * FROM $dbtable WHERE sid = '$sid'");

//If submission ID exist, update record
if ($result->num_rows > 0) {
    $result = $mysqli->query("UPDATE $dbtable SET firstname = '$fname', lastname = '$lname', email = '$email', message = '$message' WHERE sid = '$sid'");
    echo "Existing Record Updated!";
}
//If new submission, insert record
else{
    $result = $mysqli->query("INSERT IGNORE INTO $dbtable (sid, firstname, lastname, email, message) VALUES ('$sid', '$fname', '$lname','$email','$message')");
    echo "New Record Added!";
    if ($result === false) {echo "SQL error:".$mysqli->error;}
}

$mysqli->close();
?>

Bounce Notifier - Get Notified When Submission Failed To Be Sent Into Your Recipient Email

The following script will utilize your default web host SMTP to send emails. So make sure your web host supports this, otherwise, the mail function won't work.

<?php
include "JotForm.php";

$jotformAPI = new JotForm("YOUR_JOTFORM_API_KEY_HERE");    // Put your API Key here
$history = $jotformAPI->getHistory("emails", "lastWeek");
$subid = $_REQUEST['submissionID'];
$submissionURL = 'https://www.jotform.com/submission/'.$subid; //Construct submission URL

$limit= 1; //send the last failed notification - increase the number if you have multiple notifications

$results = array();

//Your emails here
$to = "johndoe@example.com"; //Replace with your recipient email
$from = "paulsmith@example.com"; //Replace with your FROM email

//change the subject title if you like
$subject = "View submission failed to be sent";

//Don't edit
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: <'.$from.'>'. "\r\n";

//Additional body message here (optional)
$msg1 = "This submission has failed to be sent into your email.";

//New method, get data from the submissions page
$html = new DOMDocument;
$html->loadHTML(file_get_contents($submissionURL));
$body = $html->getElementsByTagName('body')->item(0);
//get html code after the body tag
foreach ($body->childNodes as $child){
    $html->appendChild($html->importNode($child, true));
}
//make the table responsive so it appears nicely on email
$body = $html->getElementsByTagName('table');
foreach ($body as $width) {
    $width->setAttribute('width', '100%');
}

//don't edit anything from here unless you know what you're doing
foreach ($history as $key=>$value) {        
    if ($value['status'] == "IN BOUNCE/BLOCK LIST" || $value['status'] == "FAILED" && $value['submissionID'] == $subid){
        if ($key == $limit-1) {
            $body =    $msg1. "<br>";
            $body .= $html->saveHTML();                
            @mail($to, $subject, $body, $headers);
        }
    }
}
?>

Bounce Notifier

We will post more examples from time to time. If you have any suggestions or if you need help with Webhook, feel free to post it below.

Contact Support:
Our customer support team is available 24/7 and our average response time is between one to two hours.
Our team can be contacted via:
Contact JotForm Support: https://www.jotform.com/contact/

Send Comment

26 Comments...

  • ghumanz

    Hello
    I have put this script at my endpoint



    and have configured the webhook as described in
    https://www.jotform.com/help/245-how-to-setup-a-webhook-with-jotform

    but it does not seem to work. However, when I post a simple php mailer script on the same webhook endpoint, i receive an email

  • lloyd.9708

    Hello

    I want Parents should receive SMS Alerts after submitting Online admission form by using webhook with textlocal.

    Please explain step by step, Thank you in advance.

  • Ahuja_Rachit

    is it possible to sent a notification through a custom bot ion discord

  • bilingualmindacademy

    Hello is it possible to put this form as a page on my website?

  • rahulmeethbrother

    I want to receive data in python script API instead of php, How can I?

  • clrobinson1965

    I found the dropbox intergration.

  • clrobinson1965

    How can I receive submission into my Dropbox account?

  • developer_independencebrothers

    Why is it not hitting endpoint created with google script web app?

  • hjian6957

    Is it possible to customize the jotform?

  • ACSIntake

    Hello,

    I am trying to use a webhook to have a submission received to an email address in plain text. The reason for the plain text is because then the email is connected with our database and only accepts plain text. I have followed the instructions on a webhook but have not any clue how to proceed.

    Please advise, thank you

  • Glenn

    I was trying to figure out how to send submissions to my MySQL database via webhook, and I finally got it working with your code. Thank you, thank you, thank you!

  • lplatz

    Referencing "Form to MySQL - Send form data to your MySQL Database". I'd like to post the data from my form to more that one table. Is that possible?

  • mackayc

    I can get the SID to write to my DB but can't get any other values.

    Perhaps I'm using the wrong field names????

    This is what I have in my php file:
    $name = $mysqli->real_escape_string($obj['name']);
    $email = $mysqli->real_escape_string($obj['emailaddress']);
    $store = $mysqli->real_escape_string($obj['store']);

    This is what my post looks like after I submit:
    Array
    (
    [submission_id] => 443529909937xxxxxxx
    [formID] => 9230547xxxxxx
    [ip] => x.x.x.x (I removed the ip for this post)
    [emailaddress] => mackayc@temthrift.com
    [store] => Antioch
    [name] => mackay

  • artskill

    Thanks, This is working great for me so far.
    Art

  • ashishmalviya73

    Not able to integrate my jotform with webhooks. please guide.

  • hardikmansaraa

    I setup webhook url in my account.
    I am getting event in webhook after form submit. But not getting form submitted detail in response.
    Can you share me your testing response data?

  • ATPHNB

    your test website is down - unable to see submissions

  • MerrillGardner

    Hello, I am trying to connect a Microsoft Flow to a Jotform form of mine, but the Flow creation is not triggering the webhook link connection. Are there any instances of webhooks not being able to connect to certain forms? Unsure why it isnt connecting properly.

  • nanaluz

    Is there a way to send this to a Netsuite account to create a lead record?

  • Sylvester007

    Hi,

    1) Is there a way of sending a copy of the form via email to 3-4 different parties if we know their email address in advance?

    2) Can you ask a respondent if they would like us to email them a copy of the form (if we get their email address)

    Thanks
    Sylvester

  • KathyZenios

    Can I send an update to a form in Asana from Jot form via webhooks?

  • konicki

    Can I post data collected on a form to another form? For example, I would like to collect data (travel authorization, air fare, lodging, meals, etc.) on form1 and then post it to form2 (budget approval) and form3 (department approval)?

  • Neel kamal

    testing..

  • edgarsm

    When using the SMS sample script in my php file.

    When replacing the info on the script, what do I use when it asks
    "
    //Replace your authentication key & credentials
    $authKey = "YourAuthKey";
    $senderId = "102234";
    $route = "default";

    "

    Where do I come up with this information?

  • malik112

    How to send post data using webhook?

  • Atbam

    Seems Good will try it