tanya tanya - 1 year ago 99
PHP Question

Share quiz results on Facebook

I have spent hours researching this but it seems to be tricky for many developers out there. I have a small php quiz, outputting results from a form in the following way:

if (maxA) {
echo '
<img src="imgs/result4.jpg"/>
<div class="results2">
<p class="title">You are a Bean</p>
<p class="details">Description</p>

The question is, how to add a Share button at the bottom of this, which will share the result on Facebook, together with the description and the picture. Note that there are four available results.

I have made a public app, and inserted the following in the head:

window.fbAsyncInit = function() {
appId : '1382290368762081',
xfbml : true,
version : 'v2.3'

(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));

And the HTML is yet to be figured out. I went back and forth with this answer: Using "share_open_graph" Facebook UI to create dynamic share dialog for quiz results - but did not actually achieve anything.I think it would be helpful to the whole community, if someone knows the exact way to make this happen, and to share them with us.

Answer Source

You can use facebook js SDK for this. you can call FB.ui method feed to achieve this. Create a button on your page

<input type="button" onclick="postToFeed()" value="Share" />

.Use the below function javscript to share on facebook.

function postToFeed() {
    // calling the API ...
    var obj = {
      method: 'feed',
      link: 'https://www.azeezkallayi.com/',
      description: "description goes here",
      picture: 'https://www.azeezkallayi.com/demo/test/womens-day.jpg',
      name: 'International womens day'       

Here you can change the values of parameters as you want.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download