jQuery.inArray not working? - javascript

So I'm using an ajax call to retrieve a user's messages. Then cross referencing them with existing messages by creating an array of their id's. I have two messages that already exists with ID of 157 and 159. Both messages are being correctly listed in the array but when I use jQuery.inArray it only seems to be able to find one of them.
function checkForNewMessages() {
$CurrentMessages = [];
$('.Message').each(function () {
$CurrentMessages.push($(this).attr('message-id'));
});
console.log('Messages found');
console.log($CurrentMessages);
console.log('Getting Messages');
$.ajax({url: "<?php echo $GLOBALS['SubDirectory']; ?>api/get/messenger/messagesfromfeed?feed_id=" + urlget('ID'), success: function(result){
$Result = decodeURI_array(JSON.parse(result));
$.each($Result, function(index) {
if(!jQuery.inArray($Result[index]['ID'], $CurrentMessages)) {
console.log("New Message, ID of '" + $Result[index]['ID'] + "'");
} else {
console.log("Old Message, ID of '" + $Result[index]['ID'] + "'");
}
});
}});
}
console result
Messages found
["157", "159"]
Getting Messages
New Message, ID of '157'
Message, ID of '159'
URI decoded JSON
[{"ID":"157","CompanyID":"13","Receiver":"13","Sender":"","Message":"test line 1\r\n& test line 2\r\n\r\n","VirtualAttachments":"[{\"Type\":\"Client\",\"ID\":\"128\",\"DisplayName\":\"\"},{\"Type\":\"Quote\",\"ID\":\"20\",\"DisplayName\":\"\"}]","FileAttachments":"[{\"Name\":\"IMG_1300.JPG\",\"Location\":\"\"}]","FeedID":"23","Seen":"TRUE","CreatedOn":"1474635298"},{"ID":"159","CompanyID":"13","Receiver":"13","Sender":"15","Message":"reply test\r\n","VirtualAttachments":"[]","FileAttachments":"[]","FeedID":"23","Seen":"TRUE","CreatedOn":"1474635960"}]

Related

Facebook Api to get any user profile pic by giving the facebook user_id

I am taking "user_id" as input and passing to my Js function which suppose to give me the profile photo data.I am trying to show that photo.Logged in user profile picture is showing properly. But when I pass logged out user_id, getting error and it says "TypeError: response.picture is undefined". It is the first time, I am trying to use fb api to get user info only by user_id.Here is my function."a" is coming from my input.
// without login,just giving username and getting profile picture
function getInfopeople(a) {
//https://graph.facebook.com/user_id/picture /me/picture?redirect=false&height=300' //this part is not working
var user_id = "/"+a + "/picture";
console.log(user_id);
FB.api('/http://graph.facebook.com/user_id,','GET', {fields:'picture.width(150).height(150)'}, function(response) {
document.getElementById('user_status').innerHTML = "<img src='" + response.picture.data.url + "'>";
});
}
You were making incorrect API call. You should call this API to fetch the user profile picture.
function getInfopeople(user_id) {
// https://graph.facebook.com/{version}/{user_id}?fields=picture.width(150).height(150)
FB.api('/' + user_id, 'GET', { fields:'picture.width(150).height(150)' }, function(response) {
document.getElementById('user_status').innerHTML = "<img src='" + response.picture.data.url + "'>";
});
}

jTinder Save to Database

I hope it's ok to ask this here. I have searched everywhere, but can't find a solution.
I've found a nice js library called jTinder at https://github.com/do-web/jTinder
Now I am trying to save likes or dislikes in a mysql database and php. But soon I will give up! I've tried a lots of different code, but nothing really happens.
Mostly I crasch the script from working at all.
Can someone help me?
$("#tinderslide").jTinder({
// dislike callback
onDislike: function (item) {
// set the status text
$('#status').html('Dislike image ' + (item.index()+1));
},
// like callback
onLike: function (item) {
// set the status text
$('#status').html('Like image ' + (item.index()+1));
},
animationRevertSpeed: 200,
animationSpeed: 400,
threshold: 1,
likeSelector: '.like',
dislikeSelector: '.dislike'
});
getdata.php looks like this:
$link = mysqli_connect("127.0.0.1", "root", "", "vacation");
// Check connection
if($link === false){
die("ERROR: Could not connect. " . mysqli_connect_error());
}
$liked = mysqli_real_escape_string($link, $_POST['like']);
$sql = "INSERT INTO destinations (like) VALUES ('$liked')";
if(mysqli_query($link, $sql)){
echo "Records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($link);
Ajax:
$.ajax({
url: 'getdata.php',
dataType: 'json',
success: function(data)
{
var id = data[0];
var name = data[1];
var count = data[3];
$('#output').html('like('+id+')');
}
There a are many problem in this code so
In your html file where is data coming from in ajax while you are not returning anything in you php code. So your code sholud be somthing like the for test
$("#tinderslide").jTinder({
// dislike callback
onDislike: function (item) {
$.ajax({
url: 'getdata.php',
data: 'DATA_YOU_WANT_TO_SEND',
dataType: 'json',
success: function (data) {
console.log()
//$('#output').html('like(' + id + ')');
}
});
// set the status text
$('#status').html('Dislike image ' + (item.index()+1));
},
// like callback
onLike: function (item) {
// set the status text
$('#status').html('Like image ' + (item.index()+1));
},
animationRevertSpeed: 200,
animationSpeed: 400,
threshold: 1,
likeSelector: '.like',
dislikeSelector: '.dislike'
});
And in php code
You are inserting like into it how do you get a post data in the php file if you are not sending it from the ajax so put data to it.

Updating rows in parse with javascript

I'm trying to update a specific field in a row in my parse table when clicking a button. The button gets the object id, and it sets an alert with the object id just to make sure we get the right objectId, but the updating line doesn't seem to work. Any ideas?
button.onclick = function () {
var MyItems = Parse.Object.extend("MyItems");
var query = new Parse.Query(MyItems);
query.equalTo("objectId", this.id);
query.first({
success: function(object) {
alert(object.get('objectId'));
object.set("status", "ok");
object.save();
},
error: function(error) {
alert("Error: " + error.code + " " + error.message);
}
});
}
Solved!
ACl was not set to write.
Changed it to public write and everything works out fine.

How to get the facebook user id to another javascript file after login?

I understand that I can get the facebook id by doing:
FB.api('/me', function(response) {
alert('Your id is ' + response.id);
});
However, I want to have the user login and then grab that id in a different file so I can handle it. Right now I have:
var id = "";
var fburl = "http://graph.facebook.com/" + id + "?callback=?"
$(function(){
$("#fb-profile-picture")[0].src = "http://graph.facebook.com/" + id +"/picture";
$.getJSON(fburl, function(data){
console.log(data);
$("#name").append(data.name);
$("#user-id").append(data.id);
});
});
and if I manually enter the id in the id var it works however I'd like to be able to grab that response.idas the value and use it in this other javascript file but I haven't figured out how to.
Assuming you
cannot control which of the scripts is executed first
you are working in an environment with a global window object
part one is only executed once
you are using jQuery
making a global and firing an event might be a solution, although not considered best practice.
Script 1:
FB.api('/me', function(response) {
window.fbCustomId = response.id;
console.log(window.fbCustomId);
$(window).trigger('fbResponseLoaded');
});
Script 2:
function renderProfile() {
var fburl = "//graph.facebook.com/" + window.fbCustomId + "?callback=?"
$(function () {
$("#fb-profile-picture")[0].src = "//graph.facebook.com/" + window.fbCustomId + "/picture";
$.getJSON(fburl, function (data) {
console.log(data, window.fbCustomId);
$("#name").append( $('<span>').text(data.name) );
$("#user-id").append( $('<span>').text(data.id) );
});
});
}
if (window.fbCustomId) {
renderProfile();
} else {
$(window).on('fbResponseLoaded', renderProfile);
}

How to access result array returned in ajax?

Arg!! I had this working flawlessly and now I am back to banging head against the keyboard.
I want access defined columns inside the array, but I am getting undefined but if I display the results using an alert as detailed in snipped of code below I see the following:
[{"firstname":" Mr","0":" Mr","lastname":" Two","1":" Two","user_public_info_id":"3","2":"3","st_usage_id":null,"3":null},{"firstname":" Mr","0":" Mr","lastname":" Three","1":" Three","user_public_info_id":"5","2":"5","st_usage_id":null,"3":null}]
***
g
***
e
***
undefined
Here is the Ajax code:
$.ajax({
type: "POST",
url: "models/ajaxHandler.php",
data: "handler=getStActivitySharingList&stu_id="+stu_id,
datatype: "json",
success: function(result){
var count = 0;
if (result !== null)
{
//display results
alert(result + " <br />*** <br />" + result[0] +" <br />*** <br />" + result[1] + " <br />*** <br />" + result[0]["firstname"]);
//clears choice list
clearOptions();
//result = $.parseJSON(result); //if this is used cannot display result again
alert (result);
$.each(result, function (i, elem) {
alert("elem"+elem.st_usage_id ); //displays as undefined and won't break
if (elem.st_usage_id === null)
{
count++;
alert(elem.firstname + " "+ elem.lastname + " " + elem.user_public_info_id);
appendOption(elem);
}
});
}
alert(count);
if (count === 0){
noResultsAvailableOption();
}else{
resultsAvailableOption();
}
ShowDialog(false);
e.preventDefault();
},
error: function(){
alert("ajax failure: could not retrieve a list of contacts");
}
});
i not know how you return it from PHP, but in jquery try:
sucess: function (result)
{
console.log(JSON.parse(result)); // parse string to json
}
See json.org
To better answer this question is to implement better debugging procedures.
Here is the code that I used for debugging this issue. The breaking down of the xmlHttpRequest clearly displayed to me that issue was with the data and that I was encountering an illegal character exception when trying to encode the data to json.
A great way to solve any issue is to first implement the correct debugging procedures, and everything else will work itself out.
error: function(xmlHttpRequest, status, error){
alert("ajax failure: could not populate list of countires | " + status + " | error:" + error);
var xmlHttpRequestStr= "";
for(x in xmlHttpRequest)
xmlHttpRequestStr = xmlHttpRequestStr + xmlHttpRequest[x];
alert(xmlHttpRequest);
}

Categories

Resources