nero's avatar
Level 1

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:

0 likes
15 replies
Snapey's avatar

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

nero's avatar
Level 1

@Snapey

If other fields are filled all does not appear error. But if only the contents of the education section just appeared error

Snapey's avatar

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?

nero's avatar
Level 1

@Snapey

I do not understand all of your answers above.

please provide examples of writing code according to my code above

36864's avatar

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.

Snapey's avatar

please provide examples of writing code according to my code above

sure. should I send you my paypal account details?

1 like
Snapey's avatar

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

nero's avatar
Level 1

@Snapey

I've tried the code from you. But still the following error message

"Parse error: syntax error, unexpected ':', expecting '('"

Snapey's avatar

But still the following error message

'still' - did you have this error before?

On what line, in which file is the error?

jlrdw's avatar

Just a short interruption here, remember to clear cache and those temp views between your code updates. That has got me several times.

nero's avatar
Level 1

@Snapey

I've fixed the above error, but still an error like this: "in_array() expects parameter 2 to be array, null given (View:

nero's avatar
Level 1
                                            <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>
Snapey's avatar
Snapey
Best Answer
Level 122

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.

nero's avatar
Level 1

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

Snapey's avatar

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 or to participate in this conversation.