2017-04-05 41 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

欢迎,您可以将此答案标记为有用,以便帮助他人! –