duancan8382 2016-04-30 03:23
浏览 184
已采纳

Php laravel使用post方法通过控制器

am try to send value form from by post method to controller

here is my view,and how can i use post method to send

 <form class="form-horizontal" role="form">
    <div class="form-group">
      <label class="col-lg-3 control-label">Title:</label>
      <div class="col-lg-8">
        <input class="form-control" value='{{ $words->first()->title }}' type="text">
      </div>
    </div>
    <div class="form-group">
      <label class="col-lg-3 control-label">Meaning:</label>
      <div class="col-lg-8">
        <input class="form-control" value="{{ $words->first()->meaning }}" type="text">
      </div>
    </div>

    <div class="form-group">
      <label class="col-md-3 control-label"></label>
      <div class="col-md-8">
        <input class="btn btn-primary" value="Save Changes" type="button">
        <span></span>
        <input class="btn btn-default" value="Cancel" type="reset">
      </div>
    </div>
  </form>

here is controller method like

public function postSaveedit($meaning){


}

using route by controller

  • 写回答

3条回答 默认 最新

  • dsgw3315 2016-04-30 11:46
    关注

    In Laravel 5.2 you can use request() helper method to solve your problem... This is how you can do it...

    Routes file should look like this (be sure that this route should be of post type)

    Route::post('/myurl', 'Controllername@postSaveEdit')->name('postSaveEdit');
    

    Form file should look like this, also please specify the input field names in the form so that you can grab them in the the controller by their specified names (like - title, meaning - see below code)...

    <form class="form-horizontal" action="{{ route('postSaveEdit') }}" method="POST" role="form">
      <div class="form-group">
        <label class="col-lg-3 control-label">Title:</label>
        <div class="col-lg-8">
          <input class="form-control" name="title" value='{{ $words->first()->title }}' type="text">
        </div>
      </div>
      <div class="form-group">
        <label class="col-lg-3 control-label">Meaning:</label>
        <div class="col-lg-8">
          <input class="form-control" name="meaning" value="{{ $words->first()->meaning }}" type="text">
        </div>
      </div>
    
      <div class="form-group">
        <label class="col-md-3 control-label"></label>
        <div class="col-md-8">
          <button class="btn btn-primary" type="submit">Save Changes</button>
          <span></span>
          <input class="btn btn-default" value="Cancel" type="reset">
        </div>
      </div>
    </form>
    

    and controller should be like this...

    public function postSaveEdit() {
        // The inputs variable contains all your form's inputs in the form of array...
        $inputs = request()->all();
        /* 
            $inputs = array(
                'title' => 'title_value',
                'meaning' => 'meaning_value'
            )
        */
        // Wheareas you can also get them by using 'get' method on request method like this
        $title = request()->get('title');
        $meaning = request()->get('meaning');
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程