php - 一个工作的 php 表单
问题描述
我无法在网上的任何地方找到一个简单的工作 php 表单。有很多表单与 HTML 代码的其余部分在同一个文件中,但是当您在同一个 index.html
文件中有 2 或 3 个表单时,这只会令人困惑。我正在寻找的只是一个基本的 HTML 表单,method='post'
其 action 属性设置为action='action.php'
,然后使其在该 .php 文件中工作。
解决方案
考虑一下您在单个 html/php 页面中的三种形式,例如
<form action="post" action="action.php">
<input type="submit" name="signin_form"/>
</form>
<form action="post" action="action.php">
<input type="submit" name="signup_form"/>
</form>
<form action="post" action="action.php">
<input type="submit" name="password_reset_form"/>
</form>
您现在可以在提交按钮上看到名称属性,当用户提交表单时,$_POST 超级全局将具有提交按钮名称的值。你可以用这个来检查这个
<?php
if(isset($_POST['signin_form']))
{
// user submitted the sign in form
}
if(isset($_POST['signup_form']))
{
// user submitted the sign up form
}
if(isset($_POST['password_reset_form']))
{
// user submitted the password reset form form
}
?>
您还可以使用表单名称的值在每个表单中添加一个隐藏字段,例如
<form action="post" action="action.php">
<input type="hidden" name="form_type" value="sign_in"/>
<input type="submit" value="submit"/>
</form>
<form action="post" action="action.php">
<input type="hidden" name="form_type" value="sign_up"/>
<input type="submit" value="submit"/>
</form>
<form action="post" action="action.php">
<input type="hidden" name="form_type" value="password_reset"/>
<input type="submit" value="submit"/>
</form>
现在你可以用同样的方式检查
<?php
if(isset($_POST['form_type']) && $_POST['form_type'] == 'sign_in')
{
// user submitted the sign in form
}
if(isset($_POST['form_type']) && $_POST['form_type'] == 'sign_up')
{
// user submitted the sign up form
}
if(isset($_POST['form_type']) && $_POST['form_type'] == 'password_reset')
{
// user submitted the password reset form form
}
?>
推荐阅读
- ios - 订阅到期日期
- php - 如何不在同一页面上重复帖子?使用 WP_Query?
- javascript - 如何在不将道具传递给该组件的情况下在组件内设置 div 样式(我正在使用包)
- haskell - 导出位置显示
- javascript - Javascript:使用 document.write() 添加样式内容
- python - 如何使用多个标签访问 groupby 数据框中的值?
- html - 使 div 与最大 div 一样大
- airflow - 如何更改 Apache Airflow 中的默认区域
- android - as3 Android访问下载文件夹中的csv文件
- reporting-services - 在 Excel 中解冻页眉