如何从AJAX返回数组作为JSON并读取结果

时间:2015-12-06 03:21:43

标签: php jquery arrays json ajax

我再次需要帮助。我试图从JSON中的AJAX返回一个表的多行,并将这些值放在文本框中。这是我的代码。

HTML:

       $.ajax({ /* START AJAX */

            type: "POST", 
            url: "actionprt.php", 
            data: {"loadnumber": loadnumber},
            dataType: 'json', 

           success: function(data) {

            $("#CustomerName").val(result.Customer);
            $("#CustomerAddress").val(result.Address);
                    }                        
            }); /* END OF AJAX */   

     $.ajax({ /* START AJAX */

            type: "POST", 
            url: "actionprt2.php", 
            data: {"loadnumber": loadnumber},
            dataType: 'json', 

           success: function(data) {

            $("#prtdescription1").val(result.data[0]);
            $("#prtmethod1").val(result.data[1]);
                    }                        
            }); /* END OF AJAX */ 

PHP:

      $stmt = $con->prepare("SELECT `tblExpDescription`.`Description`,`Method`,`PONumber`,`Gallons`,`DollarAmount` FROM `tblExpenses` LEFT JOIN `tblExpDescription` ON (`tblExpenses`.`Description` = `tblExpDescription`.`ExpenseID`) WHERE (`tblExpenses`.`LoadID`= ?);");

$stmt->bind_param("i", $_POST["loadnumber"]); /* PARAMETIZE THIS VARIABLE TO YOUR QUERY */
$stmt->execute(); /* EXECUTE QUERY */
$stmt->bind_result($Description, $Method, $PONumber, $Gallons, $Amount); 
/* BIND THE RESULTS TO THESE VARIABLES */
$stmt->fetch(); /* FETCH THE RESULTS */
$stmt->close(); /* CLOSE THE PREPARED STATEMENT */

/* RETURN THIS DATA TO THE MAIN FILE */
echo json_encode(array("Description" => $Description, "Method" => $Method, "PONumber" => $PONumber, "Gallons" => $Gallons, "Amount" => $Amount));
}

这将返回行,但可以有0行或多行。如何将此数组返回到表单?

2 个答案:

答案 0 :(得分:1)

你怎么得到data并使用result.data尝试像这样使用它

success: function(data) {
   $.each(data , function(key , value){
      console.log(key);
      console.log(value);
      console.log(value.des);
      console.log(value.met);
   });
  }                        
}); /* END OF AJAX */ 

答案 1 :(得分:1)

为他人。这是我最后的工作代码。

Jquery的:         $ .ajax({/ * START AJAX * /

        type: "POST", /* METHOD TO USE TO PASS THE DATA */
        url: "actionprt2.php", /* THE FILE WHERE WE WILL PASS THE DATA */
        data: {"loadnumber": loadnumber}, /* THE DATA WE WILL PASS TO action.php */
        dataType: 'json', /* DATA TYPE THAT WILL BE RETURNED FROM action.php */
        success: function(data) {
            i = 1
           $.each(data , function(key , value){
            $.each(value , function(key2 , value2){

             $("#prt" + key2 + i).val(value2);
             $("#prt" + key2 + i).val(value2);
             $("#prt" + key2 + i).val(value2);
             $("#prt" + key2 + i).val(value2);
             $("#prt" + key2 + i).val(value2);

              });
              i++
           });
          }                        
        }); /* END OF AJAX */ 

PHP:

    $stmt = $con->prepare("SELECT `tblExpDescription`.`Description`,`Method`,`PONumber`,`Gallons`,`DollarAmount` FROM `tblExpenses` LEFT JOIN `tblExpDescription` ON (`tblExpenses`.`Description` = `tblExpDescription`.`ExpenseID`) WHERE (`tblExpenses`.`LoadID`= ?);");

    $stmt->bind_param("i", $_POST["loadnumber"]); /* PARAMETIZE THIS VARIABLE TO YOUR QUERY */
    $stmt->execute(); /* EXECUTE QUERY */
    $stmt->bind_result($Description, $Method, $PONumber, $Gallons, $Amount); 
    /* BIND THE RESULTS TO THESE VARIABLES */
    $detail = array();  
    while($stmt->fetch()) {           
        $detail[] = array('Description' => $Description, 'Method' => $Method, 'PONumber' => $PONumber, 'Gallons' => $Gallons, 'Amount' => $Amount);
}
    $stmt->close(); /* CLOSE THE PREPARED STATEMENT */

    /* RETURN THIS DATA TO THE MAIN FILE */
    echo json_encode($detail);
    } /* END OF IF NOT EMPTY loadnumber */