What is a webhook?
In general terms, a webhook is simply a notification sent over the web, which is triggered automatically whenever a specific event occurs.
In this case, the event is a new typeform submission. Whenever a new typeform submission comes in, a notification containing the response data is immediately sent to your chosen destination: the webhook URL which you set in the configuration panel.
Typeform webhook notifications are sent via HTTP POST request, and the request body (containing the response data) is in JSON format.
Webhooks are an advanced feature intended for 49forms users who know how to handle them.
/*
* notify json structure *
------------------------------------------------
{
"event_id": 804,
"event_type": "form_response",
"survey_id": "7qsqqvoocp",
"submitted_at": 1680598901,
"answers": [
{
"field": {
"type": "textinput",
"title": "What is your name?"
},
"answer": "John"
},
{
"field": {
"type": "number",
"title": "How old are you?"
},
"answer": "28"
},
{
"field": {
"type": "email",
"title": "Email"
},
"answer": "[email protected]"
}
]
}
*/
$json_data_49forms = file_get_contents("php://input");
$data_49forms_Array = json_decode($json_data_49forms,true);
//mapping 49forms survey question -> db field
$mapping = array(
"What is your name?" => "name",
"How old are you?" => "age",
"Email" => "email"
);
$db_insert_data = array();
foreach($data_49forms_Array["answers"] as $response){
$survey_question = $response["field"]["title"];
if( $mapping[$survey_question] ){
$db_field = $mapping[$survey_question];
/*get answer*/
$db_insert_data[$db_field] = $response["answer"];
}
}
/* add submitted_at datetime */
$db_insert_data["submitted_at"] = date("Y-m-d H:i:s",$data_49forms_Array["submitted_at"]);
/* mysqli */
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$dbConnection = new mysqli('localhost', 'root', 'root', 'surveys');
$preparedStatement = $dbConnection->prepare('INSERT INTO my_first_form (name,age,email,submitted_at) VALUES (?,?,?,?)');
$preparedStatement->bind_param("siss",$db_insert_data["name"],$db_insert_data["age"],$db_insert_data["email"],$db_insert_data["submitted_at"]);
$preparedStatement->execute();