javascript - Pass table info to php and the upload it to the database -


hey have table inside 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 table via javascript 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 want upload info in database, i'm trying doing pass table info post , use javascript loop every row , adds database. it's not working. can 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 on every row.   //cells accessed easy    console.log(row);    }   </script>   <?php }  ?> 

you can use jquery ajax

update html code , add jquery ajax function 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 server file path url in ajax function , echo $_post['table'] on server html, make sure add jquery library on page if have not loaded yet.


Comments