表单提交后在同一页面上显示文本框消息

时间:2013-01-18 11:42:40

标签: php textbox

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

 <?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 } ?>

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

提交的新详细信息下方

3 个答案:

答案 0 :(得分: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 } ?>

答案 1 :(得分: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;?>

答案 2 :(得分: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>

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