dorkahemp972157683 2016-08-08 16:32
浏览 47
已采纳

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

The Scenario

I have a html form (index.php) which submits data using ajax to an external file (register.php) which does the parsing job and inserts data into mysql table.

The Problem

When the form is submitted, it inserts the data twice in the database. I searched everywhere but could not find any solution.

Here is my code:

index.php (form):

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

Can anyone help me in find whats wrong with this code?

  • 写回答

1条回答 默认 最新

  • duanqin2026 2016-08-09 08:51
    关注

    I found the mistake:

    The onsubmit="submitForm(); return false; is applied to the form tag. I removed it from there and instead I added onclick="submitForm(); returnfalse; to the submit button.

    Now its working as expacted.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch