when it comes to PHP, everyone knows it's used to make a website. So let's implement a simple page form submission!
In the last section we said the installation of the PHP development environment and a basic Hello PHP program implementation, hello PHP program is the introduction, you just see the front of a path to the incomparable light, then today we set foot on this road!
1, first enter the Wamp www directory, in order to facilitate the management of our new Demo1 directory, in this directory to create a new text document, renamed test.html, Code:
This is a simple HTML page, if the HTML is not familiar with, you can see www.w3school.com.cn/, here is a detailed syntax introduction and online debugging functions, personally feel is a very good learning platform.
Here you can see that there is a form, the form is submitted as a post, submitted to the test.php page. Obviously, we need to implement a test.php code behind it. We first go to the browser access: localhost/demo1/test.html, we see the page as follows:
See the page we usually visit the look of it, but it is really ugly, but it does not matter, we are heavy in the implementation of technology and Function! Keep looking down.
2, the new test.php file, directly paste code:
<?php$title = $_post[' title '; $content = $_post["Content"];file_put_contents ("Data.txt", $title. ",". $content. " \ n ", file_append); echo" <a href= ' test.html > Continue adding </a> "; echo" <a href= ' show.php ' > View </a> ";? >
The first two lines get the data from the title and content two fields that the form is post to and save in the $title and $conten variables, and then call the File_put_contents method to save the data in the Data.txt file. After two behavior add two jump links, continue to add jump back to our fill out the form of the page, view jumps to the show.php page.
3,show.php
The role of the show.php page is to read the Data.txt data and display it on the page:
<?php$content = file_get_contents ("Data.txt");//echo $content; Echo str_replace ("\ n", "<br>", $content);? >
The first line of code reads the data in the data.txt into the variable $content and then displays it on the page. But since we're saving the data with a newline character \ n, and the <br> in HTML, we're replacing all of them with the <br> that HTML can parse correctly.
4. Testing
Visit The localhost/demo1/test.html page and fill in the title and content to successfully submit the data and view it.
Think of our usual online forum posting is this, of course, this is just a discussion. If you want to achieve the function of the forum, we still study hard, after all, this is just embark on the road to success!