2017-02-03 105 views
0

我已經提出了一些關於這個的問題,我被幫助過了,但是他們說這種方式只有在表單正常時纔有效,輸入的名字是「something here」我的表單只有一個輸入「文本」名稱「表」來放表格表格的其餘部分來自通過mysql ajax。我的問題是,如何將此表單傳遞到數據庫,因爲它是動態的?我的代碼如下。動態表單並通過jquery和ajax發送給mysql

我的形式

<div class="well"> 


       <!-- left --> 
       <div id="theproducts" class="col-sm-5"> 
       </div> 
       <!-- left --> 
       <form method="post" action="relatorio.php" id="formRel"> 
       <span>Mesa</span> 
     <input type="text" id="numero_mesa" name="numero_mesa"> 
       <input type="text" id="theinputsum"> 

       <!-- right --> 
       <div id="thetotal" class="col-sm-7"> 
        <h1 id="total"></h1> 
        <button type="submit" class="btn btn-lg btn-success btn-block"><i class="fa fa-shopping-cart" aria-hidden="true"></i> Finalizar Pedido</button> 
       </form> 
       </div> 
       <!-- right --> 


      </div> 

和JavaScript代碼。

<script> 
$('#formRel').submit(function(event){ 
     event.preventDefault(); 
     var formDados = new FormData($(this)[0]); 
$.ajax({ 
    method: "POST", 
    url: "relatorio.php", 
    data: $("#formRel").serialize(), 
    dataType : "html" 
}) 
}; 
</script> 

插入查詢是這樣的。

<?php 
error_reporting(-1); 
ini_set('display_errors', 'On'); 


//Criar a conexao 
$link = new mysqli ("localhost", "root", "", "restaurant"); 
if($link->connect_errno){ 
    echo"Nossas falhas local experiência .."; 
    exit(); 
} 
//echo "<pre>"; print_r($_POST); exit; 
$mesa = $_POST['numero_mesa']; 
$pedido = $_POST['products']; 
$preco = $_POST['products']; 


$sql = "INSERT INTO spedido ('pedido','preco','numero_mesa') VALUES ('$mesa','$pedido','$preco')"; 

$link->query($sql); 





?> 

enter image description here

回答

0
<script> 
$(document).on('submit', '#formRel', function(event) { 
    event.preventDefault(); 
    var numero_mesa = $('#numero_mesa').val(); 
    $.ajax({ 
     type: "POST", 
     url: "relatorio.php?", 
     data: "numero_mesa="+ numero_mesa+ 
     "&products="+ products, 
     success: function (data) { 
      alert(data) // this will send you a message that might help you see whats going on in your php file 
     }); 
    }) 
}; 
</script> 
+0

但我怎麼做派到MySQL,類型我該怎麼辦一部分在PHP發送到數據庫? –

相關問題