2017-07-16 63 views
0

以下是我的表格: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控制器的数据,但不知道如何实现这一目标。 任何帮助,将不胜感激。

+0

什么是你得到的错误 –

+0

任何解决方案? – Prakash

回答

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; 
+0

这是填充表格的AJAX响应。请参阅tr和td。 – Prakash

+0

是否要将响应的数据插入数据库? –