0

I am creating e-learning that users will take a quiz. Upon submit they will be sent to the results page where it will show them their scores. If the user passes a link will pop up to a certificate they can print out. I cannot get the certificate to populate variables(first name, last name, etc). Here is what I have:

index.php (home page where they take the quiz and fill out a form)

<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" />&nbsp;
</label>
<label>First Name:
<input type="text" name="fname" id="fname" tabindex="2" />&nbsp;
</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>

On results.php (page that shows results and link to certificate)

<?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>

On 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>

Thank you!

1
  • 1
    your are only redirecting user to certificate.php page with the link. you have to store the data in some hidden form and pass it to certificate.php in order to access the variables.
    – Kamran
    Apr 17, 2015 at 13:30

3 Answers 3

0

You are not passing any variables to the certificate.php page. There are several options of passing data between php pages such as: 1) Sessions 2) $_POST 3) $_GET

The easiest option in your code is using the $_GET variable. Simply use this line of code in results.php:

echo "<a href=certificate.php?fname={$variables['fname']}&lname={$variables['lname']} >Print Certificate</a>"?></h1>

Than in certificate.php retrieve the data by using: <?php echo $_GET['fname'] . " " . $_GET['lname'] . ?>

Note 1: you will need to urlendode() the variables in the link in case someone puts special chairs in their name/last name Note 2: It's a hackable approach. An employee could print a certificate with just any name/last name by calling a prepared url.

The best approach would be to rewrite all three pages to use Sessions: http://php.net/manual/en/features.sessions.php

0
0

Either you go for Kamran's approach and build a hidden form which will send by POST the information needed in the certificate page, either you go for a database oriented model(i.e. save users and their results and then retrieve those information to generate the certificate).

The first way is pretty unsafe since you can change the values of the fields using Firebug and send dirty unexpected input if you don't test the values.

0

In results.php store necessary values in 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;

Then use it in certificate.php like this:

//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];

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.