[英]How to get old input value from name input array laravel
I have this code我有这个代码
@foreach($questions as $question_name => $question_details)
<h5>{{ $question_name }}</h5>
@foreach($question_details as $question_id => $question_description)
<p class="ms-3">{{ $question_description }}</p>
<input type="hidden" name="question_id[]" value="{{ $question_id }}"/>
<textarea name="answers[]" class="form-control ms-3" cols="30" rows="2" placeholder="Your answer here.."></textarea>
@endforeach
@endforeach
And when I submit a form with some empty values, the answers that I got also get deleted if some error occurs I tried this to get back my old inputs but it didn't bring me anything当我提交一个包含一些空值的表单时,如果出现错误,我得到的答案也会被删除
I tried我试过了
<textarea name="answers[]" class="form-control ms-3" cols="30" rows="2" placeholder="Your answer here..">{{ old('answers') }}</textarea>
<textarea name="answers[]" class="form-control ms-3" cols="30" rows="2" placeholder="Your answer here..">{{ old('answers[]') }}</textarea>
<textarea name="answers[]" class="form-control ms-3" cols="30" rows="2" placeholder="Your answer here..">{{ old('answers.0') }}</textarea>
But nothing really succeed to bring me back the old values that I typed can someone help me with that?但是没有什么能真正成功地让我恢复我输入的旧值,有人可以帮助我吗?
When you submit a form that has a HTML array ( name="answers[]"
), it will be converted to a proper PHP array in the backend.当您提交具有 HTML 数组 ( name="answers[]"
) 的表单时,它将在后端转换为正确的 PHP 数组。 This also means that the values will be indexed like a PHP array is always indexed.这也意味着这些值将被索引,就像 PHP 数组总是被索引一样。 If you have three rows, even without values, you can expect it to look like so:如果你有三行,即使没有值,你也可以期望它看起来像这样:
dd($request->input('answers');
/* Output: [
0 => null,
1 => null,
2 => null,
]; */
When you then fetch old('answers')
, you will get an array
.当您随后获取old('answers')
时,您将获得一个array
。 If you want the second answer, you will have to fetch old('answers.1')
.如果您想要第二个答案,则必须获取old('answers.1')
。
If you want the structure to be more clear and dynamic, you could use your question ID as index parameter:如果您希望结构更加清晰和动态,您可以使用您的问题 ID 作为索引参数:
@foreach($question_details as $question_id => $question_description)
<p class="ms-3">{{ $question_description }}</p>
<textarea name="answers[{{$question_id}}]" class="form-control ms-3" cols="30" rows="2" placeholder="Your answer here...">{{old("answers.{$question_id}")}}</textarea>
@endforeach
Now your array
will be keyed by your question IDs.现在您的array
将由您的问题 ID 键入。 If, however, you don't want to do that, you can also key by loop index:但是,如果您不想这样做,您也可以按循环索引键:
@foreach($question_details as $question_id => $question_description)
<p class="ms-3">{{ $question_description }}</p>
<input type="hidden" name="question_id[{{$loop->index}}]" value="{{ $question_id }}"/>
<textarea name="answers[{{$loop->index}}]" class="form-control ms-3" cols="30" rows="2" placeholder="Your answer here...">{{old("answers.{$loop->index}")}}</textarea>
@endforeach
You can read more about the $loop
variable here您可以在此处阅读有关$loop
变量的更多信息
<textarea name="answers[]"
class="form-control ms-3" cols="30" rows="2"
placeholder="Your answer here.."
>{{ old('answers.' . $loop->index) }}</textarea>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.