How to use multiple post method in a signle form in laravel blade?

1k Views Asked by At

I am working with laravel.I have a form.I want to use two button name submit and save.This two button will work for two post method route.

Here my form...

<form action="{{ route('quizSubmit',$sessionId) }}" method="post" class="form-group">
  @csrf
  @foreach($quizQuestions->questions as $key =>  $question)
    @php
    $q = 1+$key
    @endphp
    <div class="form-group">
      {{--  <input type="hidden" class="form-control" name="question[]" value="{{$question->id}}">  --}}
      <label class="form-check-label" for="question">Question {{1+$key}}:</label>
      <h4>{{$question->name}}</h4>
      <input type="hidden" name="questions[{{ $q }}]" value="{{ $question->id }}">
       @foreach($question->choices as $key =>  $choice)
          {{-- <label class="form-check-label" for="radio1">
            <input name="radio-{{$q}}" type="radio">{{$choice->name}}
          </label> --}}
          <label>
              <input id="choice" type="radio" name="choice[{{$question->id}}]" 
                  value="{{$choice->id}}">
                  {{$choice->name}}
          </label>
      @endforeach
    </div>
    @endforeach
  <div class="form-group">
    <input type="submit" class="btn btn-primary">
    <form action="{{ route('quiz.incomplete',$sessionId) }}" method="post">
      @csrf
       <input type="submit" class="btn btn-info" value="Save" />
    </form>
  </div>
  
</form>

Here the routes. for submit button.

Route::post('quiz-session-ans/{sessionId}/questions/choices/submit','Web\Site\Quiz\QuizSessionAnsController@store')->name('quizSubmit');

for save button

Route::post('quiz-session-ans/{sessionId}/questions/choices/save','Web\Site\Quiz\QuizSessionAnsController@incompleteSession')->name('quiz.incomplete');

But the above code is not working the way that I want. How to solve it?

1

There are 1 best solutions below

0
Thomas Wiringa On

<form>s cannot be nested inside of each other. If you want 2 submit buttons going to different actions, you can use the formaction attribute on the submit buttons

For example:

<form method="post">
    <button formaction="/url1" type="submit">Submit to /url1</button>
    <button formaction="/url2" type="submit">Submit to /url2</button>
</form>

For more information, see: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#attr-formaction