简体   繁体   English

如何将(通过javascript)动态创建的HTML表的数据发送到PHP文件进行处理?

[英]How do I send the data of an (by javascript) dynamically created HTML table to a PHP file to process?

(first off, I'm very new to JavaScript - HTML/PHP I know a little better) (首先,我是JavaScript的新手 - HTML / PHP我知道的更好一点)

I would like to send the table data (which is dynamically created by javascript - depending on how many students there are in the database) to a PHP file to process. 我想将表数据(由javascript动态创建 - 取决于数据库中有多少学生)发送到要处理的PHP文件。 The table is created and added to HTML-id "create_groups" (which is included in a HTML Form) after the button "Create Groups" has been pressed. 在按下“创建组”按钮后,将创建该表并将其添加到HTML-id“create_groups”(包含在HTML表单中)。

I've tried giving each table row it's own name ( tr.setAttribute('name', students[i][col[0]]); ) which works (at least the browser shows it in the "console" window) but when I press "submit" the Javascript table data isn't transmitted to the create_groups.php. 我试过给每个表行自己的名字( tr.setAttribute('name', students[i][col[0]]); )哪个有效(至少浏览器在“控制台”窗口中显示)但是当我按“提交”时,Javascript表数据不会传输到create_groups.php。 At least I can't seem to get hold of it. 至少我似乎无法掌握它。

teacher.php teacher.php

  function createTableFromJSON() { hideAll(); document.getElementById('create_groups').style.display = "block"; let con = new XMLHttpRequest(); //Create Object console.log("1"); con.open("GET", "teacher_check.php", true); //open the connection con.onreadystatechange = function() { //define Callback function if (con.readyState == 4 && con.status == 200) { console.log("2"); console.log(this.responseText); let response = this.responseText; let students = JSON.parse(this.responseText); //Convert String back into JSON object console.log(students); let col = []; for (let key in students[0]) { col.push(key); } // CREATE DYNAMIC TABLE. let table = document.createElement("table"); // CREATE HTML TABLE HEADER ROW USING THE EXTRACTED HEADERS ABOVE. let tr = table.insertRow(-1); // TABLE ROW AT THE END let th = document.createElement("th"); th.innerHTML = "SELECT"; tr.appendChild(th); for (let i = 0; i < col.length; i++) { let th = document.createElement("th"); // TABLE HEADER. th.innerHTML = col[i]; tr.appendChild(th); } // ADD JSON DATA TO THE TABLE AS ROWS. for (let i = 0; i < students.length; i++) { tr = table.insertRow(-1); var checkbox = document.createElement('input'); checkbox.type = "checkbox"; console.log(students[i][col[0]]); //this shows the right names tr.appendChild(checkbox); tr.setAttribute('name', students[i][col[0]]); for (let j = 0; j < col.length; j++) { let tabCell = tr.insertCell(-1); tabCell.innerHTML = students[i][col[j]]; } } // FINALLY ADD THE NEWLY CREATED TABLE WITH JSON DATA TO A CONTAINER. let divContainer = document.getElementById("create_groups"); //divContainer.innerHTML = ""; divContainer.appendChild(table); document.getElementById("create_groups").submit(); } } con.setRequestHeader("Content-Type", "application/x-www-form-urlencoded"); //Set appropriate request Header con.send(); //Request File Content } function hideAll() { document.getElementById('create_groups').style.display = "none"; } 
  <input type="button" name="create_groups" value="Create Groups" onclick="createTableFromJSON()"> <form action="create_groups.php" method ="post"> <div class="container white darken-4" id="create_groups" style="display:none;"> <p> <label> <span>Groupname:</span> <input type="text" name="groupname"> </label> </p> <p><button type="submit" name ="submit">Submit</button></p> </div> 

create_groups.php create_groups.php

if (isset($_POST['submit'])) {
    $student = $_POST['stud'];
    $groupname = $_POST['groupname'];

   echo $groupname;
   echo $student;
}

I expect to be able to access all the names of the students in the create_groups.php file, which are checked off in the other teacher.php file in the table. 我希望能够访问create_groups.php文件中学生的所有名称,这些名称在表格中的其他teacher.php文件中进行了检查。 Obviously the "isChecked" part isn't implemented yet, because the other part doesn't work yet. 显然,“isChecked”部分尚未实现,因为其他部分尚未实现。
So the creating of the table with the correct data works, just not the transmitting to the PHP file. 因此,使用正确的数据创建表可以正常工作,而不是传输到PHP文件。

There is few issues in html syntax and javascript as well please try correcting them. html语法和javascript中几乎没有问题,请尝试纠正它们。

hideAll() function definition is missing in you script first line. 您脚本第一行中缺少hideAll()函数定义。 Second in html before 在html之前的第二个

tag there is unnecessary ">" character. 标记有不必要的“>”字符。

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

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