1.在php中表单的提交是一种十分重要的数据提交方式。
比如登录注册,购物车等都要用到表单提交。
2.建立一个简单的表单
~~~
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<form action="welcome.php" method="post">
名字:<input type="text" name="fname"><br><br>
年龄:<input type="text" name="age"><br><br>
<input type="submit"value="提交">
</form>
</body>
</html>
~~~
名字:
年龄:
3、在form表单中action规定了当提交表单时,向何处发送表单的数据。action="welcome.php"就是规定了数据要提交到welcome.php文件处理。method是规定提交的方式,我们这里用的是post提交方式。
action是规定当提交表单时,向何处发送表单数据。
method是规定提交的方式
`<form action="welcome.php"method="post">`
4、然后给input标签定义一个name,只有定义了name属性,后台才能准确的接收到数据。
名字: `<input type="text" name="fname"><br><br>`
name:定义名称。
5、为让我们提交的数据能够在页面输出,我们进入welcome.php文件处理我们提交的数据。
6、在welcome.php中使用echo输出$_POST接收数据。$_POST['fname']、$_POST['age']分别接收名字和年龄的值,fname、age就是在input中name属性定义的属性值。
~~~
<?php
header('content-type:text/html;charset=utf-8');
echo '欢迎' . $_POST['fname'] . '<br>';
echo '你的年龄是' . $_POST['age'] . '岁';
~~~
### 例子中换成get也是同样的流程
### php页面 接收值则为 $_grt['fname']
# 例子代码
## html页面
~~~
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
</head>
<body>
<form action="welcome.php" method="post">
名字:<input type="text" name="fname"><br><br>
年龄:<input type="text" name="age"><br><br>
<input type="submit"value="提交">
</form>
</body>
</html>
~~~
## welcome.php页面
~~~
<?php
header('content-type:text/html;charset=utf-8');
echo '欢迎' . $_POST['fname'] . '<br>';
echo '你的年龄是' . $_POST['age'] . '岁';
?>
~~~