在同一页面上输出的简单 html/php 表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6509570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Simple html/php form to output on the same page
提问by khalid
Basically all I want is for users to come into the site, type in their message and name, and the results should be displayed in the same page and when another user comes in again, the results from previous user should be there and anything that comes up should just be added to the list.
基本上我想要的只是让用户进入网站,输入他们的消息和姓名,结果应该显示在同一页面中,当另一个用户再次进入时,前一个用户的结果应该在那里以及任何出现的内容up 应该只是添加到列表中。
currently I have:
目前我有:
<form id="form1" name="form1" method="post" action="">
<label>Please type in a message
<input type="text" name="msg" id="msg" />
</label>
<label>and your name
<input type="text" name="pin" id="name" />
</label>
<p>
<label>Submit
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
</form>
<?php
$msg = $_POST[msg];
$name = $_POST[name];
?>
<br />
<?php echo "$msg"?>
<?php echo "$name"?>
but when another record is type in, the previous one is lost...
但是当输入另一条记录时,之前的记录丢失了......
thanks in advance
提前致谢
回答by Andrea
This should do what you want. It loads previous posts from posts.txt
, adds the current one, displays the posts and saves it. You'll need to make sure posts.txt
exists and has correct permissions.
这应该做你想做的。它从 加载以前的帖子posts.txt
,添加当前帖子,显示帖子并保存它。您需要确保posts.txt
存在并具有正确的权限。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form id="form1" name="form1" method="post" action="">
<label>Please type in a message
<input type="text" name="msg" id="msg" />
</label>
<label>and your name
<input type="text" name="name" id="name" />
</label>
<p>
<label>Submit
<input type="submit" name="submit" id="submit" value="Submit" />
</label>
</p>
</form>
<?php
$msg = $_POST["msg"];
$name = $_POST["name"];
$posts = file_get_contents("posts.txt");
$posts = "$msg - $name\n" . $posts;
file_put_contents("posts.txt", $posts);
echo $posts;
?>
</body>
</html>
回答by Nishchay Sharma
That's where a database is needed. Just make a simple sql database or create a file that is appended every time a user posts his/her message. So whenever your page loads, it must first load the previous stored data and then proceed further.
这就是需要数据库的地方。只需创建一个简单的 sql 数据库或创建一个文件,每次用户发布他/她的消息时都会附加该文件。所以每当你的页面加载时,它必须首先加载之前存储的数据,然后再继续。
回答by Steve Robbins
Couple things:
夫妇的事情:
$first = $_POST['msg'];
$second = $_POST['pin'];
You need those single quotes around the post argument in order to save it to a variable.
您需要在 post 参数周围使用单引号,以便将其保存到变量中。
Secondly, in order to store data, you need to keep it somewhere, whether it be a text file or MySQL database. Post data only exists when the form is posted. After that, it's destroyed.
其次,为了存储数据,您需要将其保存在某个地方,无论是文本文件还是 MySQL 数据库。发布数据仅在发布表单时存在。在那之后,它被摧毁了。
Probably the easiest thing for you to do is append to a text file (if this is a simple application).
可能对您来说最简单的事情是附加到文本文件(如果这是一个简单的应用程序)。
回答by Sampson
You need to persist, or save, the data from previous sessions. One options is to use a flat-file, where you save the entries into a text file. This may be okay for a very small website receiving very little traffic. Essentially, when somebody posts something you would prepend it only the contents of a local file.
您需要保留或保存以前会话中的数据。一种选择是使用平面文件,将条目保存到文本文件中。这对于接收很少流量的非常小的网站来说可能没问题。本质上,当有人发布某些内容时,您只会在其前面添加本地文件的内容。
The other option is to look into a database option, such as MySQL. With this method you will insert each message/name combination into a local database, and then pull out all records (or the nth most recent) and display them below your comment-form.
另一种选择是查看数据库选项,例如 MySQL。使用此方法,您将每个消息/名称组合插入本地数据库,然后拉出所有记录(或第 n 个最近的记录)并将它们显示在您的评论表单下方。
回答by Nobody
You only display the last post you did. Which will actually never enable other users to see what you have posted. To store this over a period of time you have to use files or a databases.
你只显示你做的最后一个帖子。这实际上永远不会让其他用户看到您发布的内容。要在一段时间内存储它,您必须使用文件或数据库。
回答by Sam152
You are going to need to look at storing these messages in a database system. MySQL is a good option, but it's a lot more than a few simple lines to setup. You can start by reading the manual. This should provide you with everything you need.
您将需要考虑将这些消息存储在数据库系统中。MySQL 是一个不错的选择,但它不仅仅是几行简单的设置。您可以从阅读手册开始。这应该为您提供所需的一切。
回答by marchaos
I'm not sure I'm following you. Do you want the text box to have the previous type value?
我不确定我是否在跟踪你。您希望文本框具有以前的类型值吗?
<input type="text" value="<?php (isset($_POST[msg])) ? $_POST[msg] : "" ?>" name="msg" id="msg" />