无法将表格提交给PHP脚本

时间:2017-07-18 12:07:05

标签: javascript php jquery ajax forms

我正在尝试使用DevOOPS Bootstrap 3 Admin主题制作一个表单填写网站,并且我在将表单字段中的数据发布到将数据插入数据库的insert.php脚本时遇到问题。

主题我使用:https://github.com/nnmware/devoops

所以主题使用了ajax loader。当我将数据插入表单然后点击SUBMIT时,主题将我重定向到insert.php但没有任何反应。然后我尝试通过javascript发送html表单数据:

> $(function () {
>     $('#form').on('submit',function (e) {
> 
>               $.ajax({
>                 type: 'post',
>                 url: 'insert.php',
>                 data: $('#form').serialize(),
>                 success: function () {
>                  alert("Introdus cu success!");
>                 }
>               });
>           e.preventDefault();
>         }); });

仍然不知道为什么不发送数据。

insert.php

 if(isset($_POST['submit']))
{
    $sql = "INSERT INTO `dateclienti` (`Nume Societate`, `Adresa Sediu`, `Adresa Punct de lucru 1`, `Adresa Punct de lucru 2`, `Registrul Comertului`, `Cod Fiscal`, `E-Mail`, `Cont Banca`, `Model casa de marcat`, `Marca casa`, `Numar Aviz`, `Data de achizitie`, `Configuratie`, `Serie casa`, `Serie fiscala`, `Nume Pers. De contact`, `Functie`, `Serie CI`, `CNP`, `Eliberat de`, `La data de`, `Telefon`,`Data`,`ID`) VALUES ('$_POST[numesoc]', '$_POST[adresasoc]', '$_POST[punct1]', '$_POST[punct2]', '$_POST[regcom]', '$_POST[codf]', '$_POST[email]', '$_POST[contbancar]', '$_POST[modelcasa]', '$_POST[marcacasa]', '$_POST[nraviz]', '$_POST[dataach]', '$_POST[config]', '$_POST[seriecasa]', '$_POST[seriefiscala]', '$_POST[perscon]', '$_POST[functie]', '$_POST[serieci]', '$_POST[CNP]', '$_POST[eliberat]', '$_POST[dataeliberarii]', '$_POST[telefon]', '$_POST[data]', null)";
    if(mysqli_query($db,$sql))
    {
        header("location: admin.php#ajax/adaugafirma.php");
    } 
    else
    {
        echo "ERRRORRRRRR !!!!";
        printf("ERRORRRRRRR: %s\n", mysqli_error($db));
    }
}

以及包含表单的页面:

<form action="admin.php#ajax/insert.php" id="form" method="post" name="form" class="form"> 
<div class="row">
    <div id="breadcrumb" class="col-xs-12">
        <a href="#" class="show-sidebar">
            <i class="fa fa-bars"></i>
        </a>
    </div>
</div>
<div class="row">
    <div class="col-xs-12 col-sm-6">
        <div class="box">
            <div class="box-header">
                <div class="box-name">
                    <i class="fa fa-search"></i>
                    <span>Datele Firmei</span>
                </div>
                <div class="box-icons">
                    <a class="collapse-link">
                        <i class="fa fa-chevron-up"></i>
                    </a>
                    <a class="expand-link">
                        <i class="fa fa-expand"></i>
                    </a>
                    <a class="close-link">
                        <i class="fa fa-times"></i>
                    </a>
                </div>
                <div class="no-move"></div>
            </div>

            <div class="box-content">
                 <div class="row">
                    <div class="col-sm-offset-3 col-sm-5"><input type="text" class="form-control" placeholder="Data" name="data"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Nume Societate" name="numesoc"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Adresa Societate" name="adresasoc"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Adresa Punct de lucru 1" name="punct1"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Adresa Punct de lucru 2" name="punct2"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Registrul Comertului" name="regcom"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Cod Fiscal" name="codf"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="E-Mail" name="email"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Cont Bancar" name="contbancar"></div>
             </div>
            </div>
        </div>
    </div>
    <div class="col-xs-12 col-sm-6">
        <div class="box">
            <div class="box-header">
                <div class="box-name">
                    <i class="fa fa-search"></i>
                    <span>Datele Casei de marcat</span>
                </div>
                <div class="box-icons">
                    <a class="collapse-link">
                        <i class="fa fa-chevron-up"></i>
                    </a>
                    <a class="expand-link">
                        <i class="fa fa-expand"></i>
                    </a>
                    <a class="close-link">
                        <i class="fa fa-times"></i>
                    </a>
                </div>
                <div class="no-move">
                </div>
            </div>
            <div class="box-content">
                                      <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:40px;"><input type="text" class="form-control" placeholder="Marca Casa de marcat" name="marcacasa"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Model Casa de marcat" name="modelcasa"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Numar si data aviz" name="nraviz"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Data de achizitie" name="dataach"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Configuratie" name="config"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Serie Casa" name="seriecasa"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;padding-bottom:52px;"><input type="text" class="form-control" placeholder="Serie Fiscala" name="seriefiscala"></div>
             </div>
            </div>
        </div>
    </div>
</div>
<div class="row">
    <div class="col-xs-12">
        <div class="box">
            <div class="box-header">
                <div class="box-name">
                    <i class="fa fa-search"></i>
                    <span>Datele Persoanei de</span>
                </div>
                <div class="box-icons">
                    <a class="collapse-link">
                        <i class="fa fa-chevron-up"></i>
                    </a>
                    <a class="expand-link">
                        <i class="fa fa-expand"></i>
                    </a>
                    <a class="close-link">
                        <i class="fa fa-times"></i>
                    </a>
                </div>
                <div class="no-move"></div>
            </div>
            <div class="box-content">
                                      <div class="row">
                    <div class="col-sm-offset-3 col-sm-5"><input type="text" class="form-control" placeholder="Persoana de contact" name="perscon"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Functie" name="functie"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Serie CI" name="serieci"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="CNP" name="CNP"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Eliberat de" name="eliberat"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Data eliberarii CI" name="dataeliberarii"></div>
             </div>
                          <div class="row">
                    <div class="col-sm-offset-3 col-sm-5" style="padding-top:20px;"><input type="text" class="form-control" placeholder="Telefon" name="telefon"></div>
             </div>
            </div>
        </div>
    </div>
</div>

<div class="col-sm-offset-5 col-sm-5"><button type="submit" name="submit" class="btn btn-success" value="1">Adauga</button></div>
</form>
<script type="text/javascript">
    WinMove();
</script>

5 个答案:

答案 0 :(得分:1)

将您的JS代码更改为以下内容。您应该始终先e.preventDefault();。其次,既然您通过AJAX提交,那么您应该return false

$(function() {
  $('#form').on('submit', function(e) {
    e.preventDefault();
    $.ajax({
      type: 'post',
      url: 'insert.php',
      data: $('#form').serialize(),
      success: function() {
        alert("Introdus cu success!");
      }
    });
    return false;
  });
});

答案 1 :(得分:1)

JS部分:

$(function() {
  $('#form').on('submit', function(e) {
    e.preventDefault();
    $.ajax({
      type: 'post',
      url: 'insert.php',
      data: $('#form').serialize(),
      success: function() {
        alert("Introdus cu success!");
      }
    });
    return false;
  });
});

并且没有字段submit,请尝试使用if(isset($_POST['data']))然后使用您的代码进行检查。

答案 2 :(得分:0)

$(function() { $('#form').on('submit', function(e) { e.preventDefault(); $.ajax({ type: 'post', url: 'insert.php', data: $('#form').serialize(), success: function() { alert("Introdus cu success!"); } }); return false; }); }); 移除操作值后尝试 然后将e.preventDefault作为jquery函数的第一行。这两项更改将解决您的问题。 像这样:

def foo():
    print 'foo was called'
    return 25

for i in xrange(0, 100, foo()):
    print i

答案 3 :(得分:0)

如果从表单中删除action属性,则可以正常工作。

<form id="form" method="post" name="form" class="form">

答案 4 :(得分:-1)

没有提交条目,请尝试:

if(isset($_POST['data']))