Facebook Contact Forms with AJAX and PHP

While working today, I was having an issue finding information on how to add a lead capture form on our Facebook Page. Luckily I stumbled across this amazing post! Submitting a Contact Form via AJAX From Your Facebook Page

However, this solution didn’t completely work out of the box so I had to pull some knowledge in from working on previous PHP Forms.

The Facebook Code

Both of these pieces of code should be placed on your Static HTML Page/Tab

The Form

1
2
3
4
5
6
7
8
9
10
11
12
<form action="http://YOURWEBSITE.com/form_submit.php" method="post">
     <p>
          <label for="name">Name:</label>
          <input type="text" name="name" id="name" />
     </p>
     <p>
          <label for="email">Email:</label>
          <input type="text" name="email" id="email" />
     </p>
     <button type="submit" onclick="return submitAjaxForm();">Submit</button>
     <p id="ajaxMessage"></p>
</form>

The AJAX

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<script><!--
function submitAjaxForm()
{
    // declare a new FBJS AJAX object
    var ajax = new Ajax();
    ajax.responseType = Ajax.FBML;
    // define a callback to handle the response from the server
    ajax.ondone = function(data)
    {
        document.getElementById('ajaxMessage').setInnerFBML(data);
    }
    // let the user know we're sending the data
    document.getElementById('ajaxMessage').setInnerXHTML('Submitting your information, please wait...');
    // collect field values
    var queryParams = { 'name' : document.getElementById('name').getValue(), 'email' : document.getElementById('email').getValue() };
    ajax.post('http://mywebsite.com/form_submit.php', queryParams);
    return false;
}
//--></script>

Please note that either of these can be edited to add or remove form fields. You just need to check to make sure the queryParams are updated in the Ajax as they are queried by the “getElementById” function

The PHP

I don’t know any other scripting languages at the moment and don’t have a desire to learn others at this point so everything else was done in PHP. The default PHP script can be found at the link above. This modified script will allow you to send an email using the PHP Mail function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
<?php
// Set Default Timezone to Indianapolis for Submission Date/Time
    date_default_timezone_set('America/Indianapolis');
// Get User Content from the Form
    $name = stripslashes($_POST['name']);
    $email = stripslashes($_POST['email']);
    $phone = stripslashes($_POST['phone']);
    $address = stripslashes($_POST['address']);
    $products = stripslashes($_POST['product']);
// Set Date for when Submission was Sent
    $date = date('F d, Y @ h:i a', time());

// Email headers and subject information
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; //HTML Content Type
    $headers .= 'From: ' . $name . '<' . $email . '>'; //Who's it from? The person that filled out the form!
        $subject = 'YOUR EMAIL SUBJECT';
    $recipient = 'You@YourDomain.com';
     
    $msg =          "<strong>Submission Info:</strong>" .
            "<br /><br />" .
            "<strong>Submitted: </strong>" . $date . "<br />" .
            "<strong>Name: </strong>" . $name . "<br />" .
            "<strong>Phone: </strong>" . $phone . "<br />" .
            "<strong>Email: </strong>" . $email . "<br />" .
            "<strong>Address: </strong>" . $address . "<br />" .
            "<strong>Product: </strong>" . $products ."<br />" .
            "<strong>How they Found Us: </strong>Facebook";

    //Mail $msg to the recipient on the form
      if (mail($recipient, $subject, $msg, $headers))
         {
            //Output a confirmation that the email has been sent
            echo "<p><strong>" . $name . " Your Entry has been submitted. </strong><p>";
        }
     else //Error Message if problems arise
     {
            echo "<p>Message failed to send.";
     }
?>

This is the code that I used. I plan on updating to counter an empty form. But haven’t had the time to do that as of yet. Let me know if you have any thoughts on how to improve a lead capture form on a Facebook page! Please note that I did NOT write this code and all credit should go to the original author George Huger and his post Submitting a Contact Form via AJAX From Your Facebook Page

PHP Code: First and Last Day of Month

As I was writing some code today, I wanted a quick way to display the end of the month Date and came across this little snippet on Justin-Cook.com. It’s a post from 2009, but it still does what I need it to!

1
2
3
4
5
6
function firstOfMonth() {
return date("m/d/Y", strtotime(date('m').'/01/'.date('Y').' 00:00:00'));
}
function lastOfMonth() {
return date("m/d/Y", strtotime('-1 second',strtotime('+1 month',strtotime(date('m').'/01/'.date('Y').' 00:00:00'))));
 }

The end date function goes to the NEXT month and subtracts a day to get the last day of the current month. Quite a nice piece of code if you have time sensitive offers like we do at Unique.

Orignal post: Get the first & last day of the month with PHP

Working Form Code for Unique

1
2
3
<?php
if ($_SERVER['REQUEST_METHOD'] != 'POST'){$me = $_SERVER['PHP_SELF'];
?>

User Form

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<form name="form1" id="form1" action="<?php echo $_SERVER['../../confirm/PHP_SELF']; ?>" method="post" style="padding:0 40px;">
<label>Your Name</label>
<input id="rep" name="rep" />
<br />
<label>Your Username</label> <input id="userName" name="userName" /> (@uniquehomesolutions will be added automatically)
<br />
<label>Prospect Name</label>
<input type="text" name="prospect" id="prospect" />
<br />
<label>Prospect Address</label>
<input type="text" name="address" id="address" />
<br />
<label>Prospect Phone</label>
<input type="text" name="pPhone" id="pPhone" />
<div id="form">
<br />
<label>Original Appointment Date</label>
<input type="text" name="appointment" id="appointment" />
<br />
<label>Time</label>
<select name="hour">
<option value="na" selected="selected"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<select name="min" id="min">
<option value="na" selected="selected"></option>
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>
<select name="tod">
<option value="na" selected="selected">Select One</option>
<option value="AM">AM</option>
<option value="PM">PM</option>
</select>
<br />
<label>Product</label>
<select name="product" id="product">
<option value="15" selected="selected">Select One</option>
<option value="Windows">Windows</option>
<option value="Doors">Doors</option>
<option value="Roofing">Roofing</option>
<option value="Waterproofing">Waterproofing</option>
<option value="Handyman">Handyman</option>
<option value="Soffit/Fascia">Soffit/Fascia</option>
<option value="Siding">Siding</option>
<option value="Bath">Bath</option>
<option value="Cabinets">Cabinets</option>
<option value="Insulation">Insulation</option>
<option value="Gutters">Gutters</option>
</select>
<br />
</div>
<label>Why did they Cancel</label>
<br />
<textarea cols="25" rows="5" id="cancel" name="cancel"></textarea>
<br />
<div id="form">
<label>Reset Appointment Date (Leave blank if none)</label>
<input type="text" name="rAppointment" id="rAppointment" />
<br />
<label>Time</label>
<select name="rHour">
<option value="na" selected="selected"></option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
<option value="5">5</option>
<option value="6">6</option>
<option value="7">7</option>
<option value="8">8</option>
<option value="9">9</option>
<option value="10">10</option>
<option value="11">11</option>
<option value="12">12</option>
</select>
<select name="rMin" id="rMin">
<option value="na" selected="selected"></option>
<option value="00">00</option>
<option value="15">15</option>
<option value="30">30</option>
<option value="45">45</option>
</select>
<select name="rtod">
<option value="na" selected="selected">Select One</option>
<option value="AM">AM</option>
<option value="PM">PM</option>
</select>
<br />
<label>If not reset Why</label>
<br />
<textarea rows="5" cols="25" name="whyNoReset"></textarea>
</div>
<br />
<label>Date and Time Taken</label>
<input type="text" name="timeTaken" id="timeTaken" />
<br />
<input type="submit" id="submit" value="Submit" style="margin:2px;" />
</form>

Send Email

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
} else {
error_reporting(0);
$headers  = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .= 'From: ' . stripslashes($_POST['rep']) . '<' . stripslashes($_POST['userName']) . '@uniquehomesolutions.org>';
$recipient = 'dwampler@uniquehomesolutions.org, ' . 'snewkirk@uniquehomesolutions.org, ' . 'rmonroe@uniquehomesolutions.org, ' . 'mdavidson@uniquehomesolutions.org, ' . stripslashes($_POST['userName']) . '@uniquehomesolutions.org'; #Email information
$subject = 'Appointment Cancellation';

$name = '<strong>Prospect Name:</strong> ' . stripslashes($_POST['prospect']) . '<br />';
$phone = '<strong>Prospect Phone:</strong> ' . stripslashes($_POST['pPhone']) . '<br />';
$address = '<strong>Address:</strong> ' . stripslashes($_POST['address']) . '<br />';
$currentAppt = '<strong>Current Appointment:</strong> ' . stripslashes($_POST['appointment']) . '@' .
stripslashes($_POST['hour']) . ':' .
stripslashes($_POST['min']) .' ' .
stripslashes($_POST['tod']) . '<br />';
$product = '<strong>Product:</strong> ' . stripslashes($_POST['product']) . '<br />';
$whyCancelled = stripslashes($_POST['cancel']) . '<br />';
$reset = '<strong>Reset Time:</strong> ' . stripslashes($_POST['rAppointment']) . '@' .
stripslashes($_POST['rHour']) . ':' .
stripslashes($_POST['rMin']) .' ' .
stripslashes($_POST['rTod']) . '<br />';
$whyNoReset = '<strong>Why no Reset:</strong> ' . stripslashes($_POST['whyNoReset']) . '<br />';
$rep = '<strong>Who took Cancel:</strong> ' . stripslashes($_POST['rep']) . '<br />';
$timeTaken = '<strong>Time Taken:</strong> ' . stripslashes($_POST['timeTaken']);
if (stripslashes($_POST['rAppointment']) == ''){
$reset2 = $whyNoReset;
}
else {
$reset2 = $reset;
}
$msg1 = $name . $currentAppt . $phone . $address . $product . $whyCancelled . $reset2 . $rep . $timeTaken;

$msg2 = '<html>
<title>Unique Home Solutions E-Newsletter</title>
<body>
<table>
<tr>
<td>'
. $msg1 . '</td>
</tr>
</table>
</body>
</html>'
;

#Mail $msg to the email inputed on the form
if (mail($recipient, $subject, $msg2, $headers))
{
echo "<p><strong>Message Sent</strong>: Below is what the email will look like. <a href='index.php'>Send Another</a></p>";
echo '<style>table{padding:0 25px;}</style>' . $msg2;
}
else #Error Message if problems arise
echo "<p>Message failed to send. Must have a valid email address. <a style='color:#00e; text-decoration:underline; cursor:pointer;' onclick='history.go(-1)'>Go back</a></p>";
}
?>