Suliman Mubarak Suliman Mubarak - 6 months ago 13
PHP Question

PHP code: echo form data to a table

I have this code,
it should print form data submitted into table of 2 cols and show it same page
but it doesn't work

Any help please

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>

Answer

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); 
      print_r($rows);
   } 
  ?> 
  <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>
Comments