Tôi tương đối mới đối với PHP và đã cạn kiệt Internet cố gắng tìm câu trả lời cho vấn đề này. Tôi đã xem xét vô số ví dụ nhưng mọi người dường như rất khác với hệ thống đăng nhập của tôi và tôi gặp khó khăn khi giải mã nó.Làm thế nào để bạn trả về lỗi biểu mẫu đăng nhập vào cùng một trang bằng cách sử dụng PHP?
Đây là mã của tôi cho đến nay:
index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Video for Education Log In</title>
<link rel="stylesheet" type="text/css" href="style.css" />
</head>
<body>
<div id="wrapper">
<div id="header">
<div id="logo">
videoedu.edu </div>
<div id="menu">
<ul>
<li><a href="account.html" class="menua">Create Account</a></li>
<li><a href="about.html" class="menua">About Us</a></li>
</ul>
</div>
</div>
<br><br><br><br>
<div id="page">
<div id="content">
<h2>Video for Education helps you connect and share with the videos in your life.</h2>
<h3>Upload Share Create Using video for your education purposes. Lecturers Welcome
Upload Share Create Using video for your education purposes. Lecturers Welcome
Upload Share Create Using video for your education purposes. Lecturers Welcome</h3>
<div class= "form">
<form name="login" method="post" action="checklogin.php">
Username: <input type="text" name="myusername" id="myusername" class="textb"/><br />
Password : <input type="password" name="mypassword" id="mypassword" class="textb"/><br />
<br>
<input type="submit" name="login" value="Login" id="login" class="texta" />
</form>
</div>
</div>
</div>
</div>
</body>
</html>
checklogin.php
<?php
$host = "localhost";
$username = "root";
$password = "";
$db_name = "test";
$tbl_name = "members";
mysql_connect("$host", "$username", "$password")or die("Cannot connect.");
mysql_select_db("$db_name")or die("Cannot select DB.");
$myusername=$_POST["myusername"];
$mypassword=$_POST["mypassword"];
if ($myusername&&$mypassword)
{
$myusername = stripslashes($myusername);
$mypassword = stripslashes($mypassword);
$myusername = mysql_real_escape_string($myusername);
$mypassword = mysql_real_escape_string($mypassword);
$sql = "SELECT * FROM $tbl_name WHERE username='$myusername' and password='$mypassword'";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
if($count == 1){
session_register("myusername");
session_register("mypassword");
header("location:login_success.php");
}
else
{
echo "Wrong Username or Password";
}
}
else
echo "You have left one or more fields blank.";
?>
login_success.php
<?
session_start();
if(!isset($_SESSION['myusername'])){
header("location:account.html");
}
echo "Welcome, ".$_SESSION['myusername']." - You are now logged in.<br>";
echo "<a href=logout.php>Logout</a>"
?>
<html>
<body>
</body>
</html>
logout.php
<?php
session_start();
session_destroy();
echo "You have been logged out, <a href='index.php'>click here</a> to return."
?>
Tôi đã thử chèn này vào index.html và thay đổi tên tập tin để index.php.
$submit = $_POST["login"];
if($submit)
{
}
... nhưng nó chỉ liên tục hiển thị một trong các lỗi ('Tên người dùng hoặc mật khẩu') xuống dưới cùng của trang bất cứ lúc nào.
Tôi muốn nó để nếu người dùng nhập sai tên người dùng hoặc mật khẩu hoặc để trống trường bắt buộc, lỗi sẽ xuất hiện trên cùng một trang, thay vì chuyển sang trang PHP trống, trống mới có lỗi tin nhắn ở góc trên cùng bên trái.
Việc kiểm tra các khoảng trống được yêu cầu dễ dàng hơn với JavaScript (jQuery hoặc các khung công tác khác hoạt động tốt). Trước khi trả lời thông báo tên người dùng hoặc mật khẩu sai, trong đó tệp (có thể là div) bạn cần hiển thị thông báo đó? Làm rõ điều này, tôi sẽ đăng câu trả lời của tôi. –