【问题标题】:Table Generator表生成器
【发布时间】:2014-11-23 09:25:20
【问题描述】:

您好,我有一个关于生成行和列的问题。想问一下我怎样才能让它只在一页中。这是我尝试过的。

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>"; 
?>

是的,它已完全运行,但我只希望它在一页中。谢谢。

【问题讨论】:

    标签: php html


    【解决方案1】:

    通常,如果您希望它在同一页面上,您只需省略 action="" 及其值。

    那么当然是把php进程和form放在同一个页面:

    <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
    

    【讨论】:

    • 感谢@Ghost 的想法。 :)
    • @DontStopLearning 很高兴这有帮助
    【解决方案2】:

    要通过只使用一页来实现这一点,只需将 action 属性留空。并在顶部添加您的 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>
    

    【讨论】:

      【解决方案3】:

      你需要post到同一个页面,检查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?

      【讨论】:

      • 这个if($_POST[title]){ 需要被引用 => if($_POST['title']){ 否则,你会得到一个未定义的常量错误。
      猜你喜欢
      • 2017-03-07
      • 2014-11-01
      • 2011-03-01
      • 2013-08-12
      • 2010-11-11
      • 2011-04-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多