Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在自定义数组JavaScript中创建输入数未知的JSON_Javascript_Php_Jquery_Json - Fatal编程技术网

在自定义数组JavaScript中创建输入数未知的JSON

在自定义数组JavaScript中创建输入数未知的JSON,javascript,php,jquery,json,Javascript,Php,Jquery,Json,预期产出为: while($ques = mysql_fetch_array($query)){ <input type='text' name='answers' test_id='".$ques['test_id']."' question_no ='[".$ques['question_no']."]'> } var frm = $('#test-set-form'); var data = JSON.stringify(frm.serializeArray());

预期产出为:

while($ques = mysql_fetch_array($query)){
    <input type='text' name='answers' test_id='".$ques['test_id']."' question_no ='[".$ques['question_no']."]'>
}
var frm = $('#test-set-form');
var data = JSON.stringify(frm.serializeArray());

我正在从数量不定的输入创建一个JSON,然后发送到服务器,并将每个数据保存到数据库中。有什么建议吗?提前感谢。

最简单的方法是更改表单元素名称属性以使用数组,这样您就可以:

[
    {"name":"answers","test_id":"1","question_no":"1",value":"<input value>"},
    {"name":"answers","test_id":"1","question_no":"2",value":"<input value>"},
    {"name":"answers","test_id":"1","question_no":"3",value":"<input value>"},

    {"name":"answers","test_id":"2","question_no":"1",value":"<input value>"},
    {"name":"answers","test_id":"2","question_no":"2",value":"<input value>"},
    {"name":"answers","test_id":"2","question_no":"3",value":"<input value>"},
]
然后你可以读一些类似的东西:

Array(
    'tests' => Array(
        'the_test_id' => Array(
            'questions' => Array(
               'the_question_id' => 'the answer value'
            )
        )
    )
)
<input name="tests[the_test_id][questions][the_question_id]" data-test_id="the_test_id" data-question_no="the_question_id" />
因此,元素的输出如下所示:

<input name="tests[the_test_id][questions][the_question_id]" />
foreach($_POST['tests'] as $test_id => $testData) {
    foreach ($testData['questions'] as $question_no => $answer) {
        // update the db using $test_id, $question_no, and $answer
    }
}
这将使您的输出看起来像:

Array(
    'tests' => Array(
        'the_test_id' => Array(
            'questions' => Array(
               'the_question_id' => 'the answer value'
            )
        )
    )
)
<input name="tests[the_test_id][questions][the_question_id]" data-test_id="the_test_id" data-question_no="the_question_id" />
工作小提琴:

我使用smth如下:

var data = [],
    $inputs = $(selectorForTheInputs);

$inputs.each(function (){
    var $this = $(this),
        datum = $this.data();

    data.push($.extend({value: $this.val()}, datum));
});
如果只需要使用符号,则使用js解析html:

[
    { "key":"answers[1][1]", value":"" },
    { "key":"answers[1][2]", value":"" },
    //...
    { "key":"answers[5][15]", value":"" }
    //...
]
你会得到你想要的

var $inputs = $('#form').find('input[name=answers]');
var data = [];

$inputs.each(function() {
    var attrsData = {};
    attrsData['name'] = $(this).attr('name');
    ...
    data.push(attrsData)
});

请停止使用MySQL_函数,因为它们已被弃用,并且可能不安全。使用MySqli或PDO。最好在表单中具有唯一的名称属性。除此之外,你似乎有很多测试,每个测试都有很多问题,对吗?如果是这样的话,格式化响应JSON的方式是非规范化的形式。基本上,以面向对象的方式。因此,您有许多类的命名取决于您,它们以以下方式组成:TestingResponse->TestAnswer[]->Test,Answer[]->Question。注意方括号[]表示多重性。例如,{测试:[{测试id:1,答案:[{问题id:1,值:…},…},…]},…]}谢谢你:我欠你太多了。在php方面,我使用的是PHP5.2。您能告诉我如何获取版本中的每个数据吗?
[
    { "key":"answers[1][1]", value":"" },
    { "key":"answers[1][2]", value":"" },
    //...
    { "key":"answers[5][15]", value":"" }
    //...
]
var $inputs = $('#form').find('input[name=answers]');
var data = [];

$inputs.each(function() {
    var attrsData = {};
    attrsData['name'] = $(this).attr('name');
    ...
    data.push(attrsData)
});
$.ajax({
    data: data
    ...
})