2017-04-05 88 views
0

嘿,我有這個表的窗體通行證表信息到PHP和上傳到數據庫

<form method="post"> 
      <table class="table table-bordered table-hover" id="factura"> 
       <thead> 
       <tr> 
        <th>Descripción</th> 
        <th class="text-center" style="width: 100px;">Cantidad</th> 
        <th class="text-right" style="width: 120px;">Precio Unitario</th> 
        <th class="text-right" style="width: 120px;">Total</th> 
       </tr> 
       </thead> 
       <tbody> 


       </tbody> 
      </table> 
       <input class="btn btn-sm btn-primary pull-right" type="submit" id="save" name="saveInvoice" disabled="true" value="Guardar"> 
      </form> 

我通過JavaScript將項目添加到該表這樣的內部。

var titulo = document.getElementById("newItemTitle").value; 
    var descripcion = document.getElementById("newItemDescription").value; 
    var cantidad = document.getElementById("newItemQuantity").value; 
    var precio = document.getElementById("newItemPrice").value; 

    var totalItem = precio * cantidad; 

    valorTotal += totalItem; 

    var precioFix = precio * 1; 


    var table = document.getElementById("factura"); 
    var row = table.insertRow(1); 
    var cell2 = row.insertCell(0); 
    cell2.innerHTML = '<p class="font-w600 push-10">' + titulo +'</p><div class="text-muted" >' + descripcion + '</div>'; 
    var cell3 = row.insertCell(1); 
    cell3.className = "text-center"; 
    cell3.innerHTML = '<span class="badge badge-primary">'+ cantidad +'</span>'; 
    var cell4 = row.insertCell(2); 
    cell4.className = "text-right"; 

    cell4.innerHTML = '$' + formatMoney(precioFix, '') + ''; 
    var cell5 = row.insertCell(3); 
    cell5.className = "text-right"; 
    cell5.innerHTML = '$' + formatMoney(totalItem, '') + ''; 

現在我要上傳數據庫中的所有這些信息,我試圖做這樣的事情,我通過表信息的帖子和我使用JavaScript來循環到每一行,並將其添加到數據庫。但它不起作用。有人能幫助我嗎?

<?php 
if($_POST['saveInvoice']) { 

    $table = $_POST['factura']; 

    ?> 
    <script> 
    var table = <?php echo($table) ?> 
    var rowLength = table.rows.length; 

    for(var i=0; i<rowLength; i+=1){ 
    var row = table.rows[i]; 

    //your code goes here, looping over every row. 
    //cells are accessed as easy 

    console.log(row); 

    } 
    </script> 
    <?php 
} 

?> 
+1

你正在混合serverside與客戶端..也許學習一些Ajax? – Konstantinos

+0

php代碼在服務器端執行...你的javascript在客戶端,所以它會運行一次頁面加載(sorta,不會在詳細信息中) – WilomGfx

+0

嗯,好吧,我到底該做什麼,我可以上傳這個信息以簡單的方式到我的分貝? – Gonzalo4488

回答

0

您可以使用jquery ajax

更新HTML這樣的代碼,並添加如下jquery ajax功能:

HTML代碼:

<form method="post" onsubmit="return sendTable()" id="tableForm"> 
    <table class="table table-bordered table-hover" id="factura"> 
     <thead> 
     <tr> 
      <th>Descripción</th> 
      <th class="text-center" style="width: 100px;">Cantidad</th> 
      <th class="text-right" style="width: 120px;">Precio Unitario</th> 
      <th class="text-right" style="width: 120px;">Total</th> 
     </tr> 
     </thead> 
     <tbody> 


     </tbody> 
    </table> 
    <input class="btn btn-sm btn-primary pull-right" type="submit" id="save" name="saveInvoice" disabled="true" value="Guardar"> 
</form> 

Ajax代碼:

var sendTable = function() { 
    $.ajax({ 
     url : "SERVER_FILE_PATH.php", 
     data : {table: $('#tableForm').html()}, 
     type : "POST", 
     dataType : "html", 
     success : function(){ 

     } 
    }) 
    return false; 
} 

添加您的服務器的文件路徑爲url在AJAX功能以及服務器上的呼應$_POST['table']拿到HTML,還要確保你的頁面上添加jquery庫,如果你還沒有加載。

+0

Thaaanks我現在要試試這個! – Gonzalo4488

+0

歡迎,您可以將此答案標記爲有用,以便幫助他人! –