Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to work with array returned from PHP script using AJAX?
    primarykey
    data
    text
    <p>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:</p> <p>PHP:</p> <pre><code>$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); </code></pre> <p>and JS:</p> <pre><code>$.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"); } } }); } } }); </code></pre> <p>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:</p> <pre><code> $('.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"); } }); </code></pre> <p>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???</p> <p>EDIT:</p> <p>This is what console.log(data['error_id_array']) gives back to me:</p> <pre><code>["2"] </code></pre> <p>EDIT2:</p> <p>Okey i will ask from other perspective. How do i access JSON object in form like this - console.log out put from data:</p> <pre><code>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 </code></pre> <p>I need to check array under error_id_array</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload