首页 > 解决方案 > 一个工作的 php 表单

问题描述

我无法在网上的任何地方找到一个简单的工作 php 表单。有很多表单与 HTML 代码的其余部分在同一个文件中,但是当您在同一个 index.html文件中有 2 或 3 个表单时,这只会令人困惑。我正在寻找的只是一个基本的 HTML 表单,method='post'其 action 属性设置为action='action.php',然后使其在该 .php 文件中工作。

标签: phpformsaction

解决方案


考虑一下您在单个 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
}

?>

推荐阅读