JavaScript 在 PHP 页面顶部时不起作用

标签 javascript php html

当我将 This Code 部分放在顶部时,用于调用函数 UserExistSuccess 的 JavaScript 无法正常工作。另一方面,当我把它放在底部时,JavaScript 可以工作,但是 header("Location:Login.php") 不工作。我试图多次改变它的位置,但没有成功。拜托,有人可以解释什么是错误的以及如何解决它吗?

<?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()">&#9776;</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';">&times;</span>
        User Already Exists
    </div>
     <div id="success">
        <span class="closebtn" onclick="this.parentElement.style.display='none';">&times;</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>

最佳答案

做某事,例如:

<?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>

关于JavaScript 在 PHP 页面顶部时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39689850/

相关文章:

javascript - 如何在 Sequelize.js + Postgres.js 中给出主键和外键关系

Javascript 重新加载以加载新实例而不重新加载页面

php - 引用 - 这个错误在 PHP 中意味着什么?

javascript - 单击特定元素时暂停 HTML 音频

javascript - 带有百分比的图像大小会减慢网站速度

javascript - 标签页(或浏览器)关闭时的 Chrome onbeforeunload 事件

PHP isset 和 javascript

php - php/apache 中的多线程

html - 覆盖用户代理样式表 - Chrome

c# - 代码中的访问控制属性