Nida Akram
Nida Akram

Reputation: 362

htmlspecialchars() expects parameter 1 to be string array given

I have an input field in which I am passing an array from my controller as hidden input but its giving me this error of array given.

Code of my view is

    <input type="hidden" name="DiseaseDiagnosed[]" value="{{$DiseaseDiagnosed}}">

Code of controller passing the value to view is

 return view('/doctorPanel/diagnoseDisease', ['chart' => $chart, 'patient' => $patient, 'symptomsStated' => $symptomsStated, 'DiseaseDiagnosed' => $DiseaseDiagnosed]);

Kindly tell me why I am getting this error

Upvotes: 4

Views: 53858

Answers (3)

Pol Lluis
Pol Lluis

Reputation: 1162

In order to create an array with inputs you need to have 1 input for each value inside the array. You are appending an array on the value when it only accepts Strings so thats why it warns you that an array was given when a String was expected.

As @Adnan suggested you can solve this using:

@foreach($DiseaseDiagnosed as $disease)

   <input type="hidden" name="DiseaseDiagnosed[]" value="{{ $disease }}">

@endforeach

Then in your controller you will recieve the array DiseaseDiagnosed with all the values you inserted, eg: You will recieve all the values within the same array->

dd($request->DiseaseDiagnosed);
// You will see this is an array with all the values

Upvotes: 2

Jobayer
Jobayer

Reputation: 1231

Blade Template engine is producing this error. you can't print array like this using {{ }}. When passing this value, you can encode it using 'DiseaseDiagnosed'=>json_encode($DiseaseDiagnosed]), then you can use that syntax. After that when you want to use this value, don't forget to decode it using json_decode()

Upvotes: 3

FULL STACK DEV
FULL STACK DEV

Reputation: 15941

<input type="hidden" name="DiseaseDiagnosed[]" value="{!! jsond_encode($DiseaseDiagnosed) !!}">

Actually, your input is DiseaseDiagnosed is an array which is returned to view.

So you have to use {{ json_decode($DiseaseDiagnosed) }}

You can also try

@foreach($DiseaseDiagnosed as $disease)

  <input type="hidden" name="DiseaseDiagnosed[]" value="{{ $disease }}">

@endforeach

Upvotes: 3

Related Questions