htmlspecialchars() expects parameter 1 to be string, array given I have a form like this.
<select name="education[]" id="education" class="form-control sselecter input-lg" multiple="true" data-placeholder="@lang('global.Select Level Education')" value="{{old('education') }}" required>
@foreach ($educations as $education)
<option value="{{ $education->translation_of}}"@if (old('education')==$education->translation_of)selected="selected"@endif> {{ $education->name }} </option>
@endforeach
</select>
But when the contents of the data then Submit the following error message appears:
"htmlspecialchars() expects parameter 1 to be string, array given (View:
Probably because in value="{{old('education') }}" education is an array and not a string that can be passed to htmlspecialchars
Its pointless anyway, you cannot pass value=into a select form element.
You need to iterate over all the choices and then to know if that choice should be selected you need to test if education contains the current choice
@Snapey
If other fields are filled all does not appear error. But if only the contents of the education section just appeared error
Yep, that will do it. If education contains multiple results then it is an array and you cannot output it in curly brackets.
Did you read my previous reply? What was not understandable?
@Snapey
I do not understand all of your answers above.
please provide examples of writing code according to my code above
You've declared the education field to be an array here:
<select name="education[]"
You are then attempting to use the old value of the education field, which you have declared to be an array, as a string, which it is not.
please provide examples of writing code according to my code above
sure. should I send you my paypal account details?
start with this
<select name="education[]" id="education" class="form-control sselecter input-lg" multiple="true" data-placeholder="@lang('global.Select Level Education')" required>
@foreach ($educations as $education)
<option value="{{ $education->translation_of}}"
@if(in_array($education->translation_of , old('education'))
selected="selected"
@endif >{{ $education->name }}</option>
@endforeach
</select>
but I have no idea what 'translation_of()' does, or how you store the educations on the model
@Snapey
I've tried the code from you. But still the following error message
"Parse error: syntax error, unexpected ':', expecting '('"
But still the following error message
'still' - did you have this error before?
On what line, in which file is the error?
Just a short interruption here, remember to clear cache and those temp views between your code updates. That has got me several times.
@Snapey
I've fixed the above error, but still an error like this:
"in_array() expects parameter 2 to be array, null given (View:
<select name="education[]" id="education" class="form-control sselecter input-lg" multiple="true" data-placeholder="@lang('global.Select Level Education')" required>
@foreach ($educations as $education)
<option value="{{ $education->translation_of}}"
@if(in_array($education->translation_of,old('education')))
selected="selected"
@endif >{{ $education->name }}</option>
@endforeach
</select>
So maybe this is the first time, and there is no previous old value
You can supply a default value, or you can insert the data from the user's existing record perhaps
@if(in_array($education->translation_of,old('education',[])))
here, specified empty array as the fallback.
@Snapey
When I change the code of this section as per your code like the following:
@if (in_array($education->translation_of,old('education',[])))
Still the following error message appears:
"htmlspecialchars() expects parameter 1 to be string, array given (View:
Well you need to track down the line of code causing the problem.
Without knowing what is in each variable, or what translation_of returns its impossible to guide you further.
Please sign in or create an account to participate in this conversation.