嗨我试图使用php进行验证,但我想在另一个页面中进行验证,例如(make form action =" page.php")并在page.php中进行验证并发送数据 但是在页面login.html中形成 我不想使用PHP_SELF在同一页面中执行所有php代码
感谢并抱歉我的英语不好
html代码(login.html)
<form class="well" role="form" action="page.php" method="post">
<input type="text" name="user" class="form-control" placeholder="User Name"/><br><br>
<span><?php echo $userErr ?></span>
<input type="password" name="pass" class="form-control"placeholder="Password" /><br><br>
<span><?php echo $passErr ?></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>
php code(page.php)
$userErr = $passErr = "";
$user = $pass = "";
if($_SERVER['REQUEST_METHOD']=="POST"){
if(empty($_POST['user'])){
$userErr = "Please Insert Your Name";
}elseif(strlen($_POST['user'])>20){
$userErr = "Your Name Is greater than 20 char";
$user = $_POST['user'];
}
else{
$user = $_POST['user'];
}
if(empty($_POST['pass'])){
$passErr ="Pleaes your password";
}
else{
$pass = $_POST['pass'];
}
}
答案 0 :(得分:0)
我试图破译你的问题。显然,您希望在名为“login.html”的页面上有一个表单,然后通过名为“page.php”的脚本对其进行验证,然后返回“login.html”,可能仅在验证失败时。 这可以通过header()函数来完成。例如,如果要在验证失败时返回login.html页面:
<强> page.php文件强>
//Validation code here
if ($validated == true)
{
//Validation succes
}
else
{
header('Location: login.html');
}
请注意,如果您已经在页面上打印了某些内容,那么header() - 函数将无效。
答案 1 :(得分:0)
您可以这样做:
<强> test.php的强>
<?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。