2014-09-20 157 views
-2

我有一个ajax调用,返回json我试图发送返回给特定输入文本id的项目。

这是AJAX:

$.ajax({ 
    url: "php/myfirstfile.php", 
    type: "POST", 
    data: $("#frameroof").serialize(), 
    cache: false, 
    dataType: "json", 
    success: function (json) { 
     $.each(json, function() { 
      $.each(json, function (key, value) { 
       /// do stuff 
       $('#' + key).val(value); 
      }); 
     }); 
    } 
}); 

这是返回什么:[{"a-frame":"100"}][{"vertical":"350"}]

它看起来即时得到2个阵列时,我需要一个循环结束了。我不确定。

这里是PHP

if(isset($_POST["cart"])){ 
    $frameArray = ($_POST["cart"]); 

    if(is_array($frameArray)){ 
     foreach($frameArray as $row){ 
      $catalogue = $row['catalogue']; 
      $certification = $row['certification']; 
      $catagory = $row['catagory']; 
      $subcatagory = $row['subcatagory']; 
      $length = $row['length'] ; 

      $sql = "SELECT `price` AS '$subcatagory' FROM `products` WHERE `catalogue_id` = '$catalogue' AND `certification` = '$certification' AND `catagory` = '$catagory' AND `sub_catagory` = '$subcatagory' AND `length` = '$length' "; 

      $result = $short_connect->query($sql); 

      if (($result) && ($result->num_rows > 0)) { 
       $results = array(); 
       //convert query result into an associative array 
       while ($row = $result->fetch_assoc()) { 
        $results[] = $row; 
       } 

       //dump all data from associative array converted from query result 
       echo (json_encode($results,true)); 

       $result->free(); 
      } 
     } 
    } 
} 

$short_connect->close(); 
+2

'[{ “一个框架”: “100”}] [{ “垂直”: “350”}] '作为一个字符串不是一个有效的JSON – 2014-09-20 22:19:51

+0

我认为你试图循环一个字符串。 – taylorcressy 2014-09-20 22:19:55

+0

是否有可能看到你的一些PHP的外观? – Frumples 2014-09-20 22:19:56

回答

0

我相信你的问题是非常简单的。在循环内部,您正在初始化结果数组,并将其输出。这会导致创建一个新的结果数组,并将其序列化为JSON,并在循环的每次迭代中输出。因此,您发送浏览器的内容不是JSON,而是几个JSON块一起运行。

这是你所需要的基本思路是这样做:

<?php 
// Initialize the output data 
$results = array(); 

foreach($something as $a_something) { 
    $results[] = do_something_to($a_something); 
} 

//Serialize and send the output data 
echo (json_encode($results,true)); 

在考虑该模式重新排列你的代码产生的(这应该为你工作,并且将返回一个空数组到浏览器,如果你的if条件不具备或查询不返回任何东西):

<?php 

// Initialize the output data 
$results = array(); 

if(isset($_POST["cart"])){ 
    $frameArray = ($_POST["cart"]); 

    if(is_array($frameArray)){ 
     foreach($frameArray as $row){ 
      $catalogue = $row['catalogue']; 
      $certification = $row['certification']; 
      $catagory = $row['catagory']; 
      $subcatagory = $row['subcatagory']; 
      $length = $row['length'] ; 

      $sql = "SELECT `price` AS '$subcatagory' FROM `products` WHERE `catalogue_id` = '$catalogue' AND `certification` = '$certification' AND `catagory` = '$catagory' AND `sub_catagory` = '$subcatagory' AND `length` = '$length' "; 

      $result = $short_connect->query($sql); 

      if (($result) && ($result->num_rows > 0)) { 
       //convert query result into an associative array 
       while ($row = $result->fetch_assoc()) { 
        //Add to the output data 
        $results[] = $row; 
       } 

       $result->free(); 
      } 
     } 
    } 
} 

$short_connect->close(); 

//Serialize and send the output data 
//dump all data from associative array converted from query result 
echo (json_encode($results,true)); 
+1

你的编码人。我真的很感激,比你知道的更多 – user3303279 2014-09-20 23:24:36

+0

没问题!不断堵塞,并在这里互动 - 你会继续更好! – JAAulde 2014-09-20 23:53:16