Use PHP to create a message book without database

Source: Internet
Author: User
Tags format current time html tags php and php definition regular expression reset

This paper introduces a way to realize the basic function of the message by reading the text file, and debugging through Php4+apache for Win98. From this program we can understand the PHP and HTML language interactive embedded flexible programming style.

First, enter the message this page, we need to use

Top of form

The form submits the message to the person, and uses the PHP processing information, may realize these two functions in the default.php, has the detailed annotation source code as follows:

# default.php#
The conditional statement is used to determine whether the form is submitted, and whether the content is completed;
if (sent== "sent") {
if (name== "") {echo "Please enter user name";}
else if (email== "") {echo "Please enter email";}
else if (note== "") {echo "Please enter a message";}
else {fp=fopen ("Text.txt", "A +");//read-write to open text.txt file for storing messages, if not, create a new one;
t = Date (Y-year m-month D-Day H, M minute s second);//get current time;
main = "Online name: <a href=\" mailto: $email \ "> name</a>:(T) <br>
Message content: <a href=\ "text.txt\" > Note</a> <br>Write=fwrite (FP, main); Write to file;
Fclose (FP);
echo "Thank you for your message =>";
echo "<a href=\" body.php\ "> View message <a>|<a href=\" index.htm\ "> Return home <a>";
<form method= "POST" action= "Default.php?sent" >
Please enter your name:<br>
<input type= "text" name= "name" ><br>
Please enter your email:<br>.
<input type= "text" name= "email" ><br>
Please input your message:<br>
<textarea name= "Note" cols= "rows=" 5 "></textarea>
<input type= "hidden" name= "sent" value= "sent" >
<input type= "Submit" name= "Submit" value= "OK" >
<input type= "reset" name= "Submit2" value= "reset" >

Where the code in the,<form> label field is a standard HTML language, used to enter the visitor's information, the submitted message is sent to the current page (default.php), note that there is a line in <form>:

<input type= "hidden" name= "sent" value= "sent" >

The contents of the line are not displayed, only the completion of the variable sent assign a value of "sent", when the form is not submitted, the variable sent value is empty, <?php ...? > Conditional Judgment sent== "sent" is not tenable, PHP program does not do any operation, only after submitting the following PHP program. When you are done, you can view the message through the "View Message" link, which is the body.php program we have given below:

# body.php#
f = fopen ("Text.txt", "A +"); Read and write to open the file, if it does not exist, new
msg = Fread (f,filesize ("Text.txt")); Reading files
Fclose (f); Close File
Print "<center> msg</center>"; Output message
echo "<a href=\" Default.php\ "> Back to Message Book |</a>";
echo "<a href=\" index.htm\ "> Return home </a>";

I believe you can easily read this code. In the above two sections of the program, we omitted the other HTML tags, in fact, you can put the above two pieces of code in the Label field, the page decoration, PHP definition code can also be placed between the
In this case, the file default.php, body.php, and Text.txt (found in body.php) are placed in the same directory as the server, and the reader can adjust as needed. If you are interested, on the basis of this example can also add other functions, such as the message entered the information content or format of the review (with regular expression) and the message of the management, message information access.

Bottom of form

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.