繁体   English   中英

致命错误:未捕获错误:调用未定义函数loginRelocate()Javascript

[英]Fatal error: Uncaught Error: Call to undefined function loginRelocate() Javascript

接收错误:

致命错误:未被捕获的错误:调用未定义的函数loginRelocate()

loginRelocate()是用于在用户登录后重定向页面的javascript函数。

其余代码有效。

login.php

<html>
<head>
   <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script> 
</head>
<body>
<?php include('submit2.php');
if(isset($_SESSION['login_user'])){
   loginRelocate();
}
?>
<div id="content" class="content">
   <div class="content-heading">
      <div class="content-heading-title">
      <h3>Login</h3>
      </div>
   </div>
   <div class="content-info">
   <form id="myForm" method="post">
      <label for="username">Username:</label>
      <input name="username" id="username" type="text" /><br />
      <label for="password">Password:</label>
      <input name="password" id="password" type="password" /><br /> 
      <input type="button" id="submitFormData" onclick="SubmitFormData();" value="Submit" />
   </form>
   <div id="results"></div>
   </div>
</div>
<script>
   function SubmitFormData() {
      var username = $("#username").val();
      var pass = $("#password").val();
      $.post("submit2.php", { 
         username: username, 
         pass: pass
      }, function(data) {
         $('#results').html(data);
         $('#myForm').trigger('reset');
      });
    }
</script>
<script type="text/javascript">
   function loginRelocate(){
      window.location.replace("panel/index.php");
   });
</script>
</body>
</html>

谢谢

正如其他人提到的那样,您不能以这种方式从PHP调用JavaScript函数...正确的方法是

if(isset($_SESSION['login_user'])){
   echo '<script type="text/javascript">loginRelocate();</script>';
}

甚至

if(isset($_SESSION['login_user'])) {
   ?> <script type="text/javascript">loginRelocate();</script> <?php
}

一种替代方法是使用PHP重定向用户并完全放弃JavaScript:

if(isset($_SESSION['login_user'])) {
   header('Location: panel/index.php');
}

或作为最后的努力,使用HTML重定向:

if(isset($_SESSION['login_user'])) {
   ?> <meta http-equiv="location" content="URL=panel/index.php" /> <?php
}

不鼓励这样做。

尝试这个:
loginRelocate是JavaScript函数还是php函数? 我认为您正在尝试在php范围内调用javascript函数。

<?php include('submit2.php'); ?>
<html>
<head>
   <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script> 
</head>
<body>
<div id="content" class="content">
   <div class="content-heading">
      <div class="content-heading-title">
      <h3>Login</h3>
      </div>
   </div>
   <div class="content-info">
   <form id="myForm" method="post">
      <label for="username">Username:</label>
      <input name="username" id="username" type="text" /><br />
      <label for="password">Password:</label>
      <input name="password" id="password" type="password" /><br /> 
      <input type="button" id="submitFormData" onclick="SubmitFormData();" value="Submit" />
   </form>
   <div id="results"></div>
   </div>
</div>
<script>
   function SubmitFormData() {
      var username = $("#username").val();
      var pass = $("#password").val();
      $.post("submit2.php", { 
         username: username, 
         pass: pass
      }, function(data) {
         $('#results').html(data);
         $('#myForm').trigger('reset');
      });
    }
</script>
<script type="text/javascript">
   function loginRelocate(){
      window.location.replace("panel/index.php");
   });
</script>

<?php
if(isset($_SESSION['login_user'])){
   echo "<script>loginRelocate();</script>";
}
?>

</body>
</html>

在声明之前,您正在调用loginRelocate
尝试将声明移到页面顶部。

<head>
   <script src="https://code.jquery.com/jquery-2.2.0.min.js"></script>
    <script type="text/javascript">
        function loginRelocate(){
        window.location.replace("panel/index.php");
   });
</script>   
</head>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM