Simple html/php form to output on the same page
Basically all I want is for users to come into the site, type开发者_JS百科 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.
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
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.
<!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>
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.
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.
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.
Probably the easiest thing for you to do is append to a text file (if this is a simple application).
This should get you started.
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.
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.
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.
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" />
精彩评论