2014-09-29 75 views
0

嗨我有一個關於生成行和列的問題。想問一下我怎樣才能在一個頁面上做到這一點。這是我嘗試過的。表生成器

HTML:

<html> 
<head> 
<title>Table Generator</title> 

<body> 
<center><h1>Generate Your Table</h1></center> 

<div id="div1"> 
<center><h4>Enter number of Row and Column</h4> 
    <form action="get_table/execute_table.php" method="POST"> 
    <label for="title">Row</label> 
    <input type="text" name="title1" placeholder="Row"> 
    <br> 
    <label for="title">Column</label> 
    <input type="text" name="title2" placeholder="Column"> 
    <br> 
    <input type="submit" name="submit" value="Generate Table"> </center> 
    </form> 

</div> 



</body> 

PHP:

<?php 

$row = $_POST['title1']; 
$column = $_POST['title2']; 

echo "<table border='1'>"; 

for($tr=1;$tr<=$row;$tr++){ 

echo "<tr>"; 
    for($td=1;$td<=$column;$td++){ 
      echo "<td>row: ".$tr." column: ".$td."</td>"; 
    } 
echo "</tr>"; 
} 

echo "</table>"; 
?> 

是,它完全運行,但我只希望它在1頁。謝謝。

回答

3

通常情況下,如果你想在同一頁上,你只是省略action=""其價值。

然後,當然,把PHP進程在同一個頁面的形式:

<div id="div1"> 
<center><h4>Enter number of Row and Column</h4> 
    <form action="" method="POST"> 
    <!--  ^^ no more value, well you could just put the same filename --> 
    <label for="title">Row</label> 
    <input type="text" name="title1" placeholder="Row"> 
    <br> 
    <label for="title">Column</label> 
    <input type="text" name="title2" placeholder="Column"> 
    <br> 
    <input type="submit" name="submit" value="Generate Table"> </center> 
    </form> 

</div> 

<?php 

$out = ''; // initialize a string holder and when the submission is done, concatenate all the strings 
if(isset($_POST['submit'])) { // catch submission button 

    $row = $_POST['title1']; 
    $column = $_POST['title2']; 

    $out .= "<table border='1'>"; 

    for($tr=1;$tr<=$row;$tr++){ 

    $out .= "<tr>"; 
     for($td=1;$td<=$column;$td++){ 
       $out .= "<td>row: ".$tr." column: ".$td."</td>"; 
     } 
    $out .= "</tr>"; 
    } 

    $out .= "</table>"; 

} 

echo $out; // finally echo it 
+0

感謝@Ghost的想法。 :) – 2014-09-29 04:03:20

+1

@DontStopLearning很高興這有幫助 – Ghost 2014-09-29 04:05:32

1

您需要發佈到同一頁面,並檢查PhP Post陣列是否有數據。

<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST"> 

注:

替換爲形式的行動雖然大部分(?所有)瀏覽器都支持自職位有一個空白的動作,這在技術上並不符合W3C標準。

然後,當頁面被提交時,它將重新加載相同的頁面並填充POST數組。某處您的網頁上添加類似於一個條件:

if($_POST['title']){ 
    //do whatever get_table/execute_table.php did 
}else{ 
    //echo the form here or, if you're allowed, use an include() 
} 

更多信息自我發帖: How do I make a PHP form that submits to self?

+0

這個if($ _ POST [title]){'需要被引用=> if($ _ POST ['title']){否則,你會得到一個未定義的常量錯誤。 – 2014-09-29 03:56:09

+0

修好了,謝謝。 – CyberEd 2014-09-29 04:07:50

+0

不客氣。 – 2014-09-29 04:08:07

1

只用一個頁面剛剛離開action屬性空實現這一目標。並添加top.and你的PHP模塊,確保將其保存爲.php然後把你的PHP代碼塊上的所有HTML的頂部


所以最後它看起來像這樣

<?php 

    $row = $_POST['title1']; 
    $column = $_POST['title2']; 

    echo "<table border='1'>"; 

    for($tr=1;$tr<=$row;$tr++){ 

    echo "<tr>"; 
     for($td=1;$td<=$column;$td++){ 
       echo "<td>row: ".$tr." column: ".$td."</td>"; 
     } 
    echo "</tr>"; 
    } 

    echo "</table>"; 
    ?> 

    <html> 
    <head> 
    <title>Table Generator</title> 

    <body> 
    <center><h1>Generate Your Table</h1></center> 

    <div id="div1"> 
    <center><h4>Enter number of Row and Column</h4> 
    <form action="" method="POST"> 
    <label for="title">Row</label> 
    <input type="text" name="title1" placeholder="Row"> 
    <br> 
    <label for="title">Column</label> 
    <input type="text" name="title2" placeholder="Column"> 
    <br> 
    <input type="submit" name="submit" value="Generate Table"> </center> 
    </form> 

    </div> 



    </body>