结构JS数据jQuery多类多维数组的最佳方法

时间:2014-08-01 20:23:57

标签: javascript php jquery arrays

我有两个内容:一个用于就业历史,一个用于地址历史。

鉴于一个人可以有多种形式的就业历史(如果他们已经超过1名雇主)或地址历史记录,我可以选择点击加号来添加表格。

因此,我有一个.click()事件处理程序,当单击时会附加一个新表单。

我已经在JS中构建了这样的数据并将其发布到服务器,除了我在执行此操作时遇到问题。我必须在就业或地址中插入每个指数的记录。

var contents = {
    employment: {
        df1e: [

        ],
        dt1e: [

        ],
        c1e: [

        ],
        p1e: [

        ],
        l1e: [

        ]
    },
    address: {
        df1a: [

        ],
        dt1a: [

        ],
        sa1a: [

        ],
        c1a: [

        ],
        st1a: [

        ],
        zc1a: [

        ],
    },
    name: $('#first_name').val() + ' ' + $('#last_name').val()
};

然后我为每个元素编写了$ .each()函数,并将它们添加到contents数组中。 Ex(每个该死的除外):

$('.df1e').each(function(i, obj) {
    contents.employment.df1e[i] = this.value;
});

必须有更好的方法来做到这一点。如何在没有太多麻烦的情况下做到这一点?

<?php

$data = json_decode(file_get_contents('php://input'), true);

if($data) {
        foreach($data['employment'] as $employment) {
            $bean->name = $data['name'] . ' - Employment';
            $bean->type_c = 'Employment';
            $bean->date_from_c = $employment['df1e'];
            $bean->date_to_c = $employment['dt1e'];
            $bean->company_c = $employment['c1e'];
            $bean->position_c = $employment['p1e'];
            $bean->location_c = $employment['l1e'];

            $bean->save();
        }

        foreach($data['address'] as $address) {
            $bean->name = $data['name'] . ' - Address';
            $bean->type_c = 'Address';
            $bean->date_from_c = $address['df1a'];
            $bean->date_to_c = $address['dt1a'];
            $bean->street_address_city_c = $address['c1a'];
            $bean->street_address_state_c = $address['st1a'];
            $bean->street_address_postalcode_c = $address['zc1a'];
            $bean->street_address_c = $address['sa1a'];

            $bean->save();
        }
}

echo json_encode(array('response' => $data));

忽略bean的东西 - 它是SugarCRM的API。它与插入MySQL相同。

1 个答案:

答案 0 :(得分:1)

首先,对象中的键(df1edt1e等)非常神秘,所以无法确切地说出你在做什么。但是保存多个地址和就业细节的最佳方法是使用一组对象(即地址是一个对象数组,雇主是一个对象数组)。您的最终产品看起来像这样:

    {
        "addresses": [
            {
                "street" : "123 fake street",
                "city"   : "New York",
                "state"  : "NY",
                "zip"    : "xxxxx"
            },
            {
                "street" : "910 fake lane",
                "city"   : "San Francisco",
                "state"  : "CA",
                "zip"    : "xxxxx"
            }
        ],
        "employers": [
            {
                "name"   : "stackoverflow",
                "street" : "110 William Street",
                "city"   : "New York",
                "state"  : "NY",
                "zip"    : "10038"
            },
            {
                "name"   : "google",
                "street" : "345 Spear St, Floors 2-4",
                "city"   : "San Franciso",
                "state"  : "CA",
                "zip"    : "94105"
            }
        ]
    }

以下是地址的示例,但雇主部分看起来一样。假设每个地址条目(包括城市,州,邮政等)都是具有“地址条目”类的容器,您可以像这样构建地址数组。

var addresses = [];

$('.address-entry').each(function(){

        var street = $(this).find('.street').val();
        var city   = $(this).find('.city').val();
        // etc - get the other values you need

       // once you have your values, 'push' an object to the addresses array
       addresses.push({

           "street" : street,
           "city"   : city,
           // etc

       });

});

// do the same for employers

// if you want it all in one object, do this
var content = {"addresses": addresses, "employers": employers};

现在您已准备好提交。如果您使用的是ajax,那很简单:

$.ajax({
    url: 'whatever.php',
    type: 'POST',
    data: content,
    success: function(resp){ console.log(resp); },
    error: function(xhr){ console.log(xhr.responseText); }
});

如果您没有使用ajax,只需将JSON.stringify(content)的结果附加到您的表单,并且由于您在服务器上使用了php,请致电:

$addresses = json_decode($_POST['addresses'], true);
$employers = json_decode($_POST['employers'], true);

来自你的php脚本

编辑:拼写