Search code examples
phplaravellaravel-5.3

Form is not being sent - laravel


I am trying to add an announcement into the database by completing a form...

Here is my HTML:

<div class="remodal" data-remodal-id="modal" data-remodal-options="closeOnOutsideClick: false">
<button data-remodal-action="close" class="remodal-close"></button>
<h1>Post a new Announcement</h1>
<hr />
    <form method="POST">
          <label><h4>Subject</h4>
            <input name="newsTitle" type="text" placeholder="Announcement title">
          </label>
          <label><h4>Body</h4>
            <textarea name="newsBody" placeholder="Write the announcement message here"></textarea>
          </label>
          <input type="hidden" name="_token" value="{{ csrf_token() }}">

          <br>
        <button data-remodal-action="cancel" class="remodal-cancel">Cancel</button>
        <button type="submit" name="newAnnPost" class="remodal-confirm">POST</button>
    </form>
</div>

and here is my route: Route::post('/backend', 'BackendController@store');

and here is the BackendController:

public function store(Request $request)
{
    if($request->has('newAnnPost')){
        $insertNews = new News;

        $insertNews->subject = $request->newsTitle;
        $insertNews->msg = $request->newsBody;
        $insertNews->author = Auth::user()->name;
        $insertNews->AuthorID = Auth::user()->id;

        $insertNews->save();

        return redirect('/backend');
    }
}

When I press on the POST button, which has type="submit", the page gets empty, white and nothing happens.

Also the form is in a modal and when I open the modal I get .../backend#modal instead of just being .../backend could that also be an issue?


Solution

  • as a start, I don't see an action attribute in the form tag, it should have been something like this

    <form method="POST" action="/backend">
    

    Also, sounds like the your laravel controller won't process your request,

    $request->has('newAnnPost')
    

    the request won't contain the newAnnPost but it will contain the other 2 input tags not the submit button, as far as I know, its not serialized as a form input when the form is being submitted