Dies ist ähnlich einer vorherigen Frage, die ich über die Übereinstimmung der ausgewählten Optionsfeld mit dem Wert im Eingabefeld fragte.PHP So drucken Sie ausgewählte Antwort in grün
Mit diesem Code kann ein Benutzer ein Quiz erstellen, indem er eine Frage und 4 mögliche Antworten eingibt. Sie müssen eine der Antworten als die richtige auswählen und auf einer separaten PHP-Seite müssen die Antworten mit der richtigen Antwort grün angezeigt werden. Ursprünglich wurde dieses Problem gelöst, aber nachdem ich den Code für die Validierung geändert hatte, funktionierte die ursprüngliche Schleife für die Anzeige nicht. Hier
ist der Ausgang des Code:
I Albany muß in grün gedruckt werden, da es die richtige Antwort ist, und wurde mit der Radio-Button ausgewählt.
Hier ist mein Code für das Formular:
<?php
session_start();
// Define variables and set to empty values
$questionErr = $answer0Err = $answer1Err = $answer2Err = $answer3Err = "";
$question = $answer0 = $answer1 = $answer2 = $answer3 = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$valid = True;
if (empty($_POST['question'])) {
$questionErr = "Please supply a question";
$valid = False;
} else {
$question = test_input($_POST['question']);
$_SESSION['question'] = $_POST['question'];
}
if (empty($_POST['answer0'])) {
$answer0Err = "Please supply a possible answer";
$valid = False;
} else {
$answer0 = test_input($_POST['answer0']);
$_SESSION['answer0'] = $_POST['answer0'];
}
if (empty($_POST['answer1'])) {
$answer1Err = "Please supply a possible answer";
$valid = False;
} else {
$answer1 = test_input($_POST['answer1']);
$_SESSION['answer1'] = $_POST['answer1'];
}
if (empty($_POST['answer2'])) {
$answer2Err = "Please supply a possible answer";
$valid = False;
} else {
$answer2 = test_input($_POST['answer2']);
$_SESSION['answer2'] = $_POST['answer2'];
}
if (empty($_POST['answer3'])) {
$answer3Err = "Please supply a possible answer";
$valid = False;
} else {
$answer3 = test_input($_POST['answer3']);
$_SESSION['answer3'] = $_POST['answer3'];
}
}
// Function to sanitize data
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
// If valid, send to QuestionReview.php to display answers
if ($valid) {
$_SESSION['radio'] = $_POST['radio'];
header('location: QuestionReview.php');
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>User-Created Quiz</title>
<style>
.shadow {
-webkit-box-shadow: 3px 3px 5px 6px #ccc; /* Safari 3-4, iOS 4.0.2 - 4.2, Android 2.3+ */
-moz-box-shadow: 3px 3px 5px 6px #ccc; /* Firefox 3.5 - 3.6 */
box-shadow: 3px 3px 5px 6px #ccc; /* Opera 10.5, IE 9, Firefox 4+, Chrome 6+, iOS 5 */
}
.instructions {
color: #696D6E;
}
#form-background {
background-color: #ECEDE8;
}
.error {
color: red;
}
</style>
</head>
<body>
<div style="width:600px">
<fieldset id="form-background" class="shadow">
<h1 class="instructions" style="text-align:center">User-Created Quiz</h1>
<p class="instructions" style="text-align:center">
<strong>Please enter a question of your own,
along with 4 possible answers in the
form below. Be sure to select the
correct answer to your question
before submitting the form.</strong>
</p>
<form style="text-align:center;" method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<br>
<label class="instructions" for="question" >Enter your question here</label><br>
<input type="text" name="question" size="50" value='<?php echo $question;?>' />
<span class="error">* <br /><?php echo $questionErr; ?></span>
<br><br>
<p class="instructions">
Please provide four answers to your question and select the
correct one.
</p>
<input type="radio" name="radio" value="answer0">
<input type="text" name="answer0" value="<?php echo $answer0; ?>" style="width:400px">
<span class="error">* <br /><?php echo $answer0Err; ?></span>
<br><br>
<input type="radio" name="radio" value="answer1">
<input type="text" name="answer1" value="<?php echo $answer1; ?>" style="width:400px">
<span class="error">* <br /><?php echo $answer1Err; ?></span>
<br><br>
<input type="radio" name="radio" value="answer2">
<input type="text" name="answer2" value="<?php echo $answer2; ?>" style="width:400px">
<span class="error">* <br /><?php echo $answer2Err; ?></span>
<br><br>
<input type="radio" name="radio" value="answer3">
<input type="text" name="answer3" value="<?php echo $answer3; ?>" style="width:400px">
<span class="error">* <br /><?php echo $answer3Err; ?></span>
<br><br>
<input type="submit" value="Submit Entry">
</form>
</fieldset>
</div>
</body>
</html>
Und hier mein Code für die Ergebnisseite ist:
<?php
session_start();
// Pull all variables from SESSION
$question = $_SESSION['question'];
$answer0 = $_SESSION['answer0'];
$answer1 = $_SESSION['answer1'];
$answer2 = $_SESSION['answer2'];
$answer3 = $_SESSION['answer3'];
$radio = $_SESSION['radio'];
$answerArray = array($answer0, $answer1, $answer2, $answer3);
shuffle($answerArray);
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Entry Review</title>
<style>
.instructions {
color: #696D6E;
}
</style>
</head>
<body>
<h1 class="instructions">Entry Review</h1>
<p><em>You entered the following question:</em></p>
<p><strong><?php echo $question; ?></strong></p><br>
<p><em>These are the answers you provided:</em>
<p>
<strong>
<?php
if(isset($_SESSION['radio'])) {
$radio = $_SESSION['radio'];
foreach ($answerArray as $value) {
echo $value . "<br />";
}
} else {
echo "Please click the back button in your browser and select a correct answer";
}
?>
</strong>
</p>
</body>
</html>
"Ursprünglich dieses Problem gelöst wurde, aber nachdem ich geändert ..." - Nun, was der ursprüngliche Inhalt war, und die wesentliche Code haben Sie entfernen ? –
Warum sollten Sie eine Schleife erstellen, wenn Sie nur kopieren/einfügen können? – CarlosCarucce
Das war meine ursprüngliche Frage: http://stackoverflow.com/questions/36826587/how-to-display-correct-answer-in-different-color-than-the-rest – slickset