Well, not actually - more a "how to" for those of you looking to use Facebook's fb:dialog FBML tag.
As usual, the documentation is not perfectly clear. It is, in this case however, actually pretty much spot on if you read it through properly - something I, of course, didn't do at first.
The documentation states that the fb:dialog tag is in beta. It's been in beta for a long time as I first used it in the "Beach Fit in 30 Days" application back in January (post) to show measuring chart information in the diary:

That was, however, a simple use and involved only showing an information popup with a close button.
What about more complex dialogs? How would you use what Facebook call "Mock AJAX" to take the contents of a form in your open dialog, send them to your server and then display a response from the server in the (still open) dialog?
How about then going a step further and redirecting to another page when the user closes the dialog?
That's what I'm going to show you here.
The example I'll go through is one of showing a dialog to obtain name and email address from an application user (remember that you can't, however, store this information in a database - it's against the Facebook TOS - although you can use it transiently), calling a server script to do something with the information and then returning a success or failure response. Upon close of the dialog the user will be redirected.
Firstly, as usual, I'm going to assume you've set up an application and are familiar with the basic principles of Facebook API coding. The example I'll go through is for PHP. If you use a different programming language then I'll leave it to you how you translate what's written here ![]()
So. First step: Set up the dialog form and the fb:dialog tag:
PHP:
<fb:dialog id="my_dialog"> | |
<fb:dialog-title>Email register</fb:dialog-title> | |
<fb:dialog-content> | |
<form id="dialog_form" method="post"> | |
<table width="300" cellspacing="1" cellpadding="4" border="0" align="center"> | |
<tr> | |
<td>Name:</td> | |
<td><input type="text" name="name" size="40"></td> | |
</tr> | |
<tr> | |
<td>Email:</td> | |
<td><input type="text" name="email" size="40"></td> | |
</tr> | |
</table> | |
</form> | |
<fb:dialog-button type="submit" value="send" clickrewriteurl="<path to your server (not the apps.facebook.com one!>/dialog_response.php" clickrewriteid="my_dialog" clickrewriteform="dialog_form" /> | |
</fb:dialog-content> | |
<fb:dialog-button type="button" value="cancel" close_dialog="1" /> | |
</fb:dialog> |
This is pretty close to the sample they give you in the documentation, just with my form added. Note that the path to the script you call is a path to your own server, not to your Facebook app!
Save this as your initial canvas page - in this case, test_dialog.php.
Next, code up the server script to do it's thing:
PHP:
require_once('config.php'); | |
| |
if (isset($_POST['name']) && isset($_POST['email'])) { | |
// do whatever you need to do here... | |
echo('<fb:dialogresponse> <fb:dialog-title>Email register</fb:dialog-title> <fb:dialog-content>Registered! Thank you.</fb:dialog-content> <fb:dialog-button type="button" value="close" close_dialog=1 href="'.$appurl.'thanks.php"/> </fb:dialogresponse>'); | |
| |
} else { | |
// do some error stuff here... | |
echo('<fb:dialogresponse> <fb:dialog-title>Email register</fb:dialog-title> <fb:dialog-content>You did not enter both your name AND your email!</fb:dialog-content> <fb:dialog-button type="button" value="close" close_dialog=1 /> </fb:dialogresponse>'); | |
| |
} |
Right. The first line ensures that my Facebook app configuration is loaded so that I have access to all variables I need and can also instantiate the Facebook class if needed here. $appurl will contain the path to my application in Facebook - i.e. the apps.facebook.com one - with a trailing "/".
Save this as dialog_response.php (as per the clickrewriteurl attribute of the submit fb:dialog-button on your form).
The form parameters will be POSTed to the script and can be retrieved and used here. If all is fine, return the expected format to Facebook as above. If you omit the fb:dialogresponse, fb:dialog-title or fb:dialog-content tags your output in the dialog box will look pretty sparse - probably just plain text surrounded by the grey border. Not what we need.
Sending back the content correctly formatted will ensure that your users see what you expect them to see, all nicely presented in the "same" dialog.
Note the line:
PHP:
<fb:dialog-button type="button" value="close" close_dialog=1 href="'.$appurl.'thanks.php"/> |
This is important as it's the line of code that will render the close button on the dialog response shown and ultimately ensure that the user is redirected to the page you want - in this case, thanks.php.
In the error processing I'm going to leave the user where they were.
The last thing we need is to open the dialog. Bear in mind that what we're working with here is the FBML tag and not FBJS or javascript. We therefore need a link of some sort.
For the sake of simplicity we'll add a link to the test_dialog.php canvas page to open the dialog:
Code:
<a href="#" clicktoshowdialog="my_dialog">Register here...</a> |
Now, assuming all is coded up nicely, your user will see the link when he/she visits your canvas page. Clicking the link will open the dialog:

Clicking "send" will call the server script which, in turn, will do it's thing and return the nicely-formatted response. The response is shown in the dialog:

Closing the dialog by means of the "close" button (there isn't another way other than reloading the page) will cause the user to be redirected to the thanks.php page:

That's it!
You can use this approach for adding extra security to "log in only" areas of your application or to provide any other interactivity you need. Dialogs opened with fb:dialog will also hold SWFs, Images or other forms of content BUT be aware that the width of the dialog seems to be fixed at about 465 pixels.
Have fun with your dialog(ue)s!
Robert











Recent comments