[英]Fatal error: Uncaught Error: Call to undefined function loginRelocate() Javascript
Receiving error: 接收错误:
Fatal error: Uncaught Error: Call to undefined function loginRelocate()
致命错误:未被捕获的错误:调用未定义的函数loginRelocate()
loginRelocate() is javascript function to redirect page if user is logged in. loginRelocate()是用于在用户登录后重定向页面的javascript函数。
Rest of code works. 其余代码有效。
login.php 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>
thanks 谢谢
As others have mentioned, you cannot call a JavaScript function from PHP in that way... the correct way to do it is 正如其他人提到的那样,您不能以这种方式从PHP调用JavaScript函数...正确的方法是
if(isset($_SESSION['login_user'])){
echo '<script type="text/javascript">loginRelocate();</script>';
}
or even 甚至
if(isset($_SESSION['login_user'])) {
?> <script type="text/javascript">loginRelocate();</script> <?php
}
An alternative approach would be to use PHP to redirect the user and dispense with the JavaScript entirely: 一种替代方法是使用PHP重定向用户并完全放弃JavaScript:
if(isset($_SESSION['login_user'])) {
header('Location: panel/index.php');
}
Or as a last ditch effort, use an HTML redirect: 或作为最后的努力,使用HTML重定向:
if(isset($_SESSION['login_user'])) {
?> <meta http-equiv="location" content="URL=panel/index.php" /> <?php
}
This is discouraged, however. 不鼓励这样做。
Try this: 尝试这个:
loginRelocate is a javascript function or a php function? loginRelocate是JavaScript函数还是php函数? I think that you are trying to calling a javascript function inside a php scope.
我认为您正在尝试在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>
You are calling loginRelocate
before you declare it. 在声明之前,您正在调用
loginRelocate
。
Try moving your declaration to the top of your page. 尝试将声明移到页面顶部。
<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.