[英]display username based results, when logged in as another user (teacher tracking)
我在下面的php頁面中嘗試模擬搜索特定用戶名(存儲在數據庫中)的簡單示例,然后單擊“提交”,該用戶的結果將顯示在頁面上。 目前,該代碼僅顯示為登錄用戶顯示的結果。如何使它適應代碼以作為另一個用戶進行搜索和跟蹤?
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相關的代碼,僅當用戶登錄時才允許加載頁面:
session_start();
if (!isset($_SESSION['username']) or ($_SESSION['username'] == '')) {
header("Location: login.php");
}
?>
顯示當前登錄用戶測驗結果的php代碼:
<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>
重申一下,作為php新手,我想要一個解決方案(在現有上下文中提供了代碼),該解決方案涉及如何使用搜索按鈕查找用戶名,然后在頁面中提供SEARCHED FOR用戶名的信息,而不是而不是已登錄的用戶名。我嘗試了各種操作,無法弄清楚邏輯。
我試過的
遵循以下原則:
<form action="/action_page.php">
Username: <input type="text" name="username1" value="username1"><br>
和:
$a = new NewQuizScore;
$scores = $a->getScores($_SESSION['username1']);
我試圖將在文本框中輸入的內容鏈接到getScores會話,但是顯然這不起作用:
根據建議的答案進行更新-仍然無效:
<?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>
您需要使用$_POST
superglobal從表單獲取值。 $_POST
不如$_GET
透明,因此我將其用於您的表單:
<!-- ADD METHOD ------------------------------->
<!------------------------------vvvvvvvvvvvvv-->
<form action="/action_page.php" method="post">
Username: <input type="text" name="username1" value=""><br>
<input type="submit" value="Submit">
</form>
發布表單時,可以使用以下腳本。 注意,由於您的表單指向/action_page.php
,因此該腳本應位於該位置:
# 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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.