dongzhi5846 2019-05-21 12:55
浏览 136

PHP Laravel如何在一个视图中使用两个表单

I'm building a Laravel-app where I have two forms in one blade-template, which appears depending on which tab is active.

It looks like this:

<div data-contact-form-id="1" class="contact-form" id="contact-company">
    <form method="POST" action="{{ route('contact.company') }}">
    // bunch of input fields here
   </form>
</div>
<div data-contact-form-id="2" class="contact-form" id="contact-private">
    <form method="POST" action="{{ route('contact.store') }}">
    // bunch of input fields here
   </form>
</div>

then my web.php

Route::post('contact', 'ContactController@store')->name('contact.store');
Route::post('contact/company', 'ContactController@company')->name('contact.company');

but I can't submit the "company-contact" form, and when I try to do remove the slash in the route I get an error route.store is not defined:

Route::post('contact', 'ContactController@store')->name('contact.store');
Route::post('contact', 'ContactController@company')->name('contact.company');

Why is this and how can I solve this?

  • 写回答

1条回答 默认 最新

  • dongyupen6269 2019-05-26 13:05
    关注

    put all input fields in a single form and submit to the store route.

    In controller:

    Here I used the company model & some fields just for example you can replace with your model & your field name

    public function store(Request $request){
      $post = $request->all();
    
      Company::create([
          'company_name' => $post['company_name']
          'company_email' => $post['company_email'],
          'company_phone' => $post['company_phone']
      ]);
    
       // another model for contact
       Contact::create([
         'name' => $post['name'],
         'email' => $post['email'],
         'phone' => $post['phone'],
       ]);
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看