2012-09-28 22 views
0

我創建一個表格,其中包括兩個文本框&一個提交按鈕,當我輸入行數&列,並點擊提交&列的表結構將在形式生成的行數按鈕,當我將數據輸入到該表中時,我想將該數據保存到數據庫中。請,如果您有任何演示的例子,那麼請給它緊迫,我在此先感謝..保存運行時創建的表中的數據到數據庫

這是我的PHP代碼:

<?php 
include_once "dreportCreation.php"; 
     {  
      if(isset($_POST['submit'])) 
       function displaydata($column,$rows)    
        {  
         echo "<table border='1' align='center'>";   
         for($i = 0;$i<$_POST['column'];$i++)  
         { 
          echo "<tr>".$j."</tr>";        
          for($j = 0; $j <$_POST['rows'];$j++)   
           { 

            echo "<td>" ."<input type=\"text\" name='$i'>"."</td>"; 
           } 
         } 
         echo "</table>"; 
        } 
     } 
    ?> 
    <html> 
    <head> 
    <title>aa</title> 
    </head> 
    <body> 
     <form name="reportCreation" method="post" action="dd.php"> 
         <label for='Table'>Define Table</label> 
         <label for='rows'>Row</label> 
         <input type="text" name="column"></input> 
         <label for='column'>Column</label> 
         <input type="text" name="rows"></input> 
         <input type="submit" name="submit" value="submit" onclick="displaydata();"> 
     </form> 
    </body> 
    </html> 

回答

0

有很多問題,你的代碼中,首先,要定義稱爲displaydata的php函數,但在提交點擊時調用具有相同名稱的JavaScript函數。因此,頁面重新加載併發送表單之外的輸入(並且在<html>標籤之前),前提是您在php內部的任何地方調用函數...至於輸入,您應該爲每列創建一個數組輸入,如下所示:

echo "<table border='1' align='center'>";   
for($i = 0;$i<$_POST['column'];$i++)  
{ 
    echo "<tr>".$j."</tr>";        
    for($j = 0; $j <$_POST['rows'];$j++)   
    { 
      echo "<td>" ."<input type=\"text\" name='column_$i[$j]'>"."</td>"; // you should provide a beter name than simly a number ... just saying :) 
    } 
} 
echo "</table>"; 

這樣,當你在PHP的信息,你會得到$_POST['i']['j']陣列,將能夠拯救他們。不管怎樣,你只會得到每列的最後一個輸入。

工作示例可以是:

<?php 
include_once "dreportCreation.php"; 
function displaydata($column,$rows)    
{  
     echo "<table border='1' align='center'>";   
     for($i = 0;$i<$_POST['column'];$i++)  
     { 
      echo "<tr>".$j."</tr>";        
      for($j = 0; $j <$_POST['rows'];$j++)   
      { 
        echo "<td>" ."<input type=\"text\" name='column_$i[$j]'>"."</td>"; 
      } 
     } 
     echo "</table>"; 
    } 
    ?> 
    <html> 
    <head> 
    <title>aa</title> 
    </head> 
    <body> 
     <form name="reportCreation" method="post" action="dd.php"> 
         <label for='Table'>Define Table</label> 
         <label for='rows'>Row</label> 
         <input type="text" name="column"></input> 
         <label for='column'>Column</label> 
         <input type="text" name="rows"></input> 
         <input type="submit" name="submit" value="submit"> 

<?Php 
if (isset($_POST['submit'])) displaydata(); // Show data INSIDE form 
?> 

     </form> 
    </body> 
    </html> 

順便說一句,你還是要檢測,對後,將數據發送到被保存。像if (isset($_POST['column_0'])) { //start saving stuff }就可以做得很好

相關問題