Laravel form with two submit button

天大地大妈咪最大 提交于 2021-02-04 21:07:04

问题


I need two submit button for my update form,

current setting

Currently when I hit submit it saves my data and redirect me another page where i can edit my multiple images (so my form is like two step function)

What I want to add

I want to add another button in order to just save my data and return me to index page (skip second step)

last result

Last result will be my edit form with two button

  1. button 1 saves data and return me to next form to edit my images
  2. button 2 saves data and return me to index page

Codes

controller function

public function update(Request $request, $id)
    {
      // validation and....

      $product->save();

      // this is my current button action (redirect to second step)
      return redirect()->route('editmultiimages',
          $product->id)->with('success',
          'Product, '. $product->title.' updated, now you can edit images.');

     // need second button action here
}

blade form

{{ Form::model($product, array('route' => array('products.update', $product->id), 'method' => 'PUT', 'files' => true)) }}

// my inputs

// my current button (saves data and goes to next step)
{{ Form::submit('Edit Images', array('class' => 'btn btn-success')) }}

{{Form::close()}}

Any idea?


回答1:


SOLVED

blade form

{{ Form::submit('Edit Images', array('class' => 'btn btn-info', 'name' => 'submitbutton')) }}
{{ Form::submit('Finish', array('class' => 'btn btn-success', 'name' => 'submitbutton')) }}

controller

switch ($request->submitbutton) {
        case 'Edit Images':
            return redirect()->route('editmultiimages', $product->id)->with('success', 'Product, '. $product->title.' updated, now you can edit images.');
            break;

        case 'Finish':
            Session::flash('success', 'Product, '. $product->title.' updated successfully.');
            return redirect()->route('products.index', $product->id);
            break;
}

Hope it help others.




回答2:


You can use two submit buttons with different value attributes.

View :

...

{{ Form::submit('Edit Images', array('class' => 'btn btn-success','name'=>'btnSubmit', 'value'=>'button1')) }}

{{ Form::submit('Edit Images', array('class' => 'btn btn-success','name'=>'btnSubmit', 'value'=>'button2')) }}

...

Controller :

public function update(Request request) {

  if(request->get('btnSubmit') == 'button1') {

    // do your stuff here...

  } else if(request->get('btnSubmit') == 'button2') {

    // do your stuff here...

  }
}



回答3:


you can use the same name and different value attribute for the submit buttons

// example:

<input type="submit" class="btn btn-success" value="save and close" name="submitbutton">
<input type="submit" class="btn btn-success" value="apply" name="submitbutton">
<input type="submit" class="btn btn-success" value="save and new" name="submitbutton">
<input type="submit" class="btn btn-success" value="save and search" name="submitbutton">
            

// Controller:

switch($request->submitbutton) {

    case 'save and close': 
        //action save here and close
    break;

    case 'save and new': 
        //action for save and new
    break;

    case 'save and search': 
        //action for save and search
    break;

    case 'apply': 
        //action for save and route here
    break;
}

or

    if ($request->submitbutton == 'apply') {
        return redirect()->route('admin.products.edit', $product->id)->with('success', "new product {$product->name} created as well.");
    } else if ($request->submitbutton == 'save and search'){
        return redirect()->route('admin.products.index', ['name' => $product->name])->with('success', "product {$product->name} saved.");
    } else if ($request->submitbutton == 'save and close'){
        return redirect()->route('admin.products.index')->with('info', "product {$product->name} saved");
    } else if ($request->submitbutton == 'save and new'){
        return redirect()->route('admin.products.create' , $request->category_id)->with('info', "product {$product->name} saved.");
    }


来源:https://stackoverflow.com/questions/50997871/laravel-form-with-two-submit-button

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!