2014-04-07 38 views
1

這裏我有一個HTML表格,它將商店提交的數據存儲在表格中。在多個請求中的表格中存儲表格值

<label> ID: </label><input type="text" name="id"/> 
<label>Name :</label><textarea name='Name'></textarea> 
<label>Value :</label><br /><input type="text" name="Value"/> 
<input type="submit" name="submit" value=" submit "/> 

下一次我提交表單時,表刷新並存儲新值。相反,我需要它將新行添加到表中,並存儲在會話期間提交的所有以前的數據。如何在不使用數據庫的情況下添加新行?

<?php 
session_start(); 
echo "<table> 
<tr> 
<th>ID</th> 
<th>Name</th> 
<th>Value</th> 
</tr>"; 

if (isset($_POST['submit'])) { 
    echo " 
    <tr> 
    <td>".$_POST['id']."</td> 
    <td>".$_POST['Name']."</td> 
    <td>".$_POST['Value']."</td> 
    </tr>"; 
} 

回答

0

試試這個:

<?php 
session_start(); 
echo "<table> 
<tr> 
<th>ID</th> 
<th>Name</th> 
<th>Value</th> 
</tr>"; 

if (isset($_POST['submit'])) { 
    $_SESSION['posts'][] = $_POST; 
    foreach ($_SESSION['posts'] as $post) 
    echo "<tr> 
      <td>{$post['id']}</td> 
      <td>{$post['Name']}</td> 
      <td>{$post['Value']}</td> 
     </tr>"; 
} 

,或者如果你想這一切在一個頁面:

<?php 
session_start(); 
echo "<table> 
<tr> 
<th>ID</th> 
<th>Name</th> 
<th>Value</th> 
</tr>"; 

if (isset($_POST['submit'])) { 
    $_SESSION['posts'][] = $_POST; 
    foreach ($_SESSION['posts'] as $post) 
    echo "<tr> 
      <td>{$post['id']}</td> 
      <td>{$post['Name']}</td> 
      <td>{$post['Value']}</td> 
     </tr>"; 
} 

?> 
<form action="" method="post"> 
<label> ID: </label><input type="text" name="id"/><br> 
<label>Name :</label><textarea name='Name'></textarea><br> 
<label>Value :</label><br /><input type="text" name="Value"/><br> 
<input type="submit" name="submit" value=" submit "/><br> 
+0

正是我一直在尋找。謝謝! – Indra

相關問題