数据在数据库中插入两次,ajax post

时间:2016-08-15 02:04:33

标签: php jquery ajax twitter-bootstrap mysqli

我有class MyObject: Object { /** Primary Key */ dynamic var id: String = "" /** Some persisted value */ dynamic var someString: String? var nextID: String { do { let realm = try Realm() var id: String = NSUUID().UUIDString /// NSUUID().UUIDString almost always makes a unique ID on the first try /// but we'll check after we generate the first one just to be sure while realm.objectForPrimaryKey(MyObject.self, key: id) != nil { id = NSUUID().UUIDString } return id } catch let error as NSError { /// Oops fatal(error: error.localizedDescription) } } convenience init(someString: String?) { self.init() id = nextID self.someString = someString save() } override class func primaryKey() -> String? { return "id" } func save() { /// Gotta check this in case the object was created without using the convenience init if id == "" { id = nextID } do { let realm = try Realm() try realm.write { /// Add... or update if already exists realm.add(self, update: true) } } catch let error as NSError { fatalError(error.localizedDescription) } } } 帖子和php的模态引导寄存器帖子,但它在数据库中插入两次数据

我的模态:

ajax

<div id="register" class="modal fade" role="dialog"> <div class="modal-dialog"> <!-- Modal content--> <div class="modal-content"> <div class="modal-header"> <button type="button" class="close" data-dismiss="modal">&times;</button> <h4 class="modal-title">New Member</h4> </div> <div class="modal-body"> <div class="msg"></div> <form id="regist" action="/model/php/ajax.php" method="post" accept-charset="utf-8" > <div class="form-group"> <label for="usr">username:</label> <input type="text" name="user" class="form-control" id="usr"> </div> <div class="form-group"> <label for="ugame">name in game :</label> <input type="text" name="ugame" class="form-control" id="ugame"> </div> <div class="form-group"> <label for="pwd">password :</label> <input type="password" name="pwd" class="form-control" id="pwd"> </div> <div class="g-recaptcha" data-sitekey="<?php echo key; ?>"></div> <button type="submit" id="signup" class="btn btn-success btn-lg">Signup<span class="glyphicon glyphicon-user"></span></button> </form> </div> <div class="modal-footer"> <button type="button" class="btn btn-info" data-dismiss="modal">close</button> </div> </div> </div> </div> 代码:

Ajax

发送帖子到[ <script> $(document).ready(function () { $("#regist").on("submit", function(e) { var postData = $(this).serializeArray(); var formURL = $(this).attr("action"); $.ajax({ url: formURL, type: "POST", data: postData, success: function(data, textStatus, jqXHR) { $('.msg').html(data); $('.inf').remove(); }, error: function(jqXHR, status, error) { console.log(status + ": " + error); } }); e.preventDefault(); }); $("#signup").on('click', function() { $("#regist").submit(); }); }); </script> ],

此处Action="ajax.php"代码:

ajax.php

1 个答案:

答案 0 :(得分:3)

您要两次发布表单。

单击submit中的form按钮时,会触发submit事件,您可以使用AJAX请求处理该事件:

$("#regist").on("submit", function(e) {
    // your AJAX code
});

但是你为该按钮设置了click事件处理程序:

$("#signup").on('click', function() {
    $("#regist").submit();
});

手动触发第二个submit事件。只需完全删除click处理程序,因为表单默认情况下已触发submit事件。