简体   繁体   中英

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?

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

Add the action attribute to the form

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

And in your controller the return redirect('/backend') redirects the form back to the same page.

Create a get route for the backend or redirect to another route

You forgot to specify route to your form. Add the following:

<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" action="{{URL:to('/backend')}}">
          <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 redirect to different location on your controller.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM