dongxu4580 2019-01-23 12:59
浏览 43

每次提交表单时,从文本字段连接并显示用户输入

I'm trying to create a messenger style application. First off I'm just trying to understand how to concatenate user conversations into a variable each time a form is submitted and then display the result. However, my code overwrites the previous message so only displays the most recent. I've created a very basic example below for demonstration leaving out form validation for clarity. I would really appreciate some help wit this.

Thanks in advance for taking the time to read or respond to this post. It's always appreciated :)

<?php 

$message .= $_REQUEST["message"];

?>

<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<title>Messanger</title>
</head>

<body>

<p id="message" >
<?php echo "Message: " . $message; ?>
</p>

<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

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

</form>

</body>
</html>
  • 写回答

1条回答 默认 最新

  • dsjpik057730 2019-01-23 13:05
    关注

    You are getting only recent value what about old values.

    You need to store the values in session and whenever we get more values, append it.

    Corrected code

    <?php 
    session_start();
    $message = isset($_POST['message']) ? $_POST['message'] : '';
    if (! empty($message)) {
     if (! isset($_SESSION['message'])) {
      $_SESSION['message'] = $message;
     }
     else {
      $_SESSION['message'] .= "<br/>". $message;
     }
    }
    echo $_SESSION["message"];
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看