簡體   English   中英

Ajax Form在數據庫php mysql中插入數據兩次

[英]Ajax Form inserts data twice in database php mysql

情景

我有一個 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>

注冊.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!";
};
?>

誰能幫我找出這段代碼有什么問題?

我發現了錯誤:

onsubmit="submitForm(); return false;應用於表單標簽。我從那里刪除了它,而是添加了onclick="submitForm(); returnfalse; onclick="submitForm(); returnfalse; 提交按鈕

現在它按預期工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM