Webbserverprogrammering 1

Show sourcecode

The following files exists in this folder. Click to view.

webbsrvprg/exercises/inlamning_1/

index.php
uppgifter1-3.php

uppgifter1-3.php

68 lines UTF-8 Windows (CRLF)
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Frågor</title>
</head>
<body>
        <?php
        $q1 
= [
            
"question" => "Vad heter Harry?",
            
"alts" => ["Harry""Något annat""Göran""Hemligt"],
            
"correct" => 0,
        ];
        
$q2 = [
            
"question" => "1 + 2?",
            
"alts" => ["1""2""3""4"],
            
"correct" => 2,
        ];
        
$q3 = [
            
"question" => "Korvstroganof?",
            
"alts" => ["Kanske det""Absolut inte""Kanske inte det""Absolut"],
            
"correct" => 3,
        ];
        
$q4 = [
            
"question" => "Vilke är den tionde bokstaven i alfabetet?",
            
"alts" => ["H""I""J""Ö"],
            
"correct" => 2,
        ];

        
$questions = [$q1$q2$q3$q4];
        
$currentQuestion 0;
        
$prev_answers "";
        if (isset(
$_GET["current"])) {
            
$currentQuestion intval($_GET["current"]);
            
$newAns htmlspecialchars(str_replace(' ''_'$_GET["ans"])); // Byter ut alla mellanslag mot _ så att man kan använda mellanslag för att skilja på olika svar
            
$prev_answers $_GET["prevAnswers"] . " " $newAns// Lägger på alla tidigare insamlade svar med den nya
        
}

        echo(
'<form style="margin: 100px 35vw;" method="get">');
        if (
$currentQuestion == 0){
            echo(
'<input type="text" name="ans" id="namn" placeholder="Harry" required><label for="namn">Skriv ditt namn</label><br><br>');    
        } else if (
$currentQuestion <= count($questions)){
            
// Lägger ut den frågan som det är dags för
            
$qDict $questions[$currentQuestion 1];
            echo(
'<fieldset><legend>' $qDict["question"] . '</legend>');
            for (
$i 0$i count($qDict["alts"]); $i += 1){
                
$alt $qDict["alts"][$i];
                echo(
'<input type="radio" name="ans" value="' $i '" id="q' $currentQuestion '_' $i '" required><label for="q' $currentQuestion '_' $i '">' $alt '</label><br><br>');
            }
            echo(
'</fieldset>');
        }

        
// Lägger till gömda inputfälten
        
echo('<input type="hidden" name="prevAnswers" value ="' $prev_answers '">');
        
$currentQuestion += 1;
        echo(
'<input type="hidden" name="current" value ="' $currentQuestion '">');

        
// Lägger ut submiten
        
if ($currentQuestion count($questions)){
            echo(
'<button type="submit">Skicka in</button>');
        } else{
            echo(
'<button type="submit">Nästa</button>');
        }
        
?>
    </form>
</body>
</html>