谢谢大家的帮助。我还有一个问题。在我之前的问题中,我问如何使文本框消息在表单提交后显示在页面顶部。我能做到这一切都要感谢你们。下一个问题是如何让这种情况发生至少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');
?>
您可以使用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');
?>
首先message=$_POST['message'];
应该是$message=$_POST['message'];
。如果你想让某件事发生8次,只需将它包装在for循环中。
for($i=0;$i<8;$i++)
{
//code to loop here
}