2016-03-30 1 views
1

Ich habe eine Ajax Anfrage, die Daten aus meiner Datenbank, speziell zwei Spalten davon, company_id und people_id, Ich muss das Ergebnis, das aus dem Json-Ergebnis kommt, ich muss prüfen, ob eine bestimmte people_id in der existiert ErgebnisSo testen Sie das zurückgegebene JSON-Ergebnis?

hier ist meine Ajax-Request

Ext.Ajax.request({ 
    url: 'system/index.php', 
    method: 'POST', 
    params: { 
     class: 'CompanyPeople', 
     method: 'secget', 
     data: Ext.encode({ 
      people_id: Ext.getCmp('peopleGrid').selectedRecord.data.people_id 
     }) 
    }, 
    success: function (response) { 

    }, 
    failure: function() { 

    } 
}); 

ich dachte, ich in der Erfolgsfunktion dieses folgenden Code tun konnte, aber es hat nicht

if (data == 0) { 
    Ext.MessageBox.alert('Status', 'Person is not attached to anything bud!.'); 
} 
else { 
    Ext.MessageBox.alert('Status', 'Person already attached to another company.'); 
} 

arbeiten auch hier ist meine php

class CompanyPeople extends Connect { 

    function __construct() { 
     parent::__construct(); 
    } 

    public function secget($vars) { 
     $sql = "SELECT * FROM CompanyPeople where people_id={$vars->data->people_id}"; 

     $data = array(); 

     $total = 0; 
     if ($result = $vars->db->query($sql)) { 
      while ($row = $result->fetch_assoc()) { 
       $data[] = array("people_id" => intval($row["people_id"])); 
       $total++; 
      } 
      $result->free(); 
     } 

     echo json_encode(array("success" => true, "total" => $total, "topics" => $data)); 
    } 
} 
+0

Ich bin nicht ganz sicher über das, was für die Festsetzung meiner Code-Formatierung in dem if-Anweisung –

+0

dank zu setzen, ich weiß, es war schrecklich –

Antwort

1

Ich denke, so etwas wie dies funktionieren soll:

success: function(response){ 
    if(response.success === true) { 
     var personExist = false; 

     for(var i = 0; i < response.topics.lenght; ++i) { 
      // Your specific people_id in myValue 
      if(response.topics[i]['people_id'] === myValue) { 
       personExist = true; 
       break; 
      } 
     } 
    } 
}, 
+0

Dank I werde das jetzt versuchen –