将JavaScript添加到PHP表单会引发Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

时间:2014-08-14 19:46:21

标签: javascript php forms laravel

我必须为类作业制作一个php表格。然后我们不得不使用另一种语言来改变形式,使其更具功能性。

我决定添加JavaScript,以便用户可以添加多个部分。我添加JavaScript后,我现在收到此错误:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

以下是表格:

{{ Form::open() }}
@for ($i=0; $i < 10; $i++)
<input type="number" name="part_number" placeholder="Part Number" />
<input type="number" name="quantity" placeholder="Quantity" />
<input type="number" name="annual_usage" placeholder="Annual Usage" />
<input type="submit" value="Add Part" />
@endfor
<input type="textarea" name="comment" placeholder="Comment" />
<input type="text" name="shippingaddress" placeholder="Shipping Address" />
<input type="text" name="project_id" placeholder="Project Id" />
<input type="text" name="user_id" placeholder="User Id" />
<input type="submit" value="Send Requests" />
{{ Form::close() }}

2 个答案:

答案 0 :(得分:0)

我没有在您的表单中看到任何javascript,但无论单独使用Javascript都不会导致此问题。这个错误说,&#34;你正试图用不正确的HTTP方法命中路由&#34;基本上,您尝试POST到只允许GET访问的路由。

默认情况下,Laravel表单将使用POST。您可以尝试将表单上的方法更改为GET:

{{ Form::open([ 'method' => 'GET' ]) }}

但实际上它应该是您在app/routes.php文件中为您尝试访问的路线定义的任何内容。

顺便说一下,当表单实际提交时,这些输入会覆盖另一个,因为在for循环中重复时它们会有名称冲突:

<input type="number" name="part_number" placeholder="Part Number" />
<input type="number" name="quantity" placeholder="Quantity" />
<input type="number" name="annual_usage" placeholder="Annual Usage" />

考虑让他们提交一个数组:

<input type="number" name="part_number[]" placeholder="Part Number" />
<input type="number" name="quantity[]" placeholder="Quantity" />
<input type="number" name="annual_usage[]" placeholder="Annual Usage" />

答案 1 :(得分:0)

JavaScript无关,Laravel默认情况下使用POST方法提交表单,并在您的应用程序中声明路由时发生MethodNotAllowedHttpException (基本上在routes.php文件中)使用一种方法(GET,POST,DELETE等),但使用不同的方法提交表单。

确保您提交表单的路线和表单中使用的方法相同。如果您没有明确提及表单中的方法,则默认情况下它将为POST,您可以使用不同的方法使用以下内容:

{{ Form::open( array( 'action' => 'Controller@method', 'method' => 'GET') ) }}

或类似的东西:

{{ Form::open( array( 'route' => 'routename', 'method' => 'GET') ) }}

或类似的东西:

{{ Form::open( array( 'url' => 'your/url', 'method' => 'GET') ) }}

更清楚的是,如果您使用以下内容声明了路线:

Route::get(...);

然后在表单中使用GET方法,或者如果其他方法使用该方法,但如果您使用post(),那么您不需要在表单中使用任何方法。查看Laravel website上的详细信息。

你的循环也是不正确的,你的输入有相同的名字,所以最后一个会覆盖以前的名字而你每个名字只能得到一个输入,在名字中使用数组表示法。