3

I have a classic form that submits data to a controller. Nothing special until here.
The thing is I want a second button, let's say "Save and Exit" that I want to also submit and redirect to home page.
How could I do that? I guess this includes a little javascript for checking? I just can't wrap my head around it.
Thank you!

<form method="POST" action="link/here" id="main_form" class="form-horizontal">
   <input name="_token" value="JDtRMqc4aRFlK4QFzDPRTxKvNxIj5EnoLOceOUBT" type="hidden">
   <div class="box-body">
      <div class="form-group">
         <label for="url" class="col-sm-3 control-label">Figura</label>
         <div class="col-sm-9">
            <input class="form-control" id="Figura" name="figura" placeholder="Figura" value="" type="text">
         </div>
      </div>
      <div class="form-group">
         <label for="url" class="col-sm-3 control-label">Parcela</label>
         <div class="col-sm-9">
            <input class="form-control" id="Parcela" name="parcela" placeholder="Parcela" value="" type="text">
         </div>
      </div>
      <div class="form-group">
         <label for="url" class="col-sm-3 control-label">Rand</label>
         <div class="col-sm-9">
            <input class="form-control" id="Rand" name="rand" placeholder="Rand" value="" type="text">
         </div>
      </div>
      <div class="form-group">
         <label for="url" class="col-sm-3 control-label">Nr. Locuri</label>
         <div class="col-sm-9">
            <input class="form-control" id="Locuri" name="locuri" placeholder="Locuri" value="" type="text">
         </div>
      </div>
      <div id="locuri_div" class="col-sm-offset-1"></div>
      <div class="pull-right">
         <button type="submit" class="btn btn-success">Salveaza</button>
      </div>
      <div class="pull-left">
         <a href="another/link/here" class="btn btn-default">Inapoi</a>
      </div>
   </div>
   <!-- /.box-body -->
</form>
cagatayodabasi
  • 762
  • 11
  • 34
Vlad Eugen Nitu
  • 195
  • 2
  • 15
  • Add a boolean flag that gets posted that will let you know whether or not to redirect. – Gavin Nov 03 '16 at 15:40
  • 2
    You can find what you need here: http://stackoverflow.com/questions/547821/two-submit-buttons-in-one-form – spa900 Nov 03 '16 at 15:41

2 Answers2

1

your current button :

<button type="submit" name="check" value="0" class="btn btn-success">Save</button>

add a new one:

<button type="submit" name="check" value="1" class="btn btn-success">Save and Exit</button>

then in your save.php do:

if($_POST['check'] == 0){
    //redirect to page
}
else{
    //redirect to home
}
Diogo Eira
  • 34
  • 8
0

Give each button a name and a value. In your controller (client-side JS is the wrong tool for this), test the name and then redirect based on that.

For example:

<button name="action" value="save">Save</button>
<button name="action" value="save_and_go_home">Save &amp; Go Home</button>

And then (to use expressjs as an example):

app.post('/here', function (req, res) {
  // Do everything else you want to do with the data

  // Then
  var action = req.body.action;
  if (action === "save_and_go_home") {
      res.redirect("/");
  } else {
      res.send('Whatever you were sending before')
  }
});
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335