感谢您帮助我们。我有另一个问题。 在我之前的问题中,我询问如何在表单提交后使文本框消息显示在页面顶部。 感谢你们,我设法做到了这一点。 接下来的问题是如何使这种情况发生至少8次?
<?php
include('header1.php');
if(isset($_POST['submit']) && $_POST['submit']=='Submit'){
message=$_POST['message'];
$name=$_POST["name"];
echo "<center>";
echo "<div>" . $name . "<br />" . $message ."</div>";
echo "</center>";
if(isset($_POST['submit']) && isset($_POST['prevmsg']) && isset($_POST['prevname'])){
echo "<center>";
echo $_POST['prevname'];
echo "<br/>";
echo $_POST['prevmsg'];
echo "</center>";
}
?>
<html>
<head>
</head>
<body style="position:center">
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">
<table>
<tr>
<th>
<label for="name">Name</label>
</th>
<td>
<input type="text" name="name" id="name">
</td>
</tr>
<input type="hidden" name="prevname" value="<?=$name?>"/>
<tr>
<th>
<label for="message">Message</label>
</th>
<td>
<textarea name="message" id="message"></textarea>
</td>
</tr>
<input type="hidden" name="prevmsg" value="<?=$message?>"/>
</table>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
<?php
} else {
?>
<html>
<head>
</head>
<body>
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">
<table>
<tr>
<th>
<label for="name">Name</label>
</th>
<td>
<input type="text" name="name" id="name">
</td>
</tr>
<tr>
<th>
<label for="message">Message</label>
</th>
<td>
<textarea name="message" id="message"></textarea>
</td>
</tr>
</table>
<input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>
<?php }
include('inc/footer.php');
?>
答案 0 :(得分:0)
您可以使用for
循环。我还清理了你的代码,因为你有一些语法错误。由于您正在重复使用该表单,因此您可能希望将其作为一个函数。
<?php
include('header1.php');
function myForm( $name, $message ) {
?>
<form method="POST" action="<?php echo $_SERVER["PHP_SELF"]; ?>">
<input type="hidden" name="prevmsg" value="<?php echo $message; ?>" />
<input type="hidden" name="prevname" value="<?php echo $name; ?>" />
<table>
<tr>
<th><label for="name">Name</label></th>
<td><input type="text" name="name" id="name"></td>
</tr>
<tr>
<th><label for="message">Message</label></th>
<td><textarea name="message" id="message"></textarea></td>
</tr>
</table>
<input type="submit" name="submit" value="Submit" />
</form>
<?php
}
if( isset($_POST['submit']) && $_POST['submit'] == 'Submit' ) {
$message = $_POST['message'];
$name = $_POST["name"];
for($i = 0; $i < 8; $i++) {
echo "<center>";
echo "<div>" . $name . "<br />" . $message ."</div>";
echo "</center>";
}
if( isset($_POST['submit']) && isset($_POST['prevmsg']) && isset($_POST['prevname']) ) {
echo "<center>";
echo $_POST['prevname'];
echo "<br/>";
echo $_POST['prevmsg'];
echo "</center>";
}
?>
<html>
<body style="position:center">
<?php myForm( $name, $message ); ?>
</body>
</html>
<?php
}
else {
?>
<html>
<body>
<?php myForm( NULL, NULL ); ?>
</body>
</html>
<?php
}
include('inc/footer.php');
?>
答案 1 :(得分:0)
首先,message=$_POST['message'];
应为$message=$_POST['message'];
。如果你想要发生8x的事情,只需将其包裹在for循环中。
for($i=0;$i<8;$i++)
{
//code to loop here
}