2011-09-17 3 views
1

JSON形式のデータをAjax経由でPHPに渡す次のコードを用意していますが、PHPコードで結果が表示されません。ここでJSONをPHPで動作させる方法

var array_str_idnum = []; 

for (var i=0;i<2;i++) { 
    array_str_idnum[i] = []; 
} 

$('#movetoset').click(function() { 
    if ($('#selectsett').val() === 'General') { 
    } 

    for(j=0;j< (array_str_idnum[0]).length;j++) { 
     if((document.getElementById('check' + array_str_idnum[0][j]).checked) && (array_str_idnum[1][j] != "moved")) { 
      document.getElementById('imagediv' + array_str_idnum[0][j]).style.display = 'none'; 
      array_str_idnum[1][j] = "moved"; 
      index = ((array_str_idnum[0]).length - 1 - j) + ''; 
      var str = $("#complicated").serialize() + "&myindex=" + encodeURIComponent(index) ; 
      var desc_str = document.getElementById('textarea' + array_str_idnum[0][j]).value; 
      str = str + "&mydescription=" + encodeURIComponent(desc_str); 
      $.ajax({ 
       type: "POST", 
       url: "addtoset.php", 
       data: str, 
       cache: false, 
       success: function(msg) { 
        $("#formstatus").ajaxComplete(function(){$(this).fadeIn("slow").html(msg + '<br /><br />')}); 
        $("#formstatus").append(msg); 
       } 
      }); 
     } 
    } 
    mydata = JSON.stringify(array_str_idnum); 

    $.ajax({ 
     type:   'post', 
     cache:   false, 
     url:   'parser.php', 
     data:   {myJson: mydata}, 
     success: function(msg) { 
      $("#formstatus").ajaxComplete(function() { $(this).fadeIn("slow").html(msg) }); 
     } 
    }); 
}); 

は私のPHPコードです:

$decoded = json_decode($_POST['myJson'],true); 

// do something with data here 
echo "decoded = $decoded[1][0]"; 

コードが悪いのか?

+1

あなたはの例を投稿することができますJSONはAJAX呼び出しによって返されますか? – b01

+0

note; $ echo [1] [0] ";または' echo "はデコードされなければなりません[1] [0] 0]; ' – Shad

+0

まず$ _POST配列にvar_dumpを実行し、その中に含まれているものを見てください。 –

答えて

0

おそらく唯一の問題はecho文です。あなたは、これが原因でPHPのみ通知、二重引用符で囲まれた文字列で、「正常な」変数である

echo "decoded = {$decoded[1][0]}"; // 

echo "decoded = ".$decoded[1][0]; 

または

にそれを変更する必要があります。配列要素(またはオブジェクトのプロパティ)の場合は、変数の周りに中括弧を使用するか、文字列連結を使用する必要があります。

私はそうのようなあなたは、他の人が提案のようなPHPコードを修正したいと思います
+0

ありがとう、それはちょうど私に復号化= nullを与える – tony

0

:ここ

<?php 

    if (!empty($_POST['myJson']) && strlen($_POST['myJson']) > 0) 
    { 
     $decoded = json_decode($_POST['myJson'], true); 
     // Echo out the JSON onject as a JavaScript variable. 
     echo "decoded = {$decoded[1][0]};"; 
    } 
    else 
    { 
     // Echo out the JSON onject as a JavaScript variable. 
     echo "decoded = null;"; 
    } 
?> 

は、いくつかのマイナーな提案をJavaScriptコードは次のとおりです。

<script type="text/javascript"> 
    $(document).ready(function() 
    { 
     var array_str_idnum = []; 

     // Init the array with two elemsnts that contain empty literal arrays. 
     for (var i = 0; i < 2; i++) 
     { 
      array_str_idnum[ i ] = []; 
     } 

     $("#movetoset").click(function() 
     { 
      var $checkbox, str, desc_str, elementSuffix; 
      // I believe the code in here was removed for privacy reasons. 
      // I also believe it populats 'array_str_idnum' with some values of some kind. 
      if ($("#selectsett").val() === "General") 
      { 
       // ... 
      } 

      for (var i = 0; i < (array_str_idnum[0]).length; i++) 
      { 
       elementSuffix = array_str_idnum[ 0 ][ i ]; 
       // Grab the checkbox. 
       $checkbox = $("#check" + elementSuffix); 

       if ($checkbox.checked && (array_str_idnum[1][i] != "moved")) 
       { 
        // Hide the image. 
        $("#imagediv" + elementSuffix).css({ "display": "none" }); 
        // Indicate that this one is now moved, so do NOT process it again. 
        array_str_idnum[ 1 ][ i ] = "moved"; 

        index = ((array_str_idnum[0]).length - 1 - i) + ''; 
        // Setting str here will reinitialize it 
        str = $("#complicated").serialize() + "&myindex=" + encodeURIComponent(index); 
        desc_str = $("#textarea" + elementSuffix).value; 
        str = str + "&mydescription=" + encodeURIComponent(desc_str); 
        // Bad idea to put ajax call in a loop. 
        $.ajax({ 
         "type": "POST", 
         "url": "addtoset.php", 
         "data": str, 
         "cache": false, 
         "success": function(msg) 
         { 
          $("#formstatus").ajaxComplete(function() 
          { 
           $(this).fadeIn("slow").html(msg + "<br /><br />"); 
          }); 

          $("#formstatus").append(msg); 
         } 
        }); 
       } 
      } 

      mydata = JSON.stringify(array_str_idnum); 

      $.ajax({ 
       "type": "POST", 
       "cache": false, 
       "url": "parser.php", 
       "data": { myJson: mydata }, 
       "success": function(msg) 
       { 
        $("#formstatus").ajaxComplete(function() 
        { 
         $(this).fadeIn("slow").html(msg) 
        }); 
       } 
      }); 
     }); 
    }); 
</script> 
関連する問題