2014-11-05 43 views
0

發送的序列化數據我只想知道如何檢索這個數據回來在PHP? (它是序列化和JSON)

這是我當前的代碼:

相關的HTML位:

<form id="add-review-form" action="/review/save" method="post"> 
     <div> 
      <textarea name="reviewDescription" class="review"></textarea> 
      <input type="hidden" name="organisationId" value="<?=$this->organisation->id?>"/> 
      <input type="hidden" name="score"/> 
      <button id="save-review-button" type="button" class="small">Opslaan</button> 

     </div> 

</form> 

jQuery的位:

  // Get form as jQuery object 
      var addReviewForm = $('#add-review-form'); 

      // Remove errors 
      removeFormMessages(addReviewForm); 

      // Get data 
      var data = addReviewForm.serialize(); 
      $.post("/review/save", data, function (data, status) { 
        console.log(data); 
       } 
      ,"json"); 

回答

0

我會打電話的JQuery張貼這樣:

$.post("/review/save", {json: JSON.stringify(data)}, 
     function (data, status) { 
     console.log(data); 
     }, "json"); 

後來PHP端,你可以對它進行分析後訪問submited JSON對象:

<?php 
    $json_array = json_decode($_POST['json']); 
?> 
+0

那麼這將像一個數組可訪問?例如$ jsondata ['score']會給我的分數?(我對json不是很熟悉) – 2014-11-05 10:55:08

+0

。另一方面,如果你按照你所做的方式發佈了一篇文章,你可以像普通的POST參數那樣訪問submited的值,比如$ _POST ['INPUT_VALUE_NAME'] – 2014-11-05 11:00:32

0

你可以得到的原始數據,如

$rawdata = file_get_contents('php://input'); 

要想從這個JSON數據,你可以使用

$jsondata = json_decode($rawdata,true); 

如果第二個參數爲true,則json_decode將返回結果associative array

+0

那麼這將會像數組一樣訪問?例如$ jsondata ['score']會給我的分數?(我不是很熟悉json) – 2014-11-05 10:53:47

+0

我已根據您的要求更新了答案。如果你在'json_decode'中傳遞第二個參數爲'true',它將返回關聯數組,這樣你就可以像'$ jsondata ['score']'一樣使用'。有關更多信息,請在php代碼中嘗試'var_dump($ jsondata)'。 – 2014-11-05 11:00:40