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

时间:2016-11-03 15:37:00

标签: javascript html

我有一个经典的表单,可以将数据提交给控制器。没有什么特别的,直到这里 问题是我想要第二个按钮,让我们说“保存并退出”我想要提交并重定向到主页。
我怎么能这样做?我想这包括一些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 个答案:

答案 0 :(得分:1)

您当前的按钮:

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

添加一个新的:

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

然后在你的save.php中执行:

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

答案 1 :(得分:0)

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

例如:

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

然后(以expressjs为例):

func callJson(){
    JSONOperations.sharedInstance.JsonRequest(roadType: Helpers.RoadWayTypes.ALERTS, imageId: nil) {(json: NSDictionary) in
        do {
            //unbox alerts
            let aData: Alerts = try unbox(dictionary: json as! UnboxableDictionary)
            self.alerts = aData
            self.tableView.reloadData()
        }
        catch {
            print(error)
        }
    }
}