Jump to content

Using Ajax to Run a MySQL Query via PHP When Facebook Like Button is Clicked


ArizonaJohn

Recommended Posts

I would like to run a MySQL query each time a Facebook Like button is clicked on a page. I already know that

FB.Event.subscribe('edge.create', function(response) {}

is used to execute something when a Like button is clicked. My problem is that I don't know Javascript / AJAX. What simple Javascript/AJAX code that I can but in the curly brackets of the FB.event that will just run the MySQL query below? My understanding is that I might need to get some sort of JQuery library, which is fine.

 

Thanks in advance,

 

John

 

The relevant code on the page:

 

<script src="http://connect.facebook.net/en_US/all.js"></script>

<?php


session_start();

$uid = $_SESSION['loginid'];

$_SESSION['submissionid'] = $submissionid;

echo '<div id="fb-root"></div>';
echo "<script>
  window.fbAsyncInit = function() {
    FB.init({appId: 'your app id', status: true, cookie: true,
             xfbml: true});
  };
  (function() {
    var e = document.createElement('script'); e.async = true;
    e.src = document.location.protocol +
      '//connect.facebook.net/en_US/all.js';
    document.getElementById('fb-root').appendChild(e);
  }());
echo '</script>";

echo '<html xmlns="http://www.w3.org/1999/xhtml" xmlns:fb="http://www.facebook.com/2008/fbml">';

echo '<script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like href="" send="true" layout="button_count" width="450" show_faces="false" font="arial"></fb:like>';

 

The MySQL query that I would like to run each time a Like button is clicked:

 

mysql_query("INSERT INTO fblikes VALUES (NULL, '$submissionid', '$uid', NULL)");

Link to comment
Share on other sites

Javascript runs "clientside" in the user's browser.

 

PHP runs on a server. 

 

Right and test a php script that accepts the values you require for the mysql insert.  Be sure that you have error checking, and the same type of security you would have if this was going to be a form submission via post.  In other words, there is nothing magic about javascript that makes it more secure and less prone to tampering or malicious input.

 

Yes I'd suggest you use jquery.  This may look confusing to you as a php programmer, due to their use of $.  Forget that php uses $ for variables.  jQuery is constructed as one giant class named jQuery, and the $ is an alias for "jquery".  So when you see:

 

$.post("test.php", { name: "John", time: "2pm" } );

 

Keep in mind that this is the same as:

 

jQuery.post("test.php", { name: "John", time: "2pm" } );

 

Take a look at the examples on http://api.jquery.com/jQuery.post/  and see if you can figure out what you specifically need to do.  jQuery can also be of help with what you'll need to with the like button itself, as it will help you grab the button click and execute the wrapper function you'll need.

 

This should explain it very simply:  http://www.learningjquery.com/2008/03/working-with-events-part-1

 

As much as these api's hide complexity, there is a point at which you just have to bite the bullet and learn a modicum of javascript syntax and rules to pull this type of thing off typically. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.