您好我有关于生成行和列的问题。我想问一下如何才能在一个页面上制作它。这就是我尝试过的。
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页中使用它。感谢。
答案 0 :(得分: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
答案 1 :(得分: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?
答案 2 :(得分:1)
通过仅使用一个页面来实现这一点,只需将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>