0

I am trying to use Ajax to insert two variables into MySQL when a user clicks the Facebook like button. Nothing is being put into the database table The code I'm using is below. Any ideas why nothing is being put into the database tables?

Thank in advance,

John

On file with Facebook Like button:

<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script >

FB.Event.subscribe('edge.create', function(response) {
   $.ajax({
   url: "fblike.php", // the url of your php script
   context: document.body,
   success: function(){
      // if you want something to be executed when a result comes back
                      }
         });
});

</script>

<?php


session_start();

$uid = $_SESSION['loginid'];

$submissionid = mysql_real_escape_string($_GET['submissionid']);
$_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>'; 

php?>

On fblikephp:

<?php




session_start();

$uid = $_SESSION['loginid'];
$submissionid = $_SESSION['submissionid'];


mysql_connect("host", "user", "pw") or die(mysql_error());
mysql_select_db("db") or die(mysql_error());


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






?>
5
  • are you trying to add javascript before your html tag? maybe you should put it inside. in the head or the body forexample Commented Jun 17, 2011 at 3:18
  • Thanks for the suggestion... I wasn't doing it, so I tried it... but it didn't solve the problem. Commented Jun 17, 2011 at 3:21
  • Instead of echoing out all that html and javascript just put a php end tag before it. It will make your code readable. Commented Jun 17, 2011 at 3:25
  • i can't give u an answer right away because obviously there are other problems in your script. so here what u can do. format the html properly <html><head/><body/></html> and so on. then add an alert to your success function to see if you are making a correct ajax call first. then i can give you more details depending on the results Commented Jun 17, 2011 at 3:27
  • I added proper HTML formatting, and still nothing is being added to database. Commented Jun 17, 2011 at 4:44

2 Answers 2

0

No offence, but I think you are completely mixing up Javascript and Server Side Script.

A basic intro to that concept is given in my reply here

But to answer your question, in my opinion you haven't set up your PHP sessions properly.

You may be triggering the creation of two sessions. One will overwrite the other if you don't manage them properly, and you will lose your session variable values.

Try echoing the session variables, and try using an HTTP debugger (like Charles) to find out which of your sessions is the LAST to be created.

Sign up to request clarification or add additional context in comments.

2 Comments

How am I creating two session variables?
I've never been able to track down why this happens actually, but I see it often when using session_start(); just on it's own. The way I found to do this was use if session_start($PHPSESSID); else session_start(); I also like to force where my session cookies are set using ini_set('session.cookie_domain','.yourdomaine.com'); but that's my preference, and then I can be sure that my session cookie is always being set where I want it to.
0

You're calling FB handlers BEFORE you load their script

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.