Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/442.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 通过客户端处理将HTML数据保存到数据库中_Php_Javascript_Ajax_Jquery_Client Side - Fatal编程技术网

Php 通过客户端处理将HTML数据保存到数据库中

Php 通过客户端处理将HTML数据保存到数据库中,php,javascript,ajax,jquery,client-side,Php,Javascript,Ajax,Jquery,Client Side,我需要将客户端生成的动态表数据保存到数据库中 我的动态表如下: <table class="table" id = "myTable"> <thead> <tr> <th>Roll No</th> <th>Student Name</th> <th>Attendance</th>

我需要将客户端生成的动态表数据保存到数据库中

我的动态表如下:

<table class="table" id = "myTable">
      <thead>
          <tr>
            <th>Roll No</th>
            <th>Student Name</th>
            <th>Attendance</th>
          </tr>
      </thead>
      <tbody>
          <?php foreach($results as $students) {?>
          <tr id="<?= $students->roll_no;?>" align ="center">
            <td><?= $students->roll_no;?></td>
            <td><?= $students->full_name;?></td>
            <td><input type="radio" id="att" name="attendance" value="present">Present
<input type="radio" id="att" name="attendance" value="absent">Absent</td>
          </tr>
          <?php } ?>                                                        
      </tbody>
    </table>
    <input type="submit" onClick="savedata()" name="submit" value="SAVE">

卷号
学名
出勤

这是解决办法。。我已修改了您的代码以修复一些错误

<table class="table" id = "myTable">
  <thead>
      <tr>
        <th>Roll No</th>
        <th>Student Name</th>
        <th>Attendance</th>
      </tr>
  </thead>
  <tbody>
      <?php foreach($results as $students) {?>
      <tr id="<?= $students["roll_no"];?>" align ="center">
        <td id="roll_no"><?= $students["roll_no"];?></td>
        <td id="full_name"><?= $students["full_name"];?></td>

         <!-- Attendance has to be unique for each row -->
        <td id="attendance"><input type="radio" id="att" name="attendance_<?= $students["roll_no"] ?>" value="present">Present
            <input type="radio" id="att" name="attendance_<?= $students["roll_no"]?>" value="absent">Absent</td>
      </tr>
      <?php } ?>                                                        
  </tbody>
</table>
这是最难看的方法。。只需使用JQuery,就可以用更少的行数和更高效地完成这项工作。我只是想向您展示如何使用您提供的代码来完成它。如果你有任何问题,请告诉我

丁斯

编辑

更好的方法

function savedata1() { 

var obj = $('#myTable tbody tr').map(function() {
    var $row = $(this);
    var roll = $row.find(':nth-child(1)').text();
    var atd = $row.find('td input[name="attendance_'+roll+'"]:radio:checked').val()
    return {
        roll_no: $row.find(':nth-child(1)').text(),
        full_name: $row.find(':nth-child(2)').text(),
        attendance: atd
    };
}).get();

 $.ajax({
        type: "POST",
        url: "ajax.php",
        data:"json=" + JSON.stringify(obj),
        processData: false, 
        dataType: 'json'
    });

}

您不需要在PHP文件中解码json,只需使用

 $value = stripslashes($_POST["json"]); 

如果您知道需要采取的步骤,可以向我们展示您为采取这些步骤而编写的代码吗?在这些步骤中,您在哪里被卡住了?您需要使用jQuery的or方法。除了提供关于AJAX的完整教程,如果没有看到您尝试使用AJAX失败的示例代码,就很难提供更多的指针。@约翰:我已经在我的编辑中解释了这些问题。我建议您为从表中获得的数据创建json对象,并对php文件进行AJAX查询,在该文件中进行json_解码。在您的表中,您可以通过以下方式创建单选按钮组:为表中的所有单选按钮赋予相同的名称属性值。因此,表中的所有单选按钮都在同一组中。如果不是有意的,你应该为每一行使用不同的名称,比如考勤号01、考勤号02等等。哦,非常感谢你的代码。。你能告诉我一个更好的方法吗。。我就是找不到更好的办法,所以只好这样做……)
<?php

    $value = json_decode(stripslashes($_POST["json"])); 
    print_r($value);
?>
Array
(
[0] => stdClass Object
    (
        [roll_no] => 10
        [full_name] => Name 1
        [attendance] => present
    )

[1] => stdClass Object
    (
        [roll_no] => 14
        [full_name] => Name 2
        [attendance] => absent
    )

[2] => stdClass Object
    (
        [roll_no] => 18
        [full_name] => Name 3
        [attendance] => present
    )
)
function savedata1() { 

var obj = $('#myTable tbody tr').map(function() {
    var $row = $(this);
    var roll = $row.find(':nth-child(1)').text();
    var atd = $row.find('td input[name="attendance_'+roll+'"]:radio:checked').val()
    return {
        roll_no: $row.find(':nth-child(1)').text(),
        full_name: $row.find(':nth-child(2)').text(),
        attendance: atd
    };
}).get();

 $.ajax({
        type: "POST",
        url: "ajax.php",
        data:"json=" + JSON.stringify(obj),
        processData: false, 
        dataType: 'json'
    });

}
 $value = stripslashes($_POST["json"]);