我有一个问题,如何在发送表单后获取数组元素。 我的目标是保存由语言创建的元素。 $ _POST的返回并不清楚我是否正确保存了bd中的日期。
示例:
表格
def to_representation(self, value):
lvalue = context.get('count', 0)
lvalue += 1
context.update({'count': lvalue})
serializer_data = SubTypeSerializer(value, context=context).data
return serializer_data
class TypeSerializer(serializers.ModelSerializer):
extends_type = RecursiveField(allow_null=True)
def to_representation(self, instance):
self.context.update({'count': 0})
return super().to_representation(instance)
class Meta:
model = Type
fields = '__all__'
class SubTypeSerializer(serializers.ModelSerializer):
extends_type = RecursiveField(allow_null=True)
class Meta:
model = Type
fields = '__all__'
此脚本允许在我单击某种语言时创建一个字段(例如)
languages = 2; // en and fr
for ($i=0, $n=sizeof($languages); $i<$n; $i++) {
$content .= '
<div class="col-md-12">
<div class="form-group row">
<div class="input-group input-group-option">
<label for="lang" class="col-1 col-form-label">' . $OSCOM_Language->getImage($languages[$i]['code']) . '</label>
' . HTML::inputField('user_question[][' . $languages[$i]['id'] . ']', null, 'placeholder="Write a question"') . '
<span class="input-group-addon input-group-addon-remove">
<span>X</span>
</span>
</div>
</div>
</div>
发布结果
<script>
$(function(){
$(document).on('focus', 'div.form-group-options div.input-group-option:last-child input', function(){
var sInputGroupHtml = $(this).parent().html();
var sInputGroupClasses = $(this).parent().attr('class');
$(this).parent().parent().append('<div class="'+sInputGroupClasses+'">'+sInputGroupHtml+'</div>');
});
$(document).on('click', 'div.form-group-options .input-group-addon-remove', function(){
$(this).parent().remove();
});
});
</script>
以下答案,反应良好;
var_dump($_POST['user_question'])
array(8) {
[0]=> array(1) { [1]=> string(9) "test_en_1" }
[1]=> array(1) { [1]=> string(9) "test_en_2" }
[2]=> array(1) { [1]=> string(9) "test_en_3" }
[3]=> array(1) { [1]=> string(0) "" }
[4]=> array(1) { [2]=> string(9) "test_fr_1" }
[5]=> array(1) { [2]=> string(9) "test_fr_2" }
[6]=> array(1) { [2]=> string(9) "test_fr_3" }
[7]=> array(1) { [2]=> string(0) "" } }
结果
$N = sizeof($_POST['user_question']); // 4
$M = sizeof($languages); // 2
for ($i=0; $i<$N; $i++) {
for ($j=0; $j<$M; $j++) {
$language_id = $languages[$M]['id']+1;
if (!is_null($_POST['user_question'][$i + $j * $N]) ) {
$test = (bool)array_filter($_POST['user_question'][$i + $j * $N], create_function('$a','return preg_match("#\S#", $a);'));
if ($test == true) {
var_dump($_POST['user_question'][$i + $j * $N]);
var_dump('<br>');
}
}
}
如何删除此
的问题array(1) { [1]=> string(9) "test_en_1" } string(4) "
" array(1) { [1]=> string(9) "test_en_2" } string(4) "
" array(1) { [1]=> string(0) "" } string(4) "
" array(1) { [2]=> string(9) "test_fr_1" } string(4) "
" array(1) { [2]=> string(9) "test_fr_2" } string(4) "
" array(1) { [2]=> string(0) "" } string(4) "
我尝试了 " array(1) { [1]=> string(0) "" } string(4) "
,但确实无法正常工作。
谢谢
答案 0 :(得分:0)
尝试:
$N = ....; // some expression that gives the number of questions per language (3 in the sample data)
$M = ....; // some expression that gives languages (2 in the sample data)
for ($i=0; $i<$N; $i++) {
for ($j=0; $j<$M; $j++) {
var_dump($_POST['user_question'][$i + $j * $N]);
}
}