2014-09-29 61 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>