dongmu1914 2017-04-05 20:25
浏览 39
已采纳

将表信息传递给php并将其上传到数据库

Hey I have this table inside a form

<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>

I'm adding items to this table via Javascript this way.

  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, '') + '';

Now I want to upload all this info in the database, I'm trying doing something like this where I pass the table info to a post and the I use javascript to loop into every row and adds it to the database. But it's not working. Can someone help me please?

<?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条回答 默认 最新

  • doushi8186 2017-04-05 20:38
    关注

    You can use jquery ajax

    Update your html code like this and add jquery ajax function as below:

    Html Code:

    <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 Code:

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

    Add your server file path for url in ajax function and echo $_POST['table'] on your server to get the html, Also make sure to add jquery library on your page if you have not loaded yet.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用verilog实现tanh函数和softplus函数
  • ¥15 求京东批量付款能替代天诚
  • ¥15 slaris 系统断电后,重新开机后一直自动重启
  • ¥15 51寻迹小车定点寻迹
  • ¥15 谁能帮我看看这拒稿理由啥意思啊阿啊
  • ¥15 关于vue2中methods使用call修改this指向的问题
  • ¥15 idea自动补全键位冲突
  • ¥15 请教一下写代码,代码好难
  • ¥15 iis10中如何阻止别人网站重定向到我的网站
  • ¥15 滑块验证码移动速度不一致问题