[英]JavaScript not working when in the top of PHP page
When I put the This Code
section in the top, the JavaScript for calling the functions UserExist
and Success
is not working. 当我将“
This Code
部分放在顶部时,用于调用UserExist
和Success
函数的JavaScript无法正常工作。 On the other hand when I put it in the bottom, the JavaScript is working, but the header("Location:Login.php")
is not working. 另一方面,当我将其放在底部时,JavaScript正常运行,但
header("Location:Login.php")
无效。 I tried to change its place multiple times, but no luck. 我试图多次更改其位置,但没有运气。 Please, can someone explain what is wrong and how to fix it?
请,有人可以解释出什么问题以及如何解决吗?
<?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>
Do sth like: 做某事:
<?php
//do all the redirecting stuff etc
$code="";
if($sth){
$code="<script>alert(':)');</script>";
}
?>
<html>
<head>
<?php echo $code;?>
</head>
<body>
if $sth is true, you will see a :)
</body>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.