Ajax Form在数据库php mysql中插入两次数据

时间:2016-08-08 16:32:59

标签: php jquery mysql ajax forms

情景

我有一个html表单(index.php),它使用ajax将数据提交到外部文件(register.php),该文件执行解析工作并将数据插入mysql表。

问题

提交表单后,它会在数据库中插入两次数据。我到处搜索但找不到任何解决方案。

这是我的代码:

index.php(表格):

<form name="register" id="register" class="col-md-12 form-horizontal" role="form" onsubmit="submitForm(); return false;">
<div class="form-group">
    <label for="regname" class="control-label"  >Name:*</label>
    <input name="regname" type="text" class="form-control" id="regname" placeholder="Please provide your Name">
</div>
<div class="form-group">
    <label for="regmobile" class="control-label">Mobile No.:*</label>
    <input name="regmobile" type="text" class="form-control" id="regmobile" placeholder="Your Mobile Number">
</div>
<div class="form-group">
    <label for="regvillage" class="control-label">Village/City:*</label>
    <input name="regvillage" type="text" class="form-control" id="regvillage" placeholder="Your Village Name">
</div>
<div class="form-group">
    <label for="regdistrict" class="control-label">District:*</label>
    <select name="regdistrict" id="regdistrict" class="form-control" data-allow-clear="true" data-placeholder="Select District" style="width:100%">
    <option value="">Please Select</option>
    <option value="Ahmedabad">Ahmedabad</option>
    <option value="Amreli">Amreli</option>
    <option value="Anand">Anand</option>
    </select>
</div>
<div class="form-group">
    <hr>
    <span id="status"></span></br>
    <button type="submit" name="submitbtn" id="submitbtn" class="pull-right btn btn-primary">Register</button>
</div>
</form>

index.php(Ajax)

<script>
function _(id){ return document.getElementById(id); }
function submitForm(){
_("submitbtn").disabled = true;
_("status").innerHTML = 'please wait ...';
var formdata = new FormData();
formdata.append( "regname", _("regname").value );
formdata.append( "regmobile", _("regmobile").value );
formdata.append( "regvillage", _("regvillage").value );
formdata.append( "regdistrict", _("regdistrict").value );
var ajax = new XMLHttpRequest();
ajax.open( "POST", "register.php" );
ajax.onreadystatechange = function() {
    if(ajax.readyState == 4 && ajax.status == 200) {
        if(ajax.responseText == "success"){
            _("register").innerHTML = '<h4>Thanks '+_("regname").value+', your registration is complete.</h4>';
        } else {
            _("status").innerHTML = ajax.responseText;
            _("submitbtn").disabled = false;
        }
    }
}
ajax.send( formdata );
}
</script>

register.php

<?php  require_once('caligrodb.php'); ?> //database connection config file
<?php 
if (isset($_POST['regname']) && isset($_POST['regmobile']) && isset($_POST['regvillage']) && isset($_POST['regdistrict'])){
    $regname = mysql_real_escape_string($_POST['regname']);
    $regmobile = mysql_real_escape_string($_POST['regmobile']);
    $regvillage = mysql_real_escape_string($_POST['regvillage']);
    $regdistrict = mysql_real_escape_string($_POST['regdistrict']);

    mysql_select_db($database_caligrodb, $caligrodb);
    mysql_query("INSERT INTO contacts(contactname, contactno, contactvillage, contactdistrict, subscribed) VALUES ('$regname', '$regmobile', '$regvillage', '$regdistrict', '1')");

    echo "success";
} else {
    echo"Kindly fill the form & submit the data!";
};
?>

任何人都可以帮我找到这段代码的错误吗?

1 个答案:

答案 0 :(得分:0)

我发现了错误:

onsubmit="submitForm(); return false;已应用于表单标记。我从那里删除了它,而是将onclick="submitForm(); returnfalse;添加到提交按钮

现在它的工作已经过时了。