S.I.
S.I.

Reputation: 3375

Routes problem in Laravel and Page not found

I have contact form in pop-up but when I click on send button I've got page not found instead of redirect me to the home page.

This is my route

Route::post('/contact_us','HomeController@contact_us')->name('contact_us');

The function in HomeController.php

public function contact_us(Request $request)
{ 
    $validator=Validator::make($request->all(), [
        'name' => 'required',
        'phone' => 'required',
        'email' => 'required|email'
    ]);

    if($validator->fails())
    {

      Session::flash('error', "join_us");
      return back()->withInput()->withErrors($validator, 'contact');
    }
    $data = array(
        'name' => $request->name,
        'email'  => $request->email,
        'phone'  => $request->phone,
        'created_at' => date('Y-m-d h:i:s'),
        ); 
    $email=$request->email;

     DB::table('application_from')->insert($data); 

    return Redirect::to('/')->with('message', 'Application added successfuly');
}    

And the form opened and close tags are

{{Form::open(array('route'=>'contact_us','method'=>'post'))}}

..... form inputs

{{Form::close()}}

When I click Submit I've got

Not Found

The requested URL /contact_us was not found on this server.

Why is trying to load this URL when it should reload the home page?

UPDATE with the form

<div class="modal-body">
    {{Form::open(array('route'=>'contact_us','method'=>'post'))}}
         <div class="form-group {{ $errors->contact->has('name') ? 'has-error' : '' }}">
              @if ($errors->contact->has('name'))
                  <span class="small text-danger ">
                      <b>{{ $errors->contact->first('name') }}</b>
                  </span>
              @endif
              <input type="text" name="name" class="form-control" placeholder="Name" >
         </div>
         <div class="form-group {{ $errors->contact->has('email') ? 'has-error' : '' }}">
              @if ($errors->contact->has('email'))
                  <span class="small text-danger ">
                     <b>{{ $errors->contact->first('email') }}</b>
                  </span>
              @endif
              <input type="text" name="email" class="form-control" placeholder="Email" >
         </div>
         <div class="form-group {{ $errors->contact->has('phone') ? 'has-error' : '' }}">
              @if ($errors->contact->has('phone'))
                   <span class="small text-danger ">
                        <b>{{ $errors->contact->first('phone') }}</b>
                   </span>
              @endif
              <input type="text" name="phone" class="form-control" placeholder="Phone" >
         </div>
         <div class="form-group text-center">
              <button type="submit" class="btn btn-custom btn-sm btn-block">Submit</button>
         </div> 
         {{Form::close()}}
 </div>

UPDATE 2: send mail function

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

Upvotes: 4

Views: 404

Answers (3)

user6940918
user6940918

Reputation:

To answer your email problem

try to add the following in your .env file

# For Localhost Email
MAIL_DRIVER=smtp
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=587
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=tls

# For Hosting Email
MAIL_DRIVER=sendmail
MAIL_HOST=smtp.googlemail.com
MAIL_PORT=465
[email protected]
MAIL_PASSWORD=mypassword
MAIL_ENCRYPTION=ssl

these credentials are for gmail-based email, don't forget to do these steps:

  1. Go to your Google Account.
  2. On the left navigation panel, click Security.
  3. On the bottom of the page, in the Less secure app access panel, click Turn on access.

Upvotes: 1

ManojKiran
ManojKiran

Reputation: 6341

Please share the complete code of forms

i have checked your function and form and routes there is no bugs

I think that the data from the form is staring in the database but the redirecting after the store is throwing error

I am not daam sure but may be

So Try Changing the

FROM

 return Redirect::to('/')->with('message', 'Application added successfuly');

TO

return redirect()->back()->with('message','Application added successfuly');

And also FROM

{{Form::open(array('route'=>'contact_us','method'=>'post'))}}

TO

  {!! Form::open(['route' => ['contact_us']]) !!}
  @method('POST')

Upvotes: 1

Kamal Paliwal
Kamal Paliwal

Reputation: 1301

Seems you are missing the csrf-token in the form. Add the csrf field in the form as below:

<input type="hidden" name="_token" id="csrf-token" value="{{ Session::token() }}" />

OR

{!! Form::token() !!}

Hope, this will resolve your issue.

Upvotes: 1

Related Questions