Tuesday, October 22, 2013

PHP returns JSON object to jQuery ajax call

The PHP
if (mysql_num_rows($pquery) == 1) {
   $result = mysql_fetch_assoc($pquery);
   echo json_encode($result);

}
The jQuery
$('#c_search').submit(function(){
       data = ($(this).serialize());

  $.ajax({
      url: 'actions/get_company.php',
      type: 'POST',
      data: data,
      cache: false,
      success: function(text){
          alert (text);
          alert(text[1]); // not right!
          alert(text.company[0]);// not right!
      }

}).error(function(){
    alert('error');
})
return false;

})
The text I get back:
{"id":"167","company":"ZEN","street":"1 S Congress Ave","email":"zxyz@yahoo.com","state":"TX","zip":"78704","phone":"512-555-1212"}