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

zarcoder's avatar

Reservation Form to be Emailed

I created a contact form that emails the details to the client. The email is sent successfully and all validation with error messages work. I'm also creating a reservation form which should essentially do the same as the contact form. I've setup my reservation routes, views and controller but the email is not sent to mailtrap.io. I'm using the same controller for both the contact and reservation form. Not sure if that is maybe an issue?

My web.php file

Route::get('reservation', 'PageController@reservation');

Route::post('reservation', 'PageController@postReservation');

Route::get('contact', 'PageController@contact');

Route::post('contact', 'PageController@postContact');

My PageController

class PageController extends Controller
{
    
    public function reservation()
    {
        return view('reservation');
    }

    public function postReservation(Request $request)
    {
        //dd($request->all()); -> Fill in data to test if form works
        $request->validate([
            'name' => 'required|string|min:2',
            'email' => 'required|email',
            'phone' => 'required|digits:10',
            'date' => 'required|date_format:Y/m/d|after:today',
            'seats' => 'required|integer',
            'message' => 'min:10'
        ]);

        $data = array (
            'name' => $request->name,
            'email' => $request->email,
            'phone' => $request->phone,
            'date' => $request->date,
            'time' => $request->time,
            'seats' => $request->seats,
            'reservationMessage' => $request->message,
        );

        Mail::send('emails.reservation', $data, function ($message) use ($data) {
            $message->from('[email protected]');
            $message->to('[email protected]');
            $message->subject($data['name']);
        });

        return redirect('reservation')->with('success', 'Thank you. We will contact you to confirm the booking');
    }

    public function contact()
    {
        return view ('contact');
    }

    public function postContact(Request $request)
    {
        //dd($request->all()); -> Fill in data to test if form works
        $request->validate([
            'name' => 'required|string|min:2',
            'email' => 'required|email',
            'number' => 'required|digits:10',
            'subject' => 'required|min:3',
            'message' => 'required|min:10'
        ]);

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

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

        return redirect('contact')->with('success', 'Your message has been sent. We will reply promptly');
    }
}

My Reservation Form

<form method="post" id="reservation-form" action={{ action('PageController@postReservation') }}>
                            {{ @csrf_field() }}
                            <div class="row">
                                <div class="col-md-4">
                                    <label>Name*</label>
                                    <p><input type="text" name="name" class="reservation-fields" required/></p>
                                </div>
                                <div class="col-md-4">
                                    <label>Email*</label>
                                    <p><input type="text" name="email" class="reservation-fields" required/></p>
                                </div>
                                <div class="col-md-4">
                                    <label>Phone*</label>
                                    <p><input type="text" name="phone" class="reservation-fields" required/></p>
                                </div>
                            </div>
                            <!--end row-->
                            <div class="row">
                                <div class="col-md-4">
                                    <label>Date*</label>
                                    <p><input type="date" name="datepicker" id="datepicker" class="reservation-fields" size="30" required/></p>
                                </div>
                                <div class="col-md-4">
                                    <label>Time*</label>
                                    <p>
                                        <select name="time" class="reservation-fields" >
                                            <option value="10:00">10:00</option>
                                            <option value="11:00">11:00</option>
                                            <option value="12:00">12:00</option>
                                            <option value="13:00">13:00</option>
                                            <option value="14:00">14:00</option>
                                            <option value="15:00">15:00</option>
                                            <option value="16:00">16:00</option>
                                        </select>
                                    </p>
                                </div>
                                <div class="col-md-4">
                                    <label>Seats*</label>
                                    <p><input type="text" name="seats" class="reservation-fields" required/></p>
                                </div>
                            </div>
                            <!--end row-->
                            <label>Special Requests</label>
                            <p> <textarea name="message" id="message2" class="reservation-fields" cols="100" rows="4" tabindex="4"></textarea></p>
                            <p class="antispam">Leave this empty: <input type="text" name="url" /></p>
                            <p class="alignc"><input type="submit" value="Book Now" id="submit" /></p>
                        </form>

My Reservation Email Template


<p>Name: {{$name}}</p>

<p>Email: {{$email}}</p>

<p>Tel Nr: {{$phone}}</p>

<p>Date: {{$date}}</p>

<p>Time: {{$time}}</p>

<p>Seats: {{$seats}}</p>

<p>Message: {{$reservationMessage}}</p>
0 likes
9 replies
bobbybouwmann's avatar

Is the data posted to the controller? I see you commented out the dd call.

zarcoder's avatar

Yes, when I dd the data, I can see it

array:9 [▼
  "_token" => "obsuu81ZQ3RuuawYfiM9T3vNggtKkWYzypaOYq7Y"
  "name" => "JP"
  "email" => "[email protected]"
  "phone" => "0741112225"
  "datepicker" => "2018-08-19"
  "time" => "10:00"
  "seats" => "12"
  "message" => "Test Message"
  "url" => null
]
zarcoder's avatar

Any ideas @bobbybouwmann, @Snapey, @Cronix on how to get my reservation form to email the details? Like, I said, when I dd the form data, I can view the array output.

PS - I know that the $message->from and $message->to in the controller should ideally have the same email address. I'm just using a different ->to address for testing purposes.

bobbybouwmann's avatar

Please stop tagging people! If they have time to help you they will drop by in the thread. Maybe they don't know the answer either and then you have wasted there time!

Anyway, so you see any information in your log files?

zarcoder's avatar

Oh, apologies - I didn't know tagging was unacceptable. I'll have to check the log files then.

Snapey's avatar

you actually also stop others answering. They look at it and take offense that you want answers from specific people.

1 like
Snapey's avatar

If your other mail code works there can't be much wrong.

dd($data) inside the mail callback to check it actually hits that point and that the data is correct

1 like
zarcoder's avatar

Thanks for the feedback. I still don't have a solution but will keep digging and post my answer once it's working

zarcoder's avatar

When I commented out the following code in the postReservation controller function and and did a dd right after that, the mail went through.

$request->validate([
            'name' => 'required|string|min:2',
            'email' => 'required|email',
            'phone' => 'required|digits:10',
            'date' => 'required|date_format:Y/m/d|after:today',
            'seats' => 'required|integer',
            'message' => 'min:10'
        ]);

I added

@if ($errors->any())
    <div class="alert alert-danger">
        <ul>
            @foreach ($errors->all() as $error)
                <li>{{ $error }}</li>
            @endforeach
        </ul>
    </div>
@endif

to my form and realised that the date format was incorrect. My validation rules stated Y/m/d and it should have been Y-m-d. I fixed that and now its working.

Thanks for the assistance and apologies again for the hassle.

Please or to participate in this conversation.