mickel mickel - 1 year ago 105
jQuery Question

data insert twice in database , ajax post

I have modal bootstrap register post form with

post and php but it inserts data twice in database

my modal :

<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 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 class="form-group">
<label for="ugame">name in game :</label>
<input type="text" name="ugame" class="form-control" id="ugame">
<div class="form-group">
<label for="pwd">password :</label>
<input type="password" name="pwd" class="form-control" id="pwd">
<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>


and here
Code :

$(document).ready(function () {
$("#regist").on("submit", function(e) {
var postData = $(this).serializeArray();
var formURL = $(this).attr("action");
url: formURL,
type: "POST",
data: postData,
success: function(data, textStatus, jqXHR) {
error: function(jqXHR, status, error) {
console.log(status + ": " + error);

$("#signup").on('click', function() {

Send Post to [
] ,

code :

<meta charset="UTF-8">
include '../db.php';
include '../../lang/lang.ar.php';

if(empty($_POST['user']) || empty($_POST['ugame']) || empty($_POST['pwd']))
echo $lang['empty'];
$q=mysqli_query($link,"SELECT * FROM accounts WHERE Username = '".$_POST['ugame']."'");
$q2=mysqli_query($link,"SELECT * FROM customers WHERE uname = '".$_POST['user']."'");
if(mysqli_num_rows($q) == 0)
echo $lang['ugame_!exist'];
}else if(mysqli_num_rows($q2) != 0)
echo $lang['exist'];
}else if(strpos($_POST['user'],';') !== false || strpos($_POST['user'],'-') !== false || strpos($_POST['user'],'#') !== false || strpos($_POST['user'],'@') !== false || strpos($_POST['user'],':') !== false || strpos($_POST['user'],'*') !== false)
echo $lang['not_allowed'];

}else if(strpos($_POST['user'],' ') !== false){
echo $lang['space'];
}else if(strlen($_POST['pwd']) < 6){
echo $lang['small_pass'];
}else if(strlen($_POST['user']) < 6){
echo $lang['small_user'];
}else if(strlen($_POST['pwd']) > 14){
echo $lang['larg_pass'];
}else if(strlen($_POST['user']) > 32){
echo $lang['larg_user'];

$date = date("y-m-d");
$stamp = date('Y-m-d\TH:i:s');

$done = mysqli_query($link,"INSERT INTO customers (uname,upass,ugame,date) VALUES ('".$_POST['user']."','".$_POST['pwd']."','".$_POST['ugame']."','".$date."')");
mysqli_query($link,"INSERT INTO notification (text,icon,date) VALUES ('New Account registred [ ".$_POST['user']." ]','icon-user','".$stamp."')");
echo $lang['register_done'];


Answer Source

You're posting the form twice.

When you click a submit button in a form, it triggers a submit event, which you handle with an AJAX request:

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

But you also have a click event handler for the button:

$("#signup").on('click', function() {

Which manually triggers a second submit event. Simply remove that click handler entirely, since the form already triggers the submit event by default.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download