beths's avatar
Level 1

Submit Button is Doing Nothing

Hello, I have been trying to determine why the submit button in my HTML form is not working. When I click on it I get no errors in the console and no exceptions. It does nothing. I have tried many of the suggestions on Laracasts and StackOverflow, but have found nothing that works. It may be something very simple and if so, I apologize. I am still fairly new at Laravel.

I have tried to name my Route::post('/posts/store', 'PostController@store'), but that did not work. I have tried the code below, but nothing is working.

action="{{ action('PostController@store') }}" action="{{ route('posts/store') }}" action="{{ route('posts.store') }}" (This was after giving it a name of posts.store) action="{{ URL('posts/store') }}"

Laravel 5.7 PHP 7.2.10 Ubuntu 18.04

create.blade.php

<div class="container">
  <div class="row">
    <div class="col-lg-3"></div>
    <div class="col-md-9">
      <h1>Enter New Blog Post</h1>
      <hr>
      <div class="form-group">
        <form method="POST" action="{{ action('PostController@store') }}">
            <label for="title">Title</label>
            <input class="form-control" id="title" name='title' type="text"></input>
          </div>
          <div class="form-group">
            <label for="post">Blog Post</label>
            <textarea class="form-control" id="post" name='post' ></textarea>
          </div>
            <button type="submit" class="btn btn-secondary btn-md m-1">Publish New Post</button>
        </form>
      </div>
    </div>
  </div>
</div>
@endsection```

PostController
```public function store(Request $request)
    {

        // validate the data
      $validatedData = $request ->validate([
          'title' => 'required | unique:posts | max:255',
          'post' => 'required'
        ]);

        // store in database
        $post -> title = $request -> input('title');
        $post -> post = $request -> input('post');
        $post->save();

        Session::flash('success', 'The blog post was saved successfully!');

        // redirect to another
        return redirect()->route('posts.show', $post ->id);
       
    }```

web.php
```Route::get('/posts/show', 'PostController@show');
Route::resource('/posts', 'PostController');
Route::post('/posts/store', 'PostController@store');
Route::post('/posts/create', 'PostController@create');```

PagesController
       ``` public function store() {
          return view('posts/store');
    }
        public function create() {
          return view('posts/create');
    }
        public function show(){
          return view('posts/show');
    }```

0 likes
4 replies
beths's avatar
Level 1

Ignore the ```marks. Those are not in the original code. I was trying to set my code up to display correctly in the question.

michaelnguyen547's avatar

you have html tag messed up. The submit button ends up outside the tag. The correct code should be

<form method="POST" action="{{ action('PostController@store') }}">
    <div class="form-group">
        <label for="title">Title</label>
        <input class="form-control" id="title" name='title' type="text"></input>
    </div>

    <div class="form-group">
        <label for="post">Blog Post</label>
        <textarea class="form-control" id="post" name='post' ></textarea>
    </div>
    <button type="submit" class="btn btn-secondary btn-md m-1">Publish New Post</button>
</form>
Cronix's avatar

The ```'s need to be on their own lines with nothing else.

You're missing the csrf token.

if you do a dd($request->all()); as the first thing in your store method, does it show the submitted values?

beths's avatar
Level 1

@michaelnguyen547 I corrected the html tags and that worked. Thank you.

@Cronix I have now included the @csrf in my form. After doing the dd, there is input showing up now.

Thank you both so much! This issue seems to have been corrected. I am now having an issue connecting to the correct database, but I should be able to figure that out. Thanks again!

Please or to participate in this conversation.