1

我有一个关于在页面顶部显示文本框消息的问题。我使用这个代码 -

 <?php
if(isset($_POST['submit']) && $_POST['submit']=='Submit'){
 $message=$_POST['message'];
 $name=$_POST["name"];
 echo $name . "<br />" . $message;?>
 <html>
 <head>
 </head>
 <body>
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">

    <br>Name:<input type="text" name="name" id="name">

    <br>Message:<textarea name="message" id="message"></textarea>

    <br><input type="submit" name="submit" value="Submit"/>

   </form>
   </body>
   </html>
   <?php    
  }
  else {  
    ?>
 <html>
 <head>  
 </head>
 <body>
 <form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">

    <br>Name:<input type="text" name="name" id="name">

    <br>Message:<textarea name="message" id="message"></textarea>

    <br><input type="submit" name="submit" value="Submit"/>

</form>
</body>
</html>
<?php } ?>

如何保存以前输入的详细信息并将其显示在表单提交的新详细信息下方

4

3 回答 3

1

您可以像这样在隐藏字段中传递上一条消息

<?php
if(isset($_POST['submit']) && isset($_POST['prevmsg'])){

 echo "New Message :".$_POST['message'];
 echo "Previous Message :".$_POST['prevmsg'];

}

if(isset($_POST['submit']) && $_POST['submit']=='Submit'){
 $message=$_POST['message'];
 $name=$_POST["name"];
 echo $name . "<br />" . $message;?>
 <html>
 <head>
 </head>
 <body>
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">

    <br>Name:<input type="text" name="name" id="name">

    <br>Message:<textarea name="message" id="message"></textarea>
        <input type="hidden" name="prevmsg" value="<?=$message?>"/>
    <br><input type="submit" name="submit" value="Submit"/>

   </form>
   </body>
   </html>
   <?php    
  }
  else {  
    ?>
 <html>
 <head>  
 </head>
 <body>
 <form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">

    <br>Name:<input type="text" name="name" id="name">

    <br>Message:<textarea name="message" id="message"></textarea>

    <br><input type="submit" name="submit" value="Submit"/>

</form>
</body>
</html>
<?php } ?>
于 2013-01-18T11:46:16.807 回答
0

一个肮脏的解决方案,让你继续......

<?php
  if(isset($_POST['submit']) && $_POST['submit']=='Submit'){
  $message=$_POST['message'];
  $name=$_POST["name"];
  if( isset( $_COOKIE['previous_message'] ) ) {
    $the_previous = explode(';', $_COOKIE['previous_message']);
    echo $the_previous[0] . "<br />" . $the_previous[1];?>
  } else {
    setcookie('previous_message', $name . ';' . $message);
  }
  setcookie('previous_message', $name . ';' . $message);
  echo $name . "<br />" . $message;?>
于 2013-01-18T11:58:06.427 回答
0

@Sibu,试试这个。

<?php
function display(){
    if(isset($_POST['submit']) && $_POST['submit']=='Submit'){
        $message=$_POST['message'];
        $name=$_POST["name"];
        echo $name . "<br />" . $message;?>
    }
}
?>
<html>
<head>
</head>
<body>
<?php display(); ?>
<form method="POST" action="<?=$_SERVER["PHP_SELF"]?>">
    <br>Name:<input type="text" name="name" id="name">
    <br>Message:<textarea name="message" id="message"></textarea>
    <br><input type="submit" name="submit" value="Submit"/>
</form>
</body>
</html>

移动该功能,以便您可以指定要显示消息的位置。希望能帮助到你。

于 2013-01-18T12:18:24.807 回答