gpt4 book ai didi

javascript - fatal error : Uncaught Error: Call to undefined function loginRelocate() Javascript

转载 作者:行者123 更新时间:2023-11-30 09:52:50 24 4
gpt4 key购买 nike

接收错误:

Fatal error: Uncaught Error: Call to undefined function loginRelocate()

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

其余代码有效。

登录.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
}

然而,这是不鼓励的。

关于javascript - fatal error : Uncaught Error: Call to undefined function loginRelocate() Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35490788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com