Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Patwan's avatar

Sending Checkbox values to email in Laravel 5.4

I have created a form in Laravel 5.4 containing checkbox fields. I am validating on the front-end using parsley.js then on the backend. I want to send the checked values from the checkbox to my email but it produces an error

ErrorException htmlspecialchars() expects parameter 1 to be string, array given(View: C:\XAMPP\htdocs\Joswan\resources\views\emails\contact.blade.php)

Please assist ?

contact.blade.php

<label name="name"> Full Name: </label>
<input type="text" name="name" placeholder="Full Name..." class="input" required> <br> <br>

<label name="name"> Phone Number: </label>
<input type="text" name="phone" placeholder="Phone Number..." class="input" required> <br> <br>

<label name="name"> Email Address: </label>
<input type="email" name="email" placeholder="Email Address..." class="input" required> <br> <br>

<label name="name"> Service: </label> <br>
<input type="checkbox" name="checkbox[]" value="Website Design" > Website Development (Blog or E-Commerce)<br>
<input type="checkbox" name="checkbox[]" value="School Management System" > School Management Systems  <br> 
<input type="checkbox" name="checkbox" value="Graphics Design" > Graphics Design (3D, Wordmark logos)<br> 
<input type="checkbox" name="checkbox[]" value="Digital Marketing" > Digital Marketing (SEO & Social Media) <br>
<input type="checkbox" name="checkbox[]" value="Article/Content writing" > Article/Content writing <br> <br>

<label name="name"> Message: </label> <br/>
<textarea placeholder="Type your Message here ..." name="message" class="input form-control" rows="5" required> </textarea>

<br><br>

<input type="submit" value="Send Message" class="submit">

Controller

    //Contact form in index page and contact page
    public function postContact(Request $request){
        $this->validate($request, [
                'name' => 'required',
                'phone' => 'required',
                'email' => 'required|email',
                'message' => 'required|min:20',
                'checkbox' => 'required']);

        $data = array(
                'name' => $request->name,
                'phone' => $request->phone,
                'email' => $request ->email,
                'checkbox' => $request ->checkbox,
                'bodyMessage' => $request->message
            );

        Mail::send('emails.contact', $data, function($message) use ($data){
            $message->from($data['email']);
            $message->to('info@*************');
        });


        return redirect()->back();

    }

email.contact.blade.php

<h3> Contact Form </h3>

<div>
    <b>Name: </b> {{ $name }} <br> <br>
    <b>Phone: </b> {{ $phone }} <br> <br>
    <b>Email Address: </b> {{ $email }} <br> <br>
    <b>Service: </b> {{ $checkbox }} <br> <br>
    <b>Message:</b> {{ $bodyMessage }} 
</div>
0 likes
9 replies
rob897's avatar

You are setting your checkbox as an array by using these

<input type="checkbox" name="checkbox[]" value="Website Design" > Website Development (Blog or E-Commerce)<br>
<input type="checkbox" name="checkbox[]" value="School Management System" > School Management Systems  <br> 
<input type="checkbox" name="checkbox" value="Graphics Design" > Graphics Design (3D, Wordmark logos)<br> 
<input type="checkbox" name="checkbox[]" value="Digital Marketing" > Digital Marketing (SEO & Social Media) <br>
<input type="checkbox" name="checkbox[]" value="Article/Content writing" > Article/Content writing <br> <br>

The Graphics Design one is missing the [] as well.

So you will need to do a foreach loop in your blade file

<b>Service: </b> 
@foreach($checkbox as $service)
    {{ $service}}
@endforeach 

You will have to figure out the formatting

1 like
Patwan's avatar

@rob897 Hello,,, Much appreciations for your help on the syntax error I have rectified it,,,would you mind helping me out on the checkbox code in the controller cause am not sure if it is the right way to parse the checked checkbox to be sent via email... All the other input fields work fine but when I add the checkbox an error is displayed after submitting..

~ Regards

rob897's avatar

In your controller can you copy the output if you dd() the request?

public function postContact(Request $request){
    dd($request);

        $this->validate($request, [
                'name' => 'required',
                'phone' => 'required',
                'email' => 'required|email',
                'message' => 'required|min:20',
                'checkbox' => 'required']);

        $data = array(
                'name' => $request->name,
                'phone' => $request->phone,
                'email' => $request ->email,
                'checkbox' => $request ->checkbox,
                'bodyMessage' => $request->message
            );

        Mail::send('emails.contact', $data, function($message) use ($data){
            $message->from($data['email']);
            $message->to('info@*************');
        });


        return redirect()->back();

    }
1 like
Patwan's avatar
Request {#38 ▼
  #json: null
  #convertedFiles: null
  #userResolver: Closure {#175 ▶}
  #routeResolver: Closure {#173 ▶}
  +attributes: ParameterBag {#40 ▶}
  +request: ParameterBag {#39 ▶}
  +query: ParameterBag {#46 ▶}
  +server: ServerBag {#42 ▶}
  +files: FileBag {#43 ▶}
  +cookies: ParameterBag {#41 ▶}
  +headers: HeaderBag {#44 ▶}
  #content: null
  #languages: null
  #charsets: null
  #encodings: null
  #acceptableContentTypes: null
  #pathInfo: "/contact"
  #requestUri: "/PwebBlog/public/contact"
  #baseUrl: "/PwebBlog/public"
  #basePath: null
  #method: "POST"
  #format: null
  #session: Store {#214 ▶}
  #locale: null
  #defaultLocale: "en"
  -isHostValid: true
  -isClientIpsValid: true
  -isForwardedValid: true
  basePath: "/PwebBlog/public"
  format: "html"
}
rob897's avatar

ok if you expand out the request you can drill down into the values that were submitted. Guessing you will have an array of checkboxes in there.

Patwan's avatar

Thanks duly noted ~ The checkbox values are there,,,, how do I pass them to be send to email ?

rob897's avatar
rob897
Best Answer
Level 30

did you try

<h3> Contact Form </h3>

<div>
    <b>Name: </b> {{ $name }} <br> <br>
    <b>Phone: </b> {{ $phone }} <br> <br>
    <b>Email Address: </b> {{ $email }} <br> <br>
    <b>Service: </b> 
    @foreach($checkbox as $service)
            {{ $service}}
    @endforeach 
    <br> <br>
    <b>Message:</b> {{ $bodyMessage }} 
</div>
2 likes
Snapey's avatar

you need to foreach on them but use keys to get the value

 @foreach($checkbox as $key => $value)

- {{ $key}}

@endforeach 
1 like
Patwan's avatar

@rob897 ... I had some internet connectivity hurdle but the form works well thanks alot...

Please or to participate in this conversation.