如何使用PHP验证HTML文件中的表单输入


How can I use PHP to validate form input in an HTML file?

我正在制作一个下拉菜单项来登录,但我不知道如何验证表单输入。我确实有一个页面要注册,它重定向到一个PHP文件,代码如下:

<?PHP
require_once("./include/membersite_config.php");
if(isset($_POST['submitted']))
{
   if($fgmembersite->RegisterUser())
   {
        $fgmembersite->RedirectToURL("thank-you.html");
   }
}
?>

然后,此代码重定向到另一个html文件。最终,您会被重定向到login.php文件,其中包含以下代码:

<?PHP
require_once("./include/membersite_config.php");
if(isset($_POST['submitted']))
{
   if($fgmembersite->Login())
   {
        $fgmembersite->RedirectToURL("../index.html");
   }
}
?>

这一切都很好,但我的问题是,现在只能通过单独的页面登录。我希望能够通过下拉列表登录,但我不能使用与上面相同的PHP代码,因为下拉列表在html文件中。如何:

  • 使用PHP验证表单输入
  • 将用户重定向到他们单击下拉项时正在访问的页面

编辑:这是我在login.php文件中使用的表单:

<form id='login' action='<?php echo $fgmembersite->GetSelfScript(); ?>' method='post' accept-charset='UTF-8'>
<fieldset >
<h2 class="titel2">Log in</h2>
<input type='hidden' name='submitted' id='submitted' value='1'/>

<div><span class='error'><?php echo $fgmembersite->GetErrorMessage(); ?></span></div>
    <input class="veld" placeholder="Gebruikersnaam" type='text' name='username' id='username' value='<?php echo $fgmembersite->SafeDisplay('username') ?>' maxlength="50" /><br/>
    <span id='login_username_errorloc' class='error'></span>
    <input class="veld" placeholder="Wachtwoord" type='password' name='password' id='password' maxlength="50" /><br/>
    <span id='login_password_errorloc' class='error centreren'></span>
    <div class="centreren"></div><a class="anormaal" href='reset-pwd-req.php'>Wachtwoord vergeten?</a></div>
    <input class="knop2" type='submit' name='Submit' value='Log in' />
</div>
</fieldset>
</form>

您需要更具体地了解验证,下面是一个示例,您可以提供帮助吗。

if(empty($_POST['something']) === TRUE)
{
   $error[]= 'Your field is empty';
}

使用此重定向:

header('Location: 'yourphpscript.php');
exit;