Receiving error:
Fatal error: Uncaught Error: Call to undefined function loginRelocate()
loginRelocate() is javascript function to redirect page if user is logged in.
Rest of code works.
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
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:
if(isset($_SESSION['login_user'])) {
header('Location: panel/index.php');
}
Or as a last ditch effort, use an HTML redirect:
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? I think that you are trying to calling a javascript function inside a php scope.
<?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.
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>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.