简体   繁体   English

javascript创建表php mysql以保存数据

[英]javascript to create table php mysql to save data

I need a table where the user enters how many rows and columns needed, they enter the numbers and the next page creates the table. 我需要一个表,用户在其中输入所需的行数和列数,然后输入数字,然后下一页将创建该表。

They will enter the info which will be saved into a database. 他们将输入信息,并将其保存到数据库中。 The only way I can think to do this is with dynamic tables, is there a better way? 我能想到的唯一方法是使用动态表,还有更好的方法吗? Here is some super basic code, I haven't worked out the full table, wanted to get feedback before I continue in case there is a better way and I need to change course. 这是一些超级基本的代码,我还没有制定出完整的表格,想在继续之前获得反馈,以防万一有更好的方法并且需要改变路线。

Simple form: 简单的形式:

  How many rows <input type="number" id="rowNumber"/><br>
  How many columns <input type="number" id="colNumber"/><br>

  <button onclick="myFunction()">Checkout</button>

     function myFunction() {
       var rowNumber = document.getElementById('rowNumber').value;
       var colNumber = document.getElementById('colNumber').value;

       window.location.href = "website/test.php?rowNumber="+rowNumber+"&colNumber="+colNumber;
}

test.php test.php

  <?php
    $rowNumber=$_GET['rowNumber'];
    $colNumber=$_GET['colNumber'];
  ?>

<script>
var numRows = "<? echo $rowNumber ?>";
var numCols = "<? echo $colNumber ?>";

var tableString = "<table>",
    body = document.getElementsByTagName('body')[0],
    div = document.createElement('div');

for (row = 1; row < numRows; row += 1) {

    tableString += "<tr onclick=\"fnselect(this)\"<? if($rowID == "A") { echo "class ='selected'";} ?>>";

    for (col = 1; col < numCols; col += 1) {

        tableString += "<td>" + "R" + row + "C" + col + "" + "<input type='text' />" + "</td>";
    }
    tableString += "</tr>";
}

tableString += "</table>";
div.innerHTML = tableString;
body.appendChild(div);
</script>

Looking into jQuery DataTables. 查看jQuery DataTables。 A lot of nice functionality in there. 那里有很多不错的功能。

You can either bind to a JSON data source, or create your own rows manually like this URL: https://datatables.net/examples/api/add_row.html 您可以绑定到JSON数据源,也可以手动创建自己的行,例如以下URL: https : //datatables.net/examples/api/add_row.html

So, to use this, you have to reference jquery AND the data tables script. 因此,要使用此功能,您必须引用jquery和数据表脚本。 You'll have to either reference them from their given URLs, or download the scripts (I recommend the latter otherwise you create references to outside servers). 您必须从它们的给定URL引用它们,或下载脚本(我建议使用后者,否则您将创建对外部服务器的引用)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM