Php 是否有其他方法在其他页面中进行验证?
您好,我尝试使用php进行验证,但我想在另一个页面(make form action=“page.php”)中进行验证,并在page.php中发送数据 但是表单在page login.html中 我不喜欢使用php_SELF在同一页面中编写所有php代码 谢谢,很抱歉我的英语不好 html代码(login.html)Php 是否有其他方法在其他页面中进行验证?,php,validation,Php,Validation,您好,我尝试使用php进行验证,但我想在另一个页面(make form action=“page.php”)中进行验证,并在page.php中发送数据 但是表单在page login.html中 我不喜欢使用php_SELF在同一页面中编写所有php代码 谢谢,很抱歉我的英语不好 html代码(login.html) 我试图解释你的问题。显然,您希望在名为“login.html”的页面上创建一个表单,然后使用名为“page.php”的脚本对其进行验证,然后返回到“login.html”,可能只有
我试图解释你的问题。显然,您希望在名为“login.html”的页面上创建一个表单,然后使用名为“page.php”的脚本对其进行验证,然后返回到“login.html”,可能只有在验证失败的情况下。 这可以通过header()函数完成。例如,如果您想在验证失败时返回login.html-page: page.php
//Validation code here
if ($validated == true)
{
//Validation succes
}
else
{
header('Location: login.html');
}
<?php
// Init
$errors = array();
// Handle Post
if (count($_POST))
{
// Check Login Fields
if (empty($_POST['user']))
$errors['user'] = 'You have not entered user name.';
else if (strlen($_POST['user']) > 20)
$errors['user'] = 'Your Name Is greater than 20 char';
if (empty($_POST['pass']))
$errors['pass'] = 'You have not entered password.';
// Proceed If No Erros Occured
if (!sizeof($errors))
{
// do something with the posted data
}
}
?>
<form class="well" role="form" action="test.php" method="post">
<input type="text" name="user" class="form-control" placeholder="User Name"/><br><br>
<span><?php echo isset($errors['user']) ? $errors['user'] : ''; ?></span>
<input type="password" name="pass" class="form-control"placeholder="Password" /><br><br>
<span><?php echo isset($errors['pass']) ? $errors['pass'] : ''; ?></span>
<input type="submit" value="Login" class="btn btn-success" style="margin-left: 45px;"/>
<a data-toggle="modal" href="#myModal" class="btn btn-info">forget your password?</a>
</form>
请注意,如果已将某些内容打印到页面上,header()-函数将不起作用。您可以执行以下操作: test.php
//Validation code here
if ($validated == true)
{
//Validation succes
}
else
{
header('Location: login.html');
}
<?php
// Init
$errors = array();
// Handle Post
if (count($_POST))
{
// Check Login Fields
if (empty($_POST['user']))
$errors['user'] = 'You have not entered user name.';
else if (strlen($_POST['user']) > 20)
$errors['user'] = 'Your Name Is greater than 20 char';
if (empty($_POST['pass']))
$errors['pass'] = 'You have not entered password.';
// Proceed If No Erros Occured
if (!sizeof($errors))
{
// do something with the posted data
}
}
?>
<form class="well" role="form" action="test.php" method="post">
<input type="text" name="user" class="form-control" placeholder="User Name"/><br><br>
<span><?php echo isset($errors['user']) ? $errors['user'] : ''; ?></span>
<input type="password" name="pass" class="form-control"placeholder="Password" /><br><br>
<span><?php echo isset($errors['pass']) ? $errors['pass'] : ''; ?></span>
<input type="submit" value="Login" class="btn btn-success" style="margin-left: 45px;"/>
<a data-toggle="modal" href="#myModal" class="btn btn-info">forget your password?</a>
</form>
单击“提交”而不填写表单时呈现如下效果:
这在您的脚本上下文中可能看起来是正确的,因为我没有任何css。不清楚您在问什么。你能试着重新表述你的问题吗?(请使用正确的标点符号)那么你有什么问题?创建
page.php
,将当前验证代码移到该页,然后继续。@amalMurali当我按照您所说的那样做时,我不能回显变量$userErr-like image。。它获取未定义的变量在问题中发布相关代码。发布login.php和page.phpgreat的代码。。但是我想要test.php中的php代码和login.html中的html。