[英]Form is not being sent - laravel
我正在尝试通过填写表格将公告添加到数据库中...
这是我的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>
这是我的路线: Route::post('/backend', 'BackendController@store');
这是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');
}
}
当我按POST按钮(类型为“ submit”)时,页面变为空白,空白,什么也没发生。
另外,表单是模态的,当我打开模态时,我得到... / backend#modal而不是... / backend,这可能也是个问题吗?
首先,我没有在form标签中看到action属性,应该是这样的
<form method="POST" action="/backend">
而且,听起来您的laravel控制器无法处理您的请求,
$request->has('newAnnPost')
该请求将不包含newAnnPost,但它将包含其他2个输入标签而不是“提交”按钮,据我所知,在提交表单时,该请求未序列化为表单输入
将action
属性添加到表单
<form method="POST" action="/backend">
在您的控制器中, return redirect('/backend')
将表单重定向回同一页面。
为backend
创建一条获取路由或重定向到另一route
您忘记了指定到表格的路线。 添加以下内容:
<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>
并重定向到控制器上的其他位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.