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

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条)

报告相同问题?