I put my first working registration form (with a database connection) online @ http://www.govwa.org/test/registration.php
This is what the basic code looks like:
<form id="signupform" autocomplete="off" method="post" action="" novalidate>
<table>
<tbody>
<tr>
<td class="label"><label id="lfirstname" for="firstname">First Name</label></td>
<td class="field"><input id="firstname" name="firstname" type="text" value="" maxlength="100"></td>
<td class="status"></td>
</tr>
<tr>
<td class="label"><label id="lsignupsubmit" for="signupsubmit">Signup</label></td>
<td class="field" colspan="2"><input id="signupsubmit" name="signup" type="submit" value="Signup"></td>
</tr>
</tbody>
</table>
<?php
include('config.php');
$pdo = connect();
// adding new member using PDO with try/catch to escape the exceptions
try {
$sql = "INSERT INTO g1_members (firstname, lastname, username, password, password_confirm, email) VALUES (:firstname, :lastname, :username, :password, :password_confirm, :email)";
$query = $pdo->prepare($sql);
$query->bindParam(':firstname', $_POST['firstname'], PDO::PARAM_STR);
$query->execute();
} catch (PDOException $e) {
echo 'PDOException : '. $e->getMessage();
}
?>
</form>
Now I’m trying to figure out how to get quiz results into another database table. A key difference is that I don’t want users to have to click a Submit button to finish the quiz, then click another Submit button to send their score to the database; the data should be sent automatically.
This is what the quiz HTML looks like like:
<form action="grade.php" method="post" id="quiz">
<ol>
<li id="q9">
<div class="Question">Scientists believe the universe is:</div>
<div class="Answer">
<label for="q9-A"><div class="Radio"><input type="radio" name="q9" id="q9-A" value="A" style="display: none;"> A. disappearing</div></label></div>
<div class="Answer">
<label for="q9-B"><div class="Radio"><input type="radio" name="q9" id="q9-B" value="B" style="display: none;"> B. expanding</div></label></div>
<div class="Answer">
</li>
</ol>
<input type="hidden" name="PreviousURL" id="url" />
<input type="submit" value="Submit Quiz" />
</form>
After a user selects some answers and clicks the Submit button, they’re taken to another page (grade.php), where the score is calculated and displayed with this code:
<?php
$totalCorrect = 0;
$answers = [1 => 'A', 2 => 'Jupiter', 3 => 'C', 4 => 'D', 5 => 'A', 6 => 'C', 7 => 'C', 8 => 'B', 9 => 'B', 10 => 'ABC'];
foreach ($answers as $num => $answer)
{
$key = 'answer-'.$num;
if (isset($_POST[$key]) && $_POST[$key] === $answer)
{
$totalCorrect++;
}
}
echo '<div id="Results" style="max-width: 600px; margin: 10px auto; font-family: Verdana, Arial, sans-serif;">
<div id="Score" style="background: #009; color: #fff; text-align: center;">'.$totalCorrect.' out of 10 correct</div>';
?>
The test score is going into a field named Test_Score, so I convert $totalCorrect to the value $Test_Score and take it from there…
<?php
$Test_Score = $totalCorrect;
$pdo = connect();
try {
$sql = "INSERT INTO g1_test_scores (Test_Score) VALUES (:Test_Score)";
$query = $pdo->prepare($sql);
$query->bindParam(':Test_Score', $_POST['Test_Score'], PDO::PARAM_STR);
$query->execute();
} catch (PDOException $e) {
echo 'PDOException : '. $e->getMessage();
}
?>
Except I don’t know what I’m doing at this point. And how do you make it submit the information to a database table without a Submit button?
My ultimate goal is to set it up so that every time a person takes a quiz, the following information is sent to the database: 1) Score, 2) Quiz ID, 3) Username and 4) Date.
Thanks for any tips.