将可复制表单的JSON数据解析为PHP for Mail [英] Parsing Duplicatable Form's JSON data to PHP for Mail

查看:71
本文介绍了将可复制表单的JSON数据解析为PHP for Mail的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,其中存在可重复的字段,并且它会生成以下JSON数据:

I have a form where there are duplicate-able fields and it produces the following JSON data:

{
    "mainmember": [
        {
            "name": "testtest",
            "surname": "test",
            "id": "8509295266086",
            "age": "27",
            "gender": "Male",
            "townofbirth": "george",
            "email": "test@test.com",
            "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个依赖项,但是在重复时,它增加为"dependant3,Dependant4,Dependant5".

now the duplicate-able fields are the dependants, in this JSON there's 2 dependant but on duplication it increases to "dependant3, dependant4, dependant5".

目前,当我提交并发送到PHP时,我可以算出家属:

Currently when i submit and send to PHP i can count the dependants:

    <?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 send函数:

and the jQuery send function I'm using:

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);
});

我只是无法获取其他数据,例如姓名,姓氏等. 我需要像这样将其解析为电子邮件:

I just cant get the other data, such as name, surname etc. I need to parse it like this to an email:

主要成员: 详细信息

依存者2: 详细信息

依赖者3: 详细信息

任何帮助我们都感激不尽.

Any Help greatly appreciated.

推荐答案

您的json结构非常痛苦,您似乎正在创建单个对象的数组. 相反,您应该具有对象数组或键引用的单个对象.

Your json structure is quite painful, you seem to be creating arrays of a single object. Instead you should have arrays of objects, or single objects referenced by keys.

理想地,对于您的结构,我认为您应该为依赖对象提供一个对象数组,为主要成员提供一个对象,如下所示:

Ideally for your structure, I think you should have an array of objects for the dependents and a single object for the mainmember, like this:

{
    "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": "fae@gmail.COM",
            "passport": "1212112111111",
            "relationship": "spouse"
        },
        {
            "name": "dtjtet2",
            "surname": "grwwr2",
            "id": "11112222222222",
            "age": "44",
            "gender": "M",
            "townofbirth": "knmysn",
            "cell": "0045128588",
            "email": "fae@gmail.COM",
            "passport": "1212112111111",
            "relationship": "spouse"
        }
    ]
}

这样,您就具有一个"mainmember"对象和一组依赖项.

This way you have a single "mainmember" object, and an array of dependents.

然后使用如下所示的php代码,您可以为其打印详细信息:

Then with php code like the following you can print the details for it:

<?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结构,如下所示:

To create the correct json structure in the jquery code, something like this:

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);
});

这未经测试,但应该可以帮助您继续.您的问题是因为您使用的是单个对象的数组,而不是对象本身.

This is untested but should help you proceed. Your problem is because you are using arrays of single objects instead of the object itself.

这篇关于将可复制表单的JSON数据解析为PHP for Mail的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆