Home >Backend Development >PHP Tutorial >A brief discussion on PHP form submission, a brief discussion on form form_PHP tutorial
Processing GET requests
The function implemented is to display "Hello XXX" on the page after entering the name
Create html file hello.html:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>欢迎</title> </head> <body> <form action="hello.php" method="get"> <input name="name" type="text"/> <input type="submit"/> </form> </body> </html>
Create PHP file hello.php:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/6/30 * Time: 15:03 */ header("Content-type: text/html; charset=utf-8"); if(isset($_GET['name'])&&$_GET['name']){//如果有值且不为空 echo 'Hello '.$_GET['name']; }else{ echo 'Please input name'; }
The Get request explicitly places the form data in the URI, and has restrictions on the length and data value encoding, such as: http://127.0.0.1/hello.php?name=Vito
Handling POST requests
Implement a simple addition function
Create html file add.html:
<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title>相加</title> </head> <body> <form action="add.php" method="post"> <input name="num1" type="text"/> + <input name="num2" type="text"/> <input type="submit" value="相加"/> </form> </body> </html>
Create PHP file add.php:
<?php /** * Created by PhpStorm. * User: Administrator * Date: 2015/6/30 * Time: 18:02 */ if($_POST['num1']&&$_POST['num2']){ echo $_POST['num1']+$_POST['num2']; }else{ echo 'Please input num'; }
Post request puts the form data in the http request body, and there is no length limit
form action="" means: form is the form, action is the redirection address, that is, where the form needs to be submitted
The above is the entire content of this article, I hope you all like it.