2
當我將This Code
部分置於頂部時,用於調用函數UserExist
和Success
的JavaScript不起作用。另一方面,當我把它放在底部時,JavaScript正在工作,但header("Location:Login.php")
不起作用。我試圖多次改變它的位置,但沒有運氣。請有人能解釋什麼是錯的,以及如何解決它?在PHP頁面頂部時JavaScript不工作
<?php
error_reporting(E_ALL & ~E_NOTICE);
error_reporting(E_ERROR | E_PARSE);
session_start();
?>
/* This Code */
<?php
if(isset($_POST["Register"]))
{
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "dbuseraccounts";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user = $_POST['Username'];
$pass = $_POST['Password'];
$query = mysqli_query($conn, "SELECT * FROM users WHERE Username= '".$user."'");
if(mysqli_num_rows($query) > 0)
{
echo "<script type='text/javascript'>UserExist();</script>";
}
else
{
$insertdata = "INSERT INTO `users` (`Username`, `Password`, `FirstName`, `LastName`, `Email`, `ContactNumber`)
VALUES ('".$_POST["Username"]."','".$_POST["Password"]."','".$_POST["FirstName"]."','".$_POST["LastName"]."','".$_POST["Email"]."','".$_POST["ContactNumber"]."')";
if ($conn->query($insertdata) === TRUE)
{
echo "<script type='text/javascript'>Success();</script>";
header("Location:Login.php");
}
else
{
echo "<script type= 'text/javascript'>alert('Error: " . $sql . "<br>" . $conn->error."');</script>";
}
}
$conn->close();
}
?>
/* This Code */
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Register</title>
<link href="css/bootstrap.css" rel="stylesheet" type="text/css">
<link href="css/stylesheet.css" rel="stylesheet" type="text/css">
<style type="text/css">
body {
margin-left: 0px;
margin-right: 0px;
}
</style>
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
<script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<ul class="topnav" id="myTopnav">
<li class="li_left"><a href="Home.php">Home</a></li>
<li class="li_left"><a href="Services.php">Services</a></li>
<li class="li_left"><a href="Promo.php">Promo</a></li>
<li class="li_left"><a href="AboutUs.php">About Us</a></li>
<li class="li_left"><a href="ContactUs.php">Contact Us</a></li>
<li class="li_left"><a href="Appointment.php">Appointment</a></li>
<li class="li_left"><a id="appointmentlist" href="Appointmentlist.php">Appointment List</a></li>
<li class="li_right"><a id="logout" href="logout.php">Logout</a></li>
<li class="li_right"><a id="user" href="User.php"><?php echo $_SESSION['Username']; ?></a></li>
<li class="li_right"><a id="login" href="Login.php">Log In</a></li>
<li class="li_right"><a id="register" href="Register.php">Register</a></li>
<li class="icon">
<a href="javascript:void(0);" onclick="myFunction()">☰</a>
</li>
</ul>
<div style="height:620px"; >
<h1 class="heading" >Register</h1>
<form id="RegisterForm" name="RegisterForm" method="post">
<div class="FormElement"><input name="Username" type="text" autofocus required="required" class="TField" id= "Username" maxlength="20" placeholder="Username"></div>
<div class="FormElement"><input name="Password" type="password" required="required" class="TField" id="Password" maxlength="20" placeholder="Password"></div>
<div class="FormElement"><input name="FirstName" type="text" autofocus required="required" class="TField" id= "FirstName" maxlength="20" placeholder="First Name"></div>
<div class="FormElement"><input name="LastName" type="text" required="required" class="TField" id="LastName" maxlength="20" placeholder="Last Name"></div>
<div class="FormElement"><input name="Email" type="email" required="required" class="TField" id="Email" maxlength="30" placeholder="Email Address"></div>
<div class="FormElement"><input name="ContactNumber" type="number" required="required" class="TField" id="ContactNumber" maxlength="20" placeholder="Contact Number"></div>
<div class="FormElement">
<div id="alert">
<span class="closebtn" onclick="this.parentElement.style.display='none';">×</span>
User Already Exists
</div>
<div id="success">
<span class="closebtn" onclick="this.parentElement.style.display='none';">×</span>
You Are Now Registered
</div>
<input name="Register" type="submit" class="button" id="Register" value="Register">
</div>
</form>
</div>
<div class="footer">
<footer></footer>
</div>
</div>
<script src="js/jquery-1.11.3.min.js"></script>
<script src="js/bootstrap.js"></script>
<script>
function myFunction() {
var x = document.getElementById("myTopnav");
if (x.className === "topnav") {
x.className += " responsive";
} else {
x.className = "topnav";
}
}
</script>
<script>
function UserExist()
{
document.getElementById("alert").style.display = "block";
}
</script>
<script>
function Success()
{
document.getElementById("success").style.display = "block";
}
</script>
<script>
function IfLoginAdminFunction()
{
document.getElementById("login").style.display = "none";
document.getElementById("register").style.display = "none";
document.getElementById("logout").style.display = "block";
}
</script>
<script>
function IfLoginNotAdminFunction()
{
document.getElementById("login").style.display = "none";
document.getElementById("register").style.display = "none";
document.getElementById("appointmentlist").style.display = "none";
document.getElementById("logout").style.display = "block";
}
</script>
<script>
function IfNotLoginFunction()
{
document.getElementById("logout").style.display = "none";
document.getElementById("user").style.display = "none";
document.getElementById("appointmentlist").style.display = "none";
}
</script>
<?php
if (isset($_SESSION['login']) && $_SESSION['login'] == true)
//if login
{
if($_SESSION['Type'] == 1)
{
echo "<script type='text/javascript'>IfLoginAdminFunction();</script>";
}
elseif($_SESSION['Type'] == 0)
{
echo "<script type='text/javascript'>IfLoginNotAdminFunction();</script>";
}
}
//if not login
else
{
echo "<script type='text/javascript'>IfNotLoginFunction();</script>";
}
?>
</body>
</html>
你就是呼應之前的代碼 –