我正在创建用户将进行测验的电子学习。提交后,他们将被发送到结果页面,在那里它将显示他们的分数。如果用户通过链接将弹出一个证书,他们可以打印出来。我无法获得证书来填充变量(名字,姓氏等)。 这就是我所拥有的:
index.php(他们参加测验并填写表格的主页)
<form id="form1" name="form1" method="post" action="results.php" onsubmit="return validateForm() ;">
<label>Last Name:
<input type="text" name="lname" id="lname" tabindex="1" />
</label>
<label>First Name:
<input type="text" name="fname" id="fname" tabindex="2" />
</label>
<label>Title:
<input type="text" name="title" id="title" tabindex="3" /><br/><br/>
</label>
<?php
$quiz = new Quiz( $questions );
echo $quiz->renderQuiz();
?>
<input name="Submit" type="submit" value="Submit" />
</form>
在results.php上(显示结果和链接到证书的页面)
<?php
$ip = $_SERVER['REMOTE_ADDR']; // employee's Ip address
$time = date("d/m/y : H:i:s", time()); // current timestamp
$email_string = create_email_string( $force_email, $_POST['email_to'] );
$questions_correct = array();
$info_to_save = array( $_POST['lname'], $_POST['fname'], $_POST['title'];
// Grades the quiz
$score = (string)round( ( count($questions_correct) / count($questions)*100), 2 );
$variables = array();
$variables['fname'] = $_POST['fname'];
$variables['lname'] = $_POST['lname'];
$variables['test_name'] = $test_name;
$variables['score'] = $score;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Results</title>
<link rel="stylesheet" type="text/css" href="./stylesheets/quiz_stylesheet.css" />
</head>
<body>
<div id="white">
<div id="header">
<img src="./images/ucdheader.jpg" width="1000" id="header" /img>
</div>
<div id="mainContent">
<h1><span>You Scored <?php echo $score ?>%</span>
<br><?php if ($score >= 80)
echo "<a href=certificate.php >Print Certificate</a>
"?></h1>
<p>Below are your results for <?php echo $test_name; ?>. In order to pass this quiz, you must score 80% or better. If you would like to try again, click the following link.</p>
<a href='<?php echo $test_page_name ?>'>Try again</a>
<p>These results were sent to the following addresses: </p>
<ul>
<?php
$all_emailed_to = explode( "; " , $email_string);
foreach ($all_emailed_to as $email) {
echo "<li>" . $email . "</li>";
}
?>
</ul>
</div>
</div>
</body>
</html>
在certificate.php上
<title>Certificate</title>
</head>
<body>
<div id="white">
<div class="btn-group btn-group-lg">
<a role="button" class="btn btn-default" href="#" data- function="print">Print Certificate</a>
</div>
<div class="certificate">
<img src="images/cert.png" width="820" height="527" id="cert" />
<div class="name">
<?php $variables['fname'] . " " . $variables['lname'] . ?>
</div>
<div class="course">
<p></p>
</div>
<div class="completion_date">
<?php $variables['time']; ?>
</div>
</div>
</div>
</body>
</html>
谢谢!
答案 0 :(得分:0)
要么你采用Kamran的方法并构建一个隐藏的表单,它将通过POST发送证书页面中所需的信息,要么你去一个面向数据库的模型(即保存用户及其结果,然后检索那些生成证书的信息。)
第一种方法非常不安全,因为您可以使用Firebug更改字段的值,如果您不测试值,则会发送脏的意外输入。
答案 1 :(得分:0)
您没有将任何变量传递给certificate.php页面。在php页面之间传递数据有几种选择,例如: 1)会议 2)$ _POST 3)$ _GET
代码中的最简单选项使用$ _GET变量。只需在 results.php :
中使用以下代码 echo "<a href=certificate.php?fname={$variables['fname']}&lname={$variables['lname']} >Print Certificate</a>"?></h1>
比 certificate.php 中使用以下方法检索数据:
<?php echo $_GET['fname'] . " " . $_GET['lname'] . ?>
注1:如果有人在名字/姓氏中放置特殊的椅子,您需要urlendode()
链接中的变量
注2:这是一种不可思议的方法。员工可以通过调用准备好的URL来打印任何名称/姓氏的证书。
最好的方法是重写所有三个页面以使用会话:http://php.net/manual/en/features.sessions.php
答案 2 :(得分:0)
在results.php中,在SESSION中存储必要的值:
$info_to_save = array( $_POST['lname'], $_POST['fname'], $_POST['title'], time);
//Start session if not started
if (!isset($_SESSION)) {
session_start();
}
$_SESSION['certInfo'] = $info_to_save;
然后在certificate.php中使用它,如下所示:
//Start session if not started
if (!isset($_SESSION)) {
session_start();
}
$lname = $_SESSION['certInfo'][0];
$fname = $_SESSION['certInfo'][1];
$title = $_SESSION['certInfo'][2];
$time = $_SESSION['certInfo'][3];