I am using the following code to send a user id when the button with ID #share is pressed. But I don't know how to get data back, a variable, which when is true will run FB.ui.
<script type="text/javascript">
$(document).ready(function(){
$("#share").click(function(){
$.ajax({
type: 'POST',
url: 'upload.php',
data: 'user_id=$user_id',
success: // I don't know what to put here, maybe "function(result){ if (result == true) {"?
FB.ui({
method: 'feed',
name: '$app_name',
link: '$app_url',
picture: '$upload_picture',
caption: 'Aluxian Apps',
description: '$app_description'
});
});
});
});
</script>
Yes, you could json_encode the variable you want to send back to client.
echo json_encode( array('result' => true ) );
Then in your js
$(document).ready(function(){
$("#share").click(function(){
$.ajax({
type: 'POST',
url: 'upload.php',
dataType: 'json',
data: 'user_id=$user_id',
success: function( data ) {
if( data.result ) // you can access the result variable here
FB.ui({
method: 'feed',
name: '$app_name',
link: '$app_url',
picture: '$upload_picture',
caption: 'Aluxian Apps',
description: '$app_description'
});
}
});
});
});
<script type="text/javascript">
$(document).ready(function(){
$("#share").click(function(){
$.ajax({
type: 'POST',
url: 'upload.php',
data: 'user_id=$user_id',
success: function(data){
if (data.result == true) {
FB.ui({
method: 'feed',
name: '$app_name',
link: '$app_url',
picture: '$upload_picture',
caption: 'Aluxian Apps',
description: '$app_description'
});
}
});
});
});
</script>
Your assumption is correct.
$.ajax({
type: 'POST',
url: 'upload.php',
data: 'user_id=$user_id',
success: function(result) {
if (result == true) {
FB.ui({
method: 'feed',
name: '$app_name',
link: '$app_url',
picture: '$upload_picture',
caption: 'Aluxian Apps',
description: '$app_description'
});
}
}
});
Be sure to add a Content Type header to your PHP file so that it communicates the information back to the client correctly. At the top of your php file: header('Content type: application/json);