Facebook Cookbook - Jay Goldman [83]
If the user types a value into the field that doesn’t match any of the friends in the selector, name will be the string they entered and idname will be blank. If they enter no text at all, the idname field will not appear in the POST variables. This actually introduces the potential for a misleading form submission in which the user picks a valid user in the selector and then erases their entry or enters some text that doesn’t match. In the first case, you’ll get an empty string for name and the uid of the user they had selected in idname, and in the second case you’ll get whatever they type into name and the uid in idname. You shouldn’t accept the uid in either of those cases because they didn’t actually mean to submit it, so you should probably do a sanity check and make sure that name matches the name of idname, just to be safe:
if(isset($_POST['friend_name'])
&& isset($_POST['friend_uid'])
&& checkName($_POST['friend_name'], $_POST['friend_uid'])){
// They match, so do your thing in here
}
function checkName($friend_name, $friend_uid){
global $api_key, $secret;
$facebook = new Facebook($api_key, $secret);
$user = $facebook->require_login();
// Retrieve the user
$user_details = $facebook->api_client->users_getInfo($friend_uid, 'name');
if($friend_name == $user_details[0]['name']){
return true;
}else{
return false;
}
}
You Can Pick Your Friends (in Batches)
Problem
I need to give my users the ability to pick a bunch of friends at the same time. How can I give them a text field that will filter their friend list as they type?
Solution
Use the fb:multi-friend-input tag. The simplest form is:
It’s hard to get much simpler than that! This is a great example of FBML saving you a huge amount of work. That piece of code will give you a simple text box shown in Figure 6-47.
Figure 6-47. multi-friend-input box
When users put focus into the field, they’ll get some instructions, as shown in Figure 6-48.
Figure 6-48. multi-friend-input with instructions
As they start typing a name, Facebook will display a list of matching friends below the field (Figure 6-49).
Figure 6-49. multi-friend-input with prediction
So far, this looks a lot like the fb:friend-selector tag covered in You Can Pick Your Friends. The difference is in what happens after the user picks her first friend—in this case, she can keep adding more and more friends to the field, as you can see in Figure 6-50.
Figure 6-50. multi-friend-input with friends
Clicking on the “X” next to the name will, of course, remove it from the field, as will backspacing over a name.
Discussion
You can have only one of these per page. Well, you can add more, but they won’t do anything.
In the example shown earlier, the handle.php page will receive a parameter called ids, which is an array containing the Facebook IDs of each friend listed in the field. You can step through the members of that array quite easily:
$friends = (isset($_REQUEST["ids"])) ? $_REQUEST["ids"] : 0;
if( $friends != 0 ){
$counter = 0;
foreach($friends as $friend){
echo '
Friend ' . $counter . '\'s ID: ' . $friend . '
';$counter++;
}
}else{
echo "
You didn't pick anyone!
";}
Given the field shown earlier in Figure 6-50, this would output:
Friend 0’s ID: 512293981
Friend 1’s ID: 759805472
Friend 2’s ID: 505031822
Friend 3’s ID: 548340659
Friend 4’s ID: 725395385
There are a few optional parameters you can pass into the tag, which are listed in Table 6-17.
Table 6-17. Parameters for fb:multi-friend-input
Name
Type
Default value
Description
width
int
350px
The width of the field.