PHP在另一个页面上显示错误
这是我的剧本。我想在字段为空以及与正则表达式不匹配时打印错误。但是当我提交表单时,错误显示在另一页上。我想在表单字段下方的同一页上分别显示错误PHP在另一个页面上显示错误,php,regex,validation,Php,Regex,Validation,这是我的剧本。我想在字段为空以及与正则表达式不匹配时打印错误。但是当我提交表单时,错误显示在另一页上。我想在表单字段下方的同一页上分别显示错误 <?php //Connects to your Database mysql_connect("localhost", "root", "") or die(mysql_error()); mysql_select_db("login") or die(mysql_error()); //Che
<?php
//Connects to your Database
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("login") or die(mysql_error());
//Checks if there is a login cookie
if(isset($_COOKIE['ID_my_site']))
//if there is, it logs you in and directes you to the members page
{
$username = $_COOKIE['ID_my_site'];
$pass = $_COOKIE['Key_my_site'];
$check = mysql_query("SELECT * FROM users WHERE username = '$username'")or die(mysql_error());
while($info = mysql_fetch_array( $check ))
{
if ($pass != $info['password'])
{
}
else
{
header("Location: members.php");
}
}
}
//if the login form is submitted
if (isset($_POST['submit'])) { // if form has been submitted
// makes sure they filled it in
if(!$_POST['username'] | !$_POST['pass']) {
die('You did not fill in a required field.');
}
// checks it against the database
if (!get_magic_quotes_gpc()) {
$_POST['email'] = addslashes($_POST['email']);
}
$check = mysql_query("SELECT * FROM users WHERE username = '".$_POST['username']."'")or die(mysql_error());
//Gives error if user dosen't exist
$check2 = mysql_num_rows($check);
if ($check2 == 0) {
die('That user does not exist in our database. <a href=add.php>Click Here to Register</a>');
}
while($info = mysql_fetch_array( $check ))
{
$_POST['pass'] = stripslashes($_POST['pass']);
$info['password'] = stripslashes($info['password']);
$_POST['pass'] = md5($_POST['pass']);
//gives error if the password is wrong
if ($_POST['pass'] != $info['password']) {
die('Incorrect password, please try again.');
}
else
{
// if login is ok then we add a cookie
$_POST['username'] = stripslashes($_POST['username']);
$hour = time() + 3600;
setcookie(ID_my_site, $_POST['username'], $hour);
setcookie(Key_my_site, $_POST['pass'], $hour);
//then redirect them to the members area
header("Location: members.php");
}
}
}
else
{
// if they are not logged in
?>
<form action="<?php echo $_SERVER['PHP_SELF']?>" method="post">
<table border="0">
<tr>
<td colspan=2><h1>Login</h1></td>
</tr>
<tr>
<td>Username:</td>
<td><input type="text" name="username" maxlength="40">
</td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" name="pass" maxlength="50">
</td>
</tr>
<tr>
<td colspan="2" align="right"><input type="submit" name="submit" value="Login">
</td>
</tr>
</table>
</form>
<?php
}
?>
有很多方法可以做到这一点
1) 如果您只需要在PHP中进行验证,那么在出现错误的情况下也会显示表单以及错误消息
2) 您可以使用Ajax进行验证并显示错误(如果有),而无需重新加载页面
3) 您可以将javascript验证与PHP(服务器)验证一起添加。尝试将退出代码>在完成所有标题
功能之后…您可以通过两种方式实现这一点:
==编辑==
我没有对此进行任何测试,因此可能会出现一些错误:)
重新加载页面时:
将表单的操作
属性设置为'
,或将其设置为当前页面(或简单地将其ommit)
例如:
这意味着当用户提交表单时,页面将重新加载,但数据将发送到用户已经在的同一页面上(在这种情况下,验证也应该在该页面上)
第二种(更优雅一点的方式)是使用AJAX发送表单,并根据结果集显示任何错误。为此,您可能希望使用jQuery,因为它确实简化了表单数据的发送和返回值的处理。您可以在获取jQuery
html:
<form id="myForm">
<!-- Your inputs here -->
<input type="submit" value="Submit this form! />
</form>
PHP文件和验证的简化版本(注意:PHP5.4+语法!)
您可以使用javascript(AJAX)发送表单。使用jquery并查看$.post()以开始:)PS:您是指同一页吗?或者在不重新加载页面的情况下显示错误?相同的页面。我也更喜欢不重新加载。如果您可以帮助分享您的想法
$('#myFormId').submit(function(){
// Submit the form using AJAX and expect a JSON return value
$.post('my_validation_file.php', $(this).serialize(), function(result) {
if (result != 'success') {
// Set an error class on all fields that failed
// according to our resultset
for (var i in result) {
$('[name="' + result[i] + '"]').addClass('error');
}
}
}, 'json');
// This prevents the form from submitting
// and thus reloading your page!
return false;
});
<?php
$errorFields = [];
$result = '';
if (empty($_POST['email'])) {
$errorFields[] = 'email';
}
$result = count($errorFields) ? $errorFields : 'success';
die(json_encode($resrult));