如何制作第二个提交按钮?

时间:2021-07-12 23:24:08

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!

我有一个经典的表单,将数据提交给控制器。直到这里没什么特别的。问题是我想要第二个按钮,让我们说“保存并退出”我想要提交并重定向到主页。我怎么能这样做?我想这包括一些javascript进行检查?我只是无法绕过它。谢谢!

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

2 个解决方案

#1


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:

然后在你的save.php做:

if($_POST['check'] == 0){
    //redirect to page
}
else{
    //redirect to home
}

#2


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.

为每个按钮指定名称和值。在您的控制器中(客户端JS是错误的工具),测试名称,然后根据它重定向。

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

然后(以expressjs为例):

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')
  }
});

#1


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:

然后在你的save.php做:

if($_POST['check'] == 0){
    //redirect to page
}
else{
    //redirect to home
}

#2


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.

为每个按钮指定名称和值。在您的控制器中(客户端JS是错误的工具),测试名称,然后根据它重定向。

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

然后(以expressjs为例):

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')
  }
});