我有以下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>
答案 0 :(得分:0)
您需要使用$_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);