2013-01-21 16 views
0

我哪裏有重複的,能夠字段的表單,它產生以下JSON數據:解析Duplicatable窗體的JSON數據到PHP的郵件

{ 
    "mainmember": [ 
     { 
      "name": "testtest", 
      "surname": "test", 
      "id": "8509295266086", 
      "age": "27", 
      "gender": "Male", 
      "townofbirth": "george", 
      "email": "[email protected]", 
      "contact": "0112121211", 
      "passport": "1111111111111", 
      "postal": "grjiubrwg", 
      "postal_code": "0010", 
      "residential": "tytyytyttyytyt", 
      "residential_code": "4422" 
     } 
    ], 
    "dependant1": [ 
     { 
      "name": "testDUP", 
      "surname": "testDUP", 
      "id": "4802235040081", 
      "age": "64", 
      "gender": "Male", 
      "townofbirth": "tehjte", 
      "cell": "4774811845", 
      "email": "testDUP", 
      "passport": "8202255133088", 
      "relationship": "spouse" 
     } 
    ], 
    "dependant2": [ 
     { 
      "name": "testDUP2", 
      "surname": "testDUP2", 
      "id": "1111111111111", 
      "age": "45", 
      "gender": "F", 
      "townofbirth": "knysnasw", 
      "cell": "0000000000", 
      "email": "testDUP2", 
      "passport": "2222222222222", 
      "relationship": "inlaw" 
     } 
    ] 
} 

現在重複的,能夠字段是家屬,在這JSON有2個依賴,但重複增加到「依賴3,依賴4,依賴5」。

目前,當我提交,發送給PHP我可以指望的家屬:

<?php 
$json = $_POST['parameters']; 
$json_string = stripslashes($json); 
$data = json_decode($json_string, true); 

$datasetCount = count($data); // returns count of array items of any array 
echo "<h1>There are $datasetCount Dependants</h1>"; 

$i = 0; 
foreach ($data as $each_dep) { 
    $i++; 
    echo "<h2>Dependant $i</h2>"; 
    while (list($key, $value) = each ($each_dep)) { 

     echo "$key: $value<br />"; 

    } 

} 

?> 

和jQuery的發送功能,我使用的是:

jQuery('#submit').click(function(){ 
    jQuery('div[class*="mainmember"]').each(function(k, v){ 
     mainmember = {}; 
     mainmember['id'] = ''; 
     $(v).find('.id').each(function(){ 
      mainmember['id'] += $(this).val(); 
     }); 
     mainmember['age'] = ''; 
     $(v).find('.age').each(function(){ 
      mainmember['age'] += $(this).val(); 
     }); 
     mainmember['gender'] = $(v).find('.gender').val(); 
     result['mainmember'] = [mainmember]; 

    }); 
    jQuery('div[class*="dependant"]').each(function(k, v){ 
     dep_counter++ 
     dependants = {}; 
     result['dependant'+dep_counter] = [dependants]; 
     dependants['id'] = ''; 
     $(v).find('.id').each(function(){ 
      dependants['id'] += $(this).val(); 
     }); 
     dependants['age'] = ''; 
     $(v).find('.age').each(function(){ 
      dependants['age'] += $(this).val(); 
     }); 
     dependants['gender'] = $(v).find('.gender').val(); 
    }); 
    var jsonData = JSON.stringify(result); 

    console.log(jsonData); 
}); 

我只是無法得到其他數據,如名字,姓氏等 我需要像這樣解析它到一封電子郵件:

主要成員: 詳細信息

從屬2: 細節

從屬3: 細節

任何幫助不勝感激。

+0

你想做什麼?你能展示一個包含重複的例子嗎? –

+0

試圖清除它更多..希望它有幫助 –

+0

在你的形式,有多少'mainmember'?只有一個,還是很多? –

回答

2

你的json結構很痛苦,你似乎在創建單個對象的數組。 相反,您應該擁有對象的數組,或者由鍵引用的單個對象。

理想的情況下爲你的結構,我想你應該有對象的數組的家屬和單個對象的mainmember,就像這樣:

{ 
    "mainmember": { 
     "name": "test2", 
     "id": "1111111111111", 
     "age": "45", 
     "gender": "M", 
     "townofbirth": "knysna", 
     "email": "tjyrrtjhe", 
     "contact": "1111111111", 
     "passport": "5555555555555", 
     "postal": "yrtjyt", 
     "postal_code": "1101", 
     "residential": "tkyutk", 
     "residential_code": "5555" 
    }, 
    "dependants": [ 
     { 
      "name": "dtjtet", 
      "surname": "grwwr", 
      "id": "1111222222222", 
      "age": "48", 
      "gender": "F", 
      "townofbirth": "knmysn", 
      "cell": "0045128588", 
      "email": "[email protected]", 
      "passport": "1212112111111", 
      "relationship": "spouse" 
     }, 
     { 
      "name": "dtjtet2", 
      "surname": "grwwr2", 
      "id": "11112222222222", 
      "age": "44", 
      "gender": "M", 
      "townofbirth": "knmysn", 
      "cell": "0045128588", 
      "email": "[email protected]", 
      "passport": "1212112111111", 
      "relationship": "spouse" 
     } 
    ] 
} 

這種方式,你有一個單一的「mainmember」對象,和一系列家屬。

<?php 
function printMember($member) { 
    foreach($member as $key=>$value) { 
     echo "$key : $value <br />"; 
    } 
} 


$json = $_POST['parameters']; 
$json_string = stripslashes($json); 
$data = json_decode($json_string, true); 

$depCount = count($data["dependants"]); 
echo "<h1>There are $depCount Dependants</h1>"; 

echo "<h2>Main member data:</h2>"; 
printMember($data["mainmember"]); 

foreach ($data["dependants"] as $index => $dependant) { 

    echo "<h2>Dependant $index</h2>"; 
    printMember($dependant); 

} 

要在jQuery代碼創建正確的JSON結構,像這樣:

jQuery('#submit').click(function(){ 
    jQuery('div[class*="mainmember"]').each(function(k, v){ 
     mainmember = {}; 
     mainmember['id'] = ''; 
     $(v).find('.id').each(function(){ 
      mainmember['id'] += $(this).val(); 
     }); 
     mainmember['age'] = ''; 
     $(v).find('.age').each(function(){ 
      mainmember['age'] += $(this).val(); 
     }); 
     mainmember['gender'] = $(v).find('.gender').val(); 
     //This is changed: Remove the [] from around the mainmember, 
     //should be a single object not an array 
     result['mainmember'] = mainmember; 

    }); 

    //create the array of dependants 
    result['dependants'] = []; 
    jQuery('div[class*="dependant"]').each(function(k, v){ 
     dependant = {}; 
     dependant['id'] = ''; 
     $(v).find('.id').each(function(){ 
      dependant['id'] += $(this).val(); 
     }); 
     dependant['age'] = ''; 
     $(v).find('.age').each(function(){ 
      dependant['age'] += $(this).val(); 
     }); 
     dependant['gender'] = $(v).find('.gender').val(); 

     //add the dependant to the array 
     result['dependants'].push(dependant); 
    }); 
    var jsonData = JSON.stringify(result); 

    console.log(jsonData); 
}); 

這是

與PHP代碼像下面你可以打印細節爲它

然後未經測試,但應該幫助你繼續。你的問題是因爲你使用單個對象的數組而不是對象本身。

+0

完美謝謝.. 。 –