for past few hours i have stack with this problem, i hope i can get a solution here.
What i am trying to do is something like this:
PHP:
$errorIds = array();
if(error hapens){
array_push($errorIds, $user['userId']);
}
$data['results'] = "success";
$data['message'] = "Your message have been sent successfully";
$data['error_id_array'] = $errorIds;
echo json_encode($data);
and JS:
$.ajax({
type: "POST",
url: HTTPS + '/lib/model/data/ctlSms.php?send=1',
data: $("#smsSendForm").serialize(),
dataType: 'json',
success: function(data){
$("textarea").removeClass("valid");
if(data['results']=="success"){
$('#smsSendForm')[0].reset();
$('.jtable').find("tbody tr").each(function(){
var firstCol = parseInt($(this).find("td:first").text());
var inArray = $.inArray(firstCol, data['error_id_array']);
if(inArray == -1){
$(this).css("background", "green");
} else {
$(this).css("background", "red");
}
});
} else {
console.log(data['message']);
$('.jtable').find("tbody tr").each(function(){
var firstCol = parseInt($(this).find("td:first").text());
var inArray = $.inArray(firstCol, data['error_id_array']);
if(firstCol == data['error_id']){
$(this).css("background", "red");
return false;
} else {
if(inArray == -1){
$(this).css("background", "green");
} else {
$(this).css("background", "red");
}
}
});
}
}
});
What i want to accomplish is that of there is a error in that array, jQuery checks if in array of user IDs is an ID that maches jTables IDs and if matches than collors background red else colors background green. And the problem is somehere here:
$('.jtable').find("tbody tr").each(function(){
var firstCol = parseInt($(this).find("td:first").text());
var inArray = $.inArray(firstCol, data['error_id_array']); /// here
if(inArray == -1){
$(this).css("background", "green");
} else {
$(this).css("background", "red");
}
});
I dont get possitive match even if i intentionaly create an error. Everything is colored green, back end code works great, but this frontend is frustrating me for past few hours. Maybe i am doind something wrong???
EDIT:
This is what console.log(data['error_id_array']) gives back to me:
["2"]
EDIT2:
Okey i will ask from other perspective. How do i access JSON object in form like this - console.log out put from data:
Object {results: "success", message: "Your message have been sent successfully", error_id_array: Array[1]}
error_id_array: Array[1]
0: "2"
length: 1
__proto__: Array[0]
message: "Your message have been sent successfully"
results: "success"
__proto__: Object
I need to check array under error_id_array
See Question&Answers more detail:
os