PHP代码:将表单数据回显到表中

时间:2016-05-05 08:37:33

标签: php

我有这个代码, 它应该打印表格数据提交到2列的表格并显示相同的页面 但它不起作用

请帮助

NogDog

  <?php 
 if(isset($_POST['submit'])) 
 { 
 $bits = ''; 
 // use preg_split so I can use the NO_EMPTY modifier 
 $cells = preg_split('/=/', $bits, -1, PREG_SPLIT_NO_EMPTY); 
 $rows = array_chunk($cells, 2); 
 } 
 ?> 
 <table>
<tr>
    <th>
        Col 1
    </th>
    <th>
        Col 2
    </th>
</tr>
<form method="post" action="
<?php 
foreach($rows as $row) { 
echo "<tr>"; 
foreach($row as $cell) { 
    echo "<td>".htmlspecialchars($cell)."</td>"; 
}  
 } 
?>">
    <input type="text" name="poet">
    <br>
    <input type="submit" name="submit" value="Submit Form">
        <br>
        </form>
 </table>

2 个答案:

答案 0 :(得分:0)

您没有在foreach循环中关闭tr标记

 <table>
 <tr>
<th>
    Col 1
</th>
<th>
    Col 2
</th>
</tr>
<form method="post" action="
 <?php 
 foreach($rows as $row) { 
 echo "<tr>"; 
 foreach($row as $cell) { 
echo "<td>".htmlspecialchars($cell)."</td>"; 
}  
 } 
?>">

答案 1 :(得分:0)

Try this code. It's working as you said. First I thought it was a dump question but it works!!!

  <?php 
  if(isset($_POST['submit'])) 
  { 
      $bits = $_POST['poet']; 
     // use preg_split so I can use the NO_EMPTY modifier 
      $cells = preg_split('/ /', $bits, -1, PREG_SPLIT_NO_EMPTY); 
      $rows = array_chunk($cells, 2); 
   } 
  ?> 
  <table>
  <tr>
     <th>
       Col 1
     </th>
     <th>
       Col 2
     </th>
  </tr>
 <?php 
 foreach($rows as $row) { 
 echo "<tr>"; 
    foreach($row as $cell) { 
      echo "<td>".htmlspecialchars($cell)."</td>"; 
    }  
 }
 ?>
 <form method="post" action="">
 <input type="text" name="poet">
 <br>
 <input type="submit" name="submit" value="Submit Form">
  <br>
  </form>
 </table>