Kavi Harjani
Kavi Harjani

Reputation: 679

form not being submitted laravel

I am building a basic form in laravel 5.4.22 but it is not being submitted, when i click on submit it stays on the same page just changing the url to something different I used this Route

    Route::post('/contact/submit', 'MessagesController@submit');

This is the controller i made use of

   `<?php

      namespace App\Http\Controllers;

       use Illuminate\Http\Request;

       class MessagesController extends Controller
    {
        public function submit(Request $request){
         this->validate($request, [
              'name' => 'required',
              'email' =>'required'
               ]);
                 return 'SUCCESS';
                    }
                   }; ` 

& here is my contact form

            @extends('layouts.app')

             @section('content')
            <H1>Contact</H1>
{!! Form::open(['action'=>'MessagesController@submit','method'=>'post']) !!}
          <div class="form-group">
            {{Form::label('name', 'Name')}}
            {{Form::text('name', '',  ['class' => 'awesome form-control', 'placeholder' => 'Enter name'] )}}
             </div>
          <div class="form-group">
            {{Form::label('email', 'E-Mail Address')}}
        {{Form::text('email', '',  ['class' => 'awesome form-control', 'placeholder' => 'Enter Email id'])}}
         </div>
        <div class="form-group">
          {{Form::label('messages', 'Message')}}
        {{Form::textarea('message', '', ['class' => 'form-control', 'placeholder' => 'Enter your message here'])}}
        </div>
         <div>
        {{Form::submit('Submit', ['class'=> 'btn btn-primary'])}}
         </div>
        {!! Form::close() !!}
      @endsection

,This is the main page where i connect the other things

      <!DOCTYPE html>
    <html>
    <head>
     <title>Dradz</title>
     <link rel="stylesheet" type="text/css" href="/css/app.css">
    </head>
    <body>

    @include('inc.navbar') 
    <div class="container">
      @if(Request::is('/'))
      @include('inc.showcase')
      @endif
      <div class="row">
        <div class="col-md-8 col-lg-8">
       @include('inc.messages')
      @yield('content')
   </div>
       <div class="col-md-4 col-lg-4">
            @include('inc.sidebar')
           @show
         </div>
    </div>
   </div>
 <footer id="footer" class="text-center">
        <p>Copyright 2017 &copy; Dradz</p>
       </footer>
      </body>
         </html>    

Upvotes: 1

Views: 83

Answers (2)

Andriy Lozynskiy
Andriy Lozynskiy

Reputation: 2604

Try to add slash to the beginnig of the url and set method to 'POST'

{!! Form::open(['url' => '/contact/submit', 'method'=>'POST']) !!}

Also I would recommend you to give names for all your routes like:

Route::post('/contact/submit', 'MessagesController@submit')->name('messages.submit');

and then you can use it in your form:

{!! Form::open(['route' => 'messages.submit', 'method'=>'POST']) !!}

Upvotes: 1

Alireza Amrollahi
Alireza Amrollahi

Reputation: 915

First, always use REQUEST file for validation it's best practice by far . Second instead of this :

{!! Form::open(['url' => 'contact/submit']) !!}

use something like :

    {!! Form::open(['action'=>'Yourcontroller@youraction','method'=>'post']) !!}

Upvotes: 1

Related Questions