2017-07-18 186 views
0

我試圖用DevOOPS Bootstrap 3 Admin主題做一個表單填充網站,我在將表單域中的數據發佈到insert.php腳本時會出現問題,該腳本會將數據插入到數據庫。無法提交表單到Php腳本

我使用的主題爲:https://github.com/nnmware/devoops

所以主題使用AJAX加載器。當我將數據插入表單然後點擊SUBMIT時,主題將我重定向到insert.php,但沒有任何反應。然後我嘗試發送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> 
+0

你的ajax請求的輸出是什麼? – hallleron

+0

不要重定向php –

回答

0

<form> 移除行動值,然後把e.preventDefault在jQuery的功能的第一行後嘗試。這兩項更改將解決您的問題。 像這樣:

$(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; 
    }); 
}); 
+4

更好的評論,而不是一個答案 –

+0

但答案是...... Himanshu有理由! – Ciccio

+0

一個襯裏不是答案。它沒有解釋任何東西。如果您有一個行答案,最好提出意見 –

-1

沒有一個提交項,嘗試:

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

提交按鈕本身就是表單數據中的'submit'值。 – David

+0

提交按鈕本身提交的值爲1。 –

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; 
    }); 
}); 
0

如果您從表單action屬性它的工作。

<form id="form" method="post" name="form" class="form"> 
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'])),然後你的代碼進行檢查。