【问题标题】:Two forms on same view in Laravel 4Laravel 4中同一视图上的两个表单
【发布时间】:2016-11-29 22:31:46
【问题描述】:

我已经尝试了其他帖子中的许多解决方案,但仍然无法正常工作。

我在页面上有两个表单(视图)

{{ Form::open(array('action' => 'AdminController@shopMode')) }}
   ....// form fields

   <button type="submit" class="btn btn-primary">Change</button>
{{ Form::close() }}           
    <hr/> 
{{ Form::open(array('action' => 'AdminController@preferencesSubmit')) }}
   ....// second form fields

   <button type="submit" class="btn btn-primary">Save Changes</button>
{{ Form::close() }}

然后在我的路线中

Route::post('/admin/preferences', ['uses' => 'AdminController@preferencesSubmit', 'before' => 'csrf|admin']);
Route::post('/admin/preferences', ['uses' => 'AdminController@shopMode', 'before' => 'csrf|admin']);

当我点击提交按钮时,数据库中没有任何变化。只是页面被刷新了,即使我提交了第二个表单,我也从第一个表单收到了成功消息。

是否因为两个帖子的路由中的 url 相同?

更新:第一个表单输入字段:

<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" <?php if ($settings['preferences_shop_mode'] == 0){ ?> checked="checked" value="1"<?php }else{ ?> value="0" <?php } ?>>

在这里,我检查首选项是否 =0 以将值设置为 1 否则值 = 0。在源代码中,我看到该值为 =1,这是正确的,因为在数据库中我有 0

<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked="checked" value="1">

这是控制器

public function shopMode() {

    $preferences = Preferences::where('preferences_id', 1)->first();
    if (!$preferences) {
        App::abort(404);
    }

    Input::merge(array_map('trim', Input::all()));

    $preferences->preferences_shop_mode = Input::get('onoffswitch');          
    $preferences->save();
    return Redirect::to('/admin/preferences')->with('message', 'Shop mode changed successfully.');
} 

知道为什么没有在数据库中更新吗?

【问题讨论】:

    标签: php laravel-4


    【解决方案1】:

    路由是级联读取的。由于两条路由具有相同的路径,因此第一个优先(已找到条目,因此无需进一步查找路由)。

    你应该用不同的路径来分割它们,例如:

    Route::post('/admin/preferences/general', ['uses' => 'AdminController@preferencesSubmit', 'before' => 'csrf|admin']);
    Route::post('/admin/preferences/shop', ['uses' => 'AdminController@shopMode', 'before' => 'csrf|admin']);
    

    【讨论】:

    • 非常感谢,但第二个表单没有提交数据库中的更改。我已经更新了我的问题。请问可以查吗?
    • 嗯,这是一个完全不同的问题。复选框的行为与其他行为不同,如果未选中,则该字段不会按请求到达。请参阅this question 它会有所帮助。告诉我!
    • 但我的值总是 0 或 1。当它发布时我应该得到这个值,不是吗?
    • 现在,当您使用选中控件发帖时,您只会收到onoffswitch=1,但如果未选中,您不会收到onoffswitch=0,对吧?
    • 我得到&lt;input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" value="0"&gt; 如果我在数据库中手动更改为0 我得到&lt;input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked="checked" value="1"&gt;。我有 value="1"value="0" 取决于数据库中的值。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-24
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 2021-07-05
    • 2018-01-27
    • 2012-12-30
    相关资源
    最近更新 更多