0

谢谢你们帮助我。我有另一个问题。在我之前的问题中,我询问了如何在表单提交后使文本框消息显示在页面顶部。感谢你们,我设法做到了。下一个问题是如何使这种情况发生至少 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');
 ?>
4

2 回答 2

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');
?>
于 2013-01-18T16:16:04.273 回答
0

那么首先message=$_POST['message'];应该是$message=$_POST['message'];。如果你想让某件事发生 8 倍,只需将其包装在 for 循环中即可。

for($i=0;$i<8;$i++)
{
//code to loop here
}
于 2013-01-18T16:16:56.813 回答