POST数据为数组

时间:2017-07-16 13:48:43

标签: php arrays ajax post

以下是我的表格:

<form method="POST" action="../controller/assignsubteacher.php">
  <table class="table table-bordered table-striped table-hover table-condensed" id="coursedetail" >
    <thead>
      <tr>
        <th>Sub Id</th>
        <th>Sub Name</th>
        <th>Teacher Name</th>
      </tr>
    </thead>
    <tbody id="table_ajax">

    </tbody>
    <tfoot>
      <tr>
        <th>Sub Id</th>
        <th>Sub Name</th>
        <th>Teacher Name</th>
      </tr>
    </tfoot>
  </table>
  <div class="col-md-2">
    <button type="submit" class="btn btn-primary btn-block btn-flat">Submit</button>
  </div>
</form>

表单中的表格由以下响应填充:

while($row=mysqli_fetch_array($result))
    {
        $list='<select id="teacher" name="teacher'.$COUNT.'" class="form-control">
            <option value = "UNKNOWN" selected="select">-SELECT-</option>';
        $get_teacher="select Regno,Name from Student_Registration inner join Login on Regno=Uname where Id=2;";
        $teacher_list = mysqli_query($con,$get_teacher);
        while($row_Teacher=mysqli_fetch_array($teacher_list))
        {
            $list.='<option value="'.$row_Teacher['Regno'].'">'.$row_Teacher['Name'].'</options>';
        }
        $list.='</select>';
        $Subject_ID=$row["SubId"].'<input type="hidden" name="SubId'.$COUNT.'" value="'.$row["SubId"].'">';
        //$Subject_Name=$row["Subject_Name"].'<input type="hidden" name="SubName'.$COUNT.'" value="'.$row["Subject_Name"].'">';
        $Subject_Name=$row["Subject_Name"];

        $tr.='<tr>
                <td>'.$Subject_ID.'</td>
                <td>'.$Subject_Name.'</td>
                <td>'.$list.'</td>
                </tr>';
        $COUNT=$COUNT+1;
    }
    echo $tr;

我无法使用发布的数据插入数据库。有什么办法可以将数据作为数组发送并检索它。 以下是填充表体的AJAX:

xhr.onreadystatechange = function() 
          {
            if (this.readyState == 4 && this.status == 200) 
            {
              console.log(xhr.responseText);
              Table.innerHTML=xhr.responseText;
              }
          };

我在考虑使用foreach在POST控制器中插入数据,但不知道如何实现。 任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

使用SQL插入查询来执行此操作。

while($row=mysqli_fetch_array($result))
{
    $list='<select id="teacher" name="teacher'.$COUNT.'" class="form-control">
        <option value = "UNKNOWN" selected="select">-SELECT-</option>';
    $get_teacher="select Regno,Name from Student_Registration inner join Login on Regno=Uname where Id=2;";
    $teacher_list = mysqli_query($con,$get_teacher);
    while($row_Teacher=mysqli_fetch_array($teacher_list))
    {
        $list.='<option value="'.$row_Teacher['Regno'].'">'.$row_Teacher['Name'].'</options>';
    }
    $list.='</select>';
    $Subject_ID=$row["SubId"].'<input type="hidden" name="SubId'.$COUNT.'" value="'.$row["SubId"].'">';
    //$Subject_Name=$row["Subject_Name"].'<input type="hidden" name="SubName'.$COUNT.'" value="'.$row["Subject_Name"].'">';
    $Subject_Name=$row["Subject_Name"];

    $tr.='<tr>
            <td>'.$Subject_ID.'</td>
            <td>'.$Subject_Name.'</td>
            <td>'.$list.'</td>
            </tr>';
    $COUNT=$COUNT+1;
    $sql = "INSERT INTO `table` (column1, column2, column3) VALUES ($list, $subject_name, $tr)";
    //Replace columns & table name
    if(mysqli_query($con, $sql)) {
         echo "Inserted";
    }
}
echo $tr;