Skip to content

Instantly share code, notes, and snippets.

@unbounce
Created June 8, 2010 19:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save unbounce/430561 to your computer and use it in GitHub Desktop.
Save unbounce/430561 to your computer and use it in GitHub Desktop.
<?php
// This is a sample PHP script that demonstrates accepting a POST from the
// Unbounce form submission webhook, and then sending an email notification.
function stripslashes_deep($value) {
$value = is_array($value) ?
array_map('stripslashes_deep', $value) :
stripslashes($value);
return $value;
}
// First, grab the form data. Some things to note:
// 1. PHP replaces the '.' in 'data.json' with an underscore.
// 2. Your fields names will appear in the JSON data in all lower-case,
// with underscores for spaces.
// 3. We need to handle the case where PHP's 'magic_quotes_gpc' option
// is enabled and automatically escapes quotation marks.
if (get_magic_quotes_gpc()) {
$unescaped_post_data = stripslashes_deep($_POST);
} else {
$unescaped_post_data = $_POST;
}
$form_data = json_decode($unescaped_post_data['data_json']);
// If your form data has an 'Email Address' field, here's how you extract it:
$email_address = $form_data->email_address[0];
// Grab the remaining page data...
$page_id = $_POST['page_id'];
$page_url = $_POST['page_url'];
$variant = $_POST['variant'];
// Assemble the body of the email...
$message_body = <<<EOM
Email: $email_address \n
Page ID: $page_id \n
URL: $page_url \n
Variant: $variant \n
EOM;
mail('Your Email Address',
'New Unbounce Form Submission!',
$message_body);
?>
@pako69
Copy link

pako69 commented Jan 6, 2022

Hi

I want to write json data to a text file instead of sendingan email like your example but it do not work.

I tried this at line 32:

$date_submitted = $form_data->date_submitted[0];
$first_name = $form_data->first_name[0];
$last_name = $form_data->last_name[0];
$email = $form_data->email[0];
$phone_number = $form_data->phone_number[0];
$type_of_appointment = $form_data->type_of_appointment[0];
$reference = $form_data->reference[0];
$tpsfort = $form_data->tpsfort[0];

$myfile = fopen("myfile", "w") or die("Unable to open file!");
$data = $date_submitted."\t".$first_name ."\t"."\r\n";
fwrite($myfile, $data);
fclose($myfile);

My text file is empty...

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment