0

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?

3
  • could you please share what does dd(session()->getOldInput()) reutrns, add this while submitting. Commented May 7, 2022 at 14:49
  • It shows me an empty array Commented May 7, 2022 at 14:51
  • Nobody can help without knowing what your controller does. For format use old('answers[0]') or old('answers.0'). See stackoverflow.com/questions/42050732/old-input-for-array Commented May 7, 2022 at 16:57

2 Answers 2

1

When you submit a form that has a HTML array (name="answers[]"), it will be converted to a proper PHP array in the backend. This also means that the values will be indexed like a PHP array is always indexed. 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. If you want the second answer, you will have to fetch old('answers.1').

If you want the structure to be more clear and dynamic, you could use your question ID as index parameter:

@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. 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

Sign up to request clarification or add additional context in comments.

Comments

0
<textarea name="answers[]" 
    class="form-control ms-3" cols="30" rows="2" 
    placeholder="Your answer here.."
    >{{ old('answers.' . $loop->index) }}</textarea>

3 Comments

It helps when you explain why your code works and why the question's code does not. This piece of code might work but it's probably not clear to the asker as to why it works.
@codingbruh Your edit of my answer introduces an issue. Now the content of the text area will include an extra and unwanted newline . The text area content should be immediately wrapped by the > and < . Please revert your edit
Alright. Fair Enough

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.