发布 Laravel 表单不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21556046/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Posting a Laravel form is not working
提问by Relaxing In Cyprus
I am setting up a simple form in laravel:
我在 Laravel 中设置了一个简单的表单:
This is the route file:
这是路由文件:
Route::get('backoffice/upload', [ 'as' => 'backoffice/upload',
'uses' => 'UploadController@uploadForm']);
Route::post('backoffice/saveimage',[ 'as' => 'backoffice/saveimage',
'uses' => 'UploadController@saveImage']);
This is the controller:
这是控制器:
class UploadController extends \BaseController
{
public function uploadForm()
{
return View::make("backoffice.upload.create");
}
public function saveImage()
{
return "Uploading...";
}
}
And this is the View file:
这是视图文件:
<h1>Upload Image</h1>
{{ Form::open(['action' => 'UploadController@saveImage']) }}
<div class='formfield'>
{{ Form::label('newfilename','New File Name (optional):') }}
{{ Form::input('text','newfilename') }}
{{ $errors->first('newfilename') }}
</div>
<div class='formfield'>
{{ Form::submit($action,['class'=>'button']) }}
{{ Form::btnLink('Cancel',URL::previous(),['class'=>'button']) }}
</div>
{{ Form::close() }}
// Generated HTML
// 生成的 HTML
<h1>Upload Image</h1>
<form method="POST" action="http://my.local/backoffice/saveimage" accept-charset="UTF-8"><input name="_token" type="hidden" value="x9g4SW2R7t9kia2B8HRJTm1jbLRl3BB8sPMwvgAM">
<div class='formfield'>
<label for="newfilename">New File Name (optional):</label>
<input name="newfilename" type="text" id="newfilename">
</div>
<div class='formfield'>
<input class="button" type="submit" value="Create">
</div>
</form>
So, if I go to: http://my.local/backoffice/uploadI get the form with the HTML above.
所以,如果我去:http://my.local/backoffice/upload,我会得到上面带有 HTML 的表单。
However, if I type anything, then click SUBMIT, I return to the form but now have the following URL:
但是,如果我输入任何内容,然后单击提交,我将返回到表单,但现在具有以下 URL:
http://my.local/backoffice/upload?pz_session=x9g4SW2R7t9kia2B8HRJTm1jbLRl3BB8sPMwvgAM&_token=x9g4SW2R7t9kia2B8HRJTm1jbLRl3BB8sPMwvgAM&newfilename=ddd
This makes no sense to me. Up until now I have always used route::resource when dealing with forms, and had no problem. I am trying to do a simple form with GET and POST and am having no end of grief. What am I missing?
这对我来说毫无意义。到目前为止,我在处理表单时一直使用 route::resource,并且没有问题。我正在尝试用 GET 和 POST 做一个简单的表单,而且我的悲伤没有尽头。我错过了什么?
Furthermore, if I modify routes.php and change it from post to any, then open a browser window and type: http://my.local/backoffice/saveimagethen I get the message "Uploading..." so that part is working ok.
此外,如果我修改 routes.php 并将其从 post 更改为 any,然后打开浏览器窗口并键入:http://my.local/backoffice/saveimage然后我收到消息“正在上传...”,因此该部分是工作正常。
回答by Relaxing In Cyprus
Found the solution. In making the backoffice of the system, I had re-used the frontoffice template but removed all the excess. Or so I had thought. However, the front office header template had a form which I had only partially deleted.
找到了解决办法。在制作系统的后台时,我重新使用了前台模板,但删除了所有多余的部分。或者我是这么想的。但是,前台标题模板有一个我只删除了部分的表单。
So the problem was that there was an opening FORM tag I didn't know about. Consequently, when I clicked on submit to my form, it was actually submitting to this other form.
所以问题是有一个我不知道的开放 FORM 标签。因此,当我点击提交到我的表单时,它实际上是提交到另一个表单。
As the other form had no action it was default to itself.
由于另一种形式没有动作,它是默认的。
Of course, had I just validated the HTML this would have shown up straight away. The lesson learned here is to validate my html before submitting questions!
当然,如果我刚刚验证了 HTML,这将立即显示出来。这里学到的教训是在提交问题之前验证我的 html!
回答by Abhinandan N.M.
Try this, and be sure to correctly configure your url at app/config/app.php
试试这个,并确保正确配置您的网址 app/config/app.php
{{Form::open(['url'=>'backoffice/saveimage'])}}
//code
{{Form::close()}}