简体   繁体   中英

display username based results, when logged in as another user (teacher tracking)

I have the following php page in which I am trying to simulate a simple example of searching for a specific username (stored in the database) and when "submit" is clicked, the results for that user are displayed on the page. At the moment, the code shows the results displayed only for the user that is logged in. How do I adapt it to fit with the code to search and track as another user?

HTML:

<p>Welcome, teachers! Here you can track student progress.</p>
<p>Enter the username of the student you wish to 'view' results for</p>
<form action="/action_page.php">
    Username: <input type="text" name="FirstName" value="Mickey"><br>
    <input type="submit" value="Submit">
</form>

Php related code that only allows the page to load if the user is logged in:

session_start();
if (!isset($_SESSION['username']) or ($_SESSION['username'] == '')) {
    header("Location: login.php");

}
?>

Php code that displays the currently logged in user's quiz results:

    <table class="table table-hover" style="text-align:center;">
        <thead>
          <tr>
            <th style="text-align:center;">Quiz</th>
            <th style="text-align:center;">Score (%)</th>
            <th style="text-align:center;">Certificate</th>
            <th style="text-align:center;">Retake</th>
          </tr>
        </thead>
        <tbody>
        <?php
        $a = new NewQuizScore;
        $scores = $a->getScores($_SESSION['username']);
        foreach ($scores as $score) {
            echo ("<tr>");
            echo ("<td>".$score[0]. "</td> ");
            echo ("<td>".$score[1]. "</td> ");
            echo ('<td><a href="certificate.php?name='.$score[0].'&score='.$score[1].'">Print Certificate</a></td>');
            echo ("<td><a href=\"quiz-show.php?quiz=01_PythonBasics". "\">Take it again!</a></td> ");

            echo ("</tr>");
        }
        ?>
        </tbody>
        </table>
    </div>
    </div>

To reiterate, as a php newbie, I would like a solution (with code in my existing context provided) for how to use the search button to look up a username, and then provide the information for the SEARCHED FOR username in the page, rather than for the username that is logged in. I've tried various things and cannot come up with the logic.

What I've tried:

Along the lines of:

<form action="/action_page.php">
    Username: <input type="text" name="username1" value="username1"><br>

and:

$a = new NewQuizScore;
$scores = $a->getScores($_SESSION['username1']);

I was trying to link what is entered in the text box, to the getScores session, but obviously this doesn't work:

Update based on suggested answer - still doesn't work:

<?php
require 'includes/functions.php';
include_once 'config.php';
include 'includes/newquizscore.php';

session_start();
if (!isset($_SESSION['username']) or ($_SESSION['username'] == '')) {
    header("Location: login.php");        
}
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
    <form action="#" method="post">
        Username: <input type="text" name="username1" value=""><br>
        <input type="submit" value="Submit">
    </form>
    <?php
    $username = (!empty($_POST['username1']))? $_POST['username1'] : $_SESSION['username'];
    $a = new NewQuizScore;
    # This is assuming this action is what you use to get the user's data
    $scores = $a->getScores($username)
    ?>
</body>
</html>

You need to get the value from the form using the $_POST superglobal . $_POST is less transparent than $_GET , so I would use that for your form:

<!-- ADD METHOD ------------------------------->
<!------------------------------vvvvvvvvvvvvv-->
<form action="/action_page.php" method="post">
    Username: <input type="text" name="username1" value=""><br>
    <input type="submit" value="Submit">
</form>

When you post the form, you can use the script below. Note, since your form is pointing to /action_page.php , that is where this script should be located:

# Fetch the post value OR the current logged in (depending if form submitted or not)
# This is assuming you are searching by username1 value from form
# (since that is what you have in your form). You can use functions like
# trim() to remove empty spaces before and after submitted value to clean
# the input up a bit
$username = (!empty($_POST['username1']))? $_POST['username1'] : $_SESSION['username'];
$a = new NewQuizScore;
# This is assuming this action is what you use to get the user's data
$scores = $a->getScores($username);

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.

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