3

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?

mafortis
  • 6,750
  • 23
  • 130
  • 288

3 Answers3

3

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.

mafortis
  • 6,750
  • 23
  • 130
  • 288
0

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...

  }
}
Madushan Perera
  • 2,568
  • 2
  • 17
  • 36
0

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.");
    }
saber tabatabaee yazdi
  • 4,404
  • 3
  • 42
  • 58