0

I'm trying to redirect using PHP header/even a JavaScript work around call inside php to my clients.php webpage.

The problem is that PHP header/the work around loads the webpage flawlessly but the Javascript still isn't ran.

Additional notes is that after the header or even Javascript redirect to the page, trying to refresh the page won't have the Javascript load. Only switching between a webpage and coming back to clients.php does the Javascript actually run.

I have also tried this: JavaScript not loading after PHP header() redirect

but to no avail.

Here are parts of my code segmented, just because its quite lengthy.

clients.php

<!-- Bootstrap core JavaScript-->
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Core plugin JavaScript-->
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>
<!-- Page level plugin JavaScript-->
<script src="vendor/chart.js/Chart.min.js"></script>
<script src="vendor/datatables/jquery.dataTables.js"></script>
<script src="vendor/datatables/dataTables.bootstrap4.js"></script>
<!-- Custom scripts for all pages-->
<script src="js/sb-admin.min.js"></script>
<!-- Custom scripts for this page-->
<script src="js/sb-admin-datatables.min.js"></script>
<script src="js/sb-admin-charts.min.js"></script>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" ty    pe="text/javascript"></script>
    <link href="../../css/toastr.css" rel="stylesheet"/>
    <script src="../../js/toastr.js"></script>
    <script type="text/javascript">
            var tmp = "<?php $tmp = $_SESSION['message_success']; echo "$tmp"; ?>";
            alert(tmp + " h");
            $( document ).ready(function() {
                    <?php
                        if($_SESSION['sysLogin'] == "success") {
                            $_SESSION['sysLogin'] = "";
                            $user = $_SESSION['user'];
                            echo "toastr.success('Welcome $user', 'User has logged in');";
                        }
                        if($_SESSION['message_success'] != "") {
                            $msg = $_SESSION['message_success'];
                            $_SESSION['message_success'] = "";
                            echo "toastr.success('$msg', 'Success!');";
                        }
                        if($_SESSION['message_error'] != "") {
                            $msg = $_SESSION['message_error'];
                            $_SESSION['message_error'] = "";
                            echo "toastr.error('$msg', 'Error!');";
                        }
                        if($_SESSION['message_warning'] != "") {
                            $msg = $_SESSION['message_warning'];
                            $_SESSION['message_warning'] = "";
                            echo "toastr.warning('$msg', 'Warning!');";
                        }
                     ?>
            });
    </script>
    </div>
    </body>

    </html>

script.php

  if($update_statement) {
      $_SESSION['message_success'] = "$_membername's Careplan has been successfully updated.";
      echo "<script type='text/javascript'>window.location.href = '../clients.php';</script>";
      //header("Location:". $redirect);
      //exit();
    }

Note that clients.php and script.php are not in the same file.

Per Riggs request

<?php
session_start();
$message="";
if(isset($_POST["login"])) {
    $redirect = NULL;
    if($_POST['redirect'] != '') {
        $redirect = $_POST['redirect'];
    }
    if(empty($_POST['username']) || empty($_POST['password'])) {
        $message = "Both fields must be filled out.";
        $_SESSION['sysLogin'] = "$message";
        header("location: ../login.php?redirect=" . urlencode($redirect));
    } else {

        require('server_connection.inc');
        $connection = connect_to_db(DB_SERVER, DB_UN, DB_PWD, DB_NAME);

        $user=mysqli_real_escape_string($connection, $_POST['username']);
        $pass=mysqli_real_escape_string($connection, $_POST['password']);
        $statement = "select * from Credentials where UserName='$user' AND Password='$pass';";
        $result = $connection->query($statement);

        if($result->num_rows == 1) {
            // lets determine the type of user that logged in
            // if not employee but CEO, Manager, ETC, its an Admin
            $employeeid = ($result->fetch_assoc())["EmployeeID"];
            $check = "select Employees.Position, Employees.Name, Employees.ID from Credentials, Employees where (Credentials.EmployeeID = Employees.ID) AND Employees.ID = '$employeeid';";
            $result_two = $connection->query($check);
            if($result->num_rows == 1) {
                $the_row = $result_two->fetch_assoc();
                if($the_row["Position"] == "Employee") {
                    $name = $the_row["Name"];
                    $the_id = $the_row['ID'];
                    $_SESSION['logon'] = true;
                    $_SESSION['user'] = "$name";
                    $_SESSION['type'] = "employee";
                    $_SESSION['sysLogin'] = "success";
                    $_SESSION['user_id'] = $the_id;
                    mysqli_close($conection);
                    if($redirect) {
                          header("Location:". $redirect);
                    } else {
                        header("location: ../index.php");
                    }
                    exit();
                } else if($the_row["Position"] == "CEO" || $the_row["Position"] == "Manager") {
                    $name = $the_row["Name"];
                    $the_id = $the_row['ID'];
                    $_SESSION['logon'] = true;
                    $_SESSION['user'] = "$name";
                    $_SESSION['type'] = "admin";
                    $_SESSION['sysLogin'] = "success";
                    $_SESSION['user_id'] = $the_id;
                    mysqli_close($conection);
                    if($redirect) {
                            header("Location:". $redirect);
                    } else {
                        header("location: ../index.php");
                    }
                    exit();
                }
            } else {
                $message = "Unable to Parse Employee. Please contact your sites Administrator.";
                $_SESSION['sysLogin'] = "$message";
                mysqli_close($conection);
                header("location: ../login.php?redirect=" . urlencode($redirect));
            }
        } else if($result->num_rows == 0){
            $message = "Incorrect username or password";
            $_SESSION['sysLogin'] = "$message";
            mysqli_close($conection);
            header("location: ../login.php?redirect=" . urlencode($redirect));
        } else {
            $message = "Database Login Error. Too many retrieved accounts. Please contact your sites Administrator.";
            $_SESSION['sysLogin'] = "$message";
            mysqli_close($conection);
            header("location: ../login.php?redirect=" . urlencode($redirect));
        }
    }
}

function write_to_log($message) {
    $file = fopen("logfile.txt", "w") or die("Unable to open file!");
    fwrite($file, "$message\n");
    fclose($file);
}

function connect_to_db($server, $username, $pwd, $dbname) {
    $conn = mysqli_connect($server, $username, $pwd);
    if(!$conn) {
            echo "" . mysqli_error($conn);
            exit;
    }
    $dbh = mysqli_select_db($conn, $dbname);
    if(!$dbh) {
        echo "" . mysqli_error($conn);
        exit;
    }
    return $conn;
}
?>

This script merely links session while also checking for the login validity of the user

The message passing does indeed work with headers to another file 'dashboard.php' but redirecting from script.php to dashboard.php doesn't yield the message, thus an implication still arises.

14
  • This may be a silly question but... Are you sure it gets into the if statement? Commented Mar 6, 2018 at 17:25
  • I DONT SEE A session_start() anywhere in the code Commented Mar 6, 2018 at 17:30
  • Add error reporting to the top of your file(s) while testing right after your opening PHP tag for example <?php error_reporting(E_ALL); ini_set('display_errors', 1); to see if it yields anything. Commented Mar 6, 2018 at 17:37
  • Or simply look at your php error logs Commented Mar 6, 2018 at 17:37
  • Also, you have to be sure that there is no output before the call to header(). Unexpected error messages or even one space before <?php will stop it from working. Commented Mar 6, 2018 at 17:40

2 Answers 2

1

The tmp JavaScript variable is not being defined since you haven't started the session. Include session_start(); at the top of your code.

<?php session_start(); ?>
<!-- Bootstrap core JavaScript-->
<script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- Core plugin JavaScript-->
<script src="vendor/jquery-easing/jquery.easing.min.js"></script>
<!-- Page level plugin JavaScript-->
<script src="vendor/chart.js/Chart.min.js"></script>
<script src="vendor/datatables/jquery.dataTables.js"></script>
<script src="vendor/datatables/dataTables.bootstrap4.js"></script>
<!-- Custom scripts for all pages-->
<script src="js/sb-admin.min.js"></script>
<!-- Custom scripts for this page-->
<script src="js/sb-admin-datatables.min.js"></script>
<script src="js/sb-admin-charts.min.js"></script>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js" ty    pe="text/javascript"></script>
    <link href="../../css/toastr.css" rel="stylesheet"/>
    <script src="../../js/toastr.js"></script>
    <script type="text/javascript">
            var tmp = "<?php $tmp = $_SESSION['message_success']; echo "$tmp"; ?>";
            alert(tmp + " h");
            $( document ).ready(function() {
                    <?php
                        if($_SESSION['sysLogin'] == "success") {
                            $_SESSION['sysLogin'] = "";
                            $user = $_SESSION['user'];
                            echo "toastr.success('Welcome $user', 'User has logged in');";
                        }
                        if($_SESSION['message_success'] != "") {
                            $msg = $_SESSION['message_success'];
                            $_SESSION['message_success'] = "";
                            echo "toastr.success('$msg', 'Success!');";
                        }
                        if($_SESSION['message_error'] != "") {
                            $msg = $_SESSION['message_error'];
                            $_SESSION['message_error'] = "";
                            echo "toastr.error('$msg', 'Error!');";
                        }
                        if($_SESSION['message_warning'] != "") {
                            $msg = $_SESSION['message_warning'];
                            $_SESSION['message_warning'] = "";
                            echo "toastr.warning('$msg', 'Warning!');";
                        }
                     ?>
            });
    </script>
    </div>
    </body>

    </html>

Sign up to request clarification or add additional context in comments.

Comments

0

To my surprise, the issue wasn't with the Javascript at all nor positioning like the thread posted suggested. Rather the issue seemed to have been with my PHP and calling $_membersname.

For advice to trouble shoot this later on, make sure no errors are outputted, even if PHP doesn't find any. This is from a variable that hasn't be declared.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.