Webbserverprogrammering 1

Show sourcecode

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

webbserverprogrammering/exercises/

dbtestforfattare.php
exercise_include/
form1_2.php
form3_4.php
form3_4_resultat.php
form_welcome.php
forms_quiz.php
funktioner.php
grunder.php
string4.php
textstrings.php
textstrings_result.php

forms_quiz.php

322 lines UTF-8 Windows (CRLF)
<!DOCTYPE html>
<html lang="sv">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Quiz</title>
</head>
<body>
<?php
$del 
= $_POST["del"] ?? 0;
$password_sent = $_POST["password_sent"] ?? 0;
$result = False;
$question_array = [
    [
        
"id" => 1,
        
"name" => "q1",
        
"question_title" => "Vad heter Sveriges huvudstad?",
        
"questions" => [
            
"Göteborg",
            
"Stockholm",
            
"Helsinki",
            
"Malmö",
        ],
        
"question_correct" => "Stockholm",
    ],
    [
        
"id" => 2,
        
"name" => "q2",
        
"question_title" => "Vad är ljusets hastighet?",
        
"questions" => [
            
"299 942 458 meter per sekund",
            
"299 792 458 meter per sekund",
            
"299 367 458 meter per sekund",
            
"299 298 458 meter per sekund",
        ],
        
"question_correct" => "299 792 458 meter per sekund",
    ],
    [
        
"id" => 3,
        
"name" => "q3",
        
"question_title" => "Gissa talet",
        
"questions" => [
            
"23423432",
            
"854",
            
"34324",
            
"3240341",
        ],
        
"question_correct" => "854",
    ],
    [
        
"id" => 4,
        
"name" => "q4",
        
"question_title" => "Vem är grundare och ägare av matkortsappen?",
        
"questions" => [
            
"Gustav",
            
"Simon",
            
"Erik",
            
"Tegin",
        ],
        
"question_correct" => "Tegin",
    ],
    [
        
"id" => 5,
        
"name" => "q5",
        
"question_title" => "Hur långt är avståndet mellan jorden och Mars klockan 14.07, 7 december 2027?",
        
"questions" => [
            
"402 289 150 000 meter",
            
"384 400 620 000 meter",
            
"267 407 380 000 meter",
            
"337 407 810 000 meter",
        ],
        
"question_correct" => "337 407 810 000 meter",
    ],
];
function 
savePreviousAnswers($id){
    for (
$i = 1; $i < $id; $i++){
        
$name = "q$i";
        
$answer = $_POST[$name] ?? "";

        echo 
'<input type="hidden" name="' . $name .'" value="' . htmlspecialchars($answer, ENT_QUOTES, 'UTF-8') . '">';
    }
}
function 
createQuestion($question){
    global 
$result;
    
$name = $question['name'];
    
$question_title = $question['question_title'];
    
$questions = $question['questions'];
    
$correct_question = $question['question_correct'];
    
$id = (int) $question['id'];    
    
$id_next = $id + 1;

    
//Skriver ut på sidan
    
echo "<h1>Fråga $id</h1>";
    echo 
'<form method="post" action="">';
    echo 
"<h2>$question_title</h2>";
    
savePreviousAnswers($id);
   
    
    if(
$result == False){
        foreach (
$questions as $option){
            echo 
'<input type="radio" name="' . $name .'" value="' . $option . '">';
            echo 
"<label>$option</label><br>";
            }
        echo 
"<br>";
        echo 
'<button type="submit" name="del" value="' . $id_next . '">Nästa fråga</button>';
        
    }
    elseif(
$result == True){
        foreach (
$questions as $option){
            if(
$option == $correct_question){
                    echo 
'<input type="radio" name="' . $name .'" value="' . $option . '">';
                    echo 
'<label style="color:green;">'. $option . '</label><br>';
                    continue;
                }
            if(
$_POST['q' . $question['id']] == $option){
                if(
$option != $correct_question){
                    echo 
'<input type="radio" name="' . $name .'" value="' . $option . '">';
                    echo 
'<label style="color:red;">'. $option . '</label><br>';
                }
                continue;
            }
            echo 
'<input type="radio" name="' . $name .'" value="' . $option . '">';
            echo 
"<label>$option</label><br>";
            }
        echo 
"<br>";
    }
    echo 
"</form>";
    
}

// Lösenordskontroll
if ($password_sent == 1){
    
$password = $_POST['password'];
    if (
$password == "password"){
        
$del = 1;
}
}

// Funktioner

function print_array($array){
    foreach(
$array as $element){
        
print_r($element);
        echo 
"<br>";
    }
}
?>
<?php
if ($del == 0){
?>

<form method="post" action="">
<label for="password">Vad är lösenordet?</label>
<input type="password" name="password">
<button type="submit" name="password_sent" value="1">Skicka</button>
</form>


<?php
}




?>

<?php
if ($del == 1){
$question1 = $question_array[0];
createQuestion($question1);

}
?>


<!-- Fråga 2 -->
<?php 
if($del == 2){
$question2 = $question_array[1];
createQuestion($question2);
}
?>


<!-- Fråga 3 -->
<?php
if ($del == 3){
$question3 = $question_array[2];
createQuestion($question3);
}
?>


<!-- Fråga 4 -->
<?php
if ($del == 4){
$question4 = $question_array[3];
createQuestion($question4);
}
?>


<!-- Fråga 5 -->
<?php
if ($del == 5){
$question5 = $question_array[4];
createQuestion($question5);
}
?>



<!-- Resultat -->

<?php

if (($del == 6) or ($del == 7)){
?>
<h1>Resultat</h1>
<form method="post" action="">
<input type="hidden" name="q1" value="<?php echo $_POST['q1']; ?>">
<input type="hidden" name="q2" value="<?php echo $_POST['q2']; ?>">
<input type="hidden" name="q3" value="<?php echo $_POST['q3']; ?>">
<input type="hidden" name="q4" value="<?php echo $_POST['q4']; ?>">
<input type="hidden" name="q5" value="<?php echo $_POST['q5']; ?>">
<button type="submit" name="del" value="0">Gör om</button>
<button type="submit" name="del" value="7">Skicka på mail</button>
</form>
<?php
$amount_questions 
= 5;
$result = True;

foreach(
$question_array as $question){
    
$result = True;
    
createQuestion($question);
    echo 
"<br><hr>";

    }

$message = "Quizresultat:\n\n";
foreach (
$question_array as $question){
    
$id = $question['id'];
    
$name = $question['name'];
    
$answer = $_POST[$name] ?? "";
    
$correct_answer = $question['question_correct'];

    
$message .= "Fråga $id: " . $question['question_title'] . "\n";
    
$message .= "Ditt svar: $answer\n";

    if (
$answer == $correct_answer){
        
$message .= "RÄTT\n\n";
    }
    else{
        
$message .= "FEL\n";
        
$message .= "Rätt svar: $correct_answer\n\n";
    }
}


?>
<input type="hidden" name="message" value="<?php echo htmlspecialchars($message, ENT_QUOTES, 'UTF-8'); ?>">
<?php


}?>



<?php
if ($del == 7){
?>
<h1>Skicka ditt resultat på mail</h1>
<form method="post" action="">
<label for="fnamn">Förnamn:</label>
<input type="text" name="fnamn" ><br><br>
<label for="lnamn">Efternamn:</label>
<input type="text" name="lnamn" ><br><br>
<label for="email">Email:</label>
<input type="email" name="email" require><br><br>
<input type="hidden" name="q1" value="<?php echo $_POST['q1']; ?>">
<input type="hidden" name="q2" value="<?php echo $_POST['q2']; ?>">
<input type="hidden" name="q3" value="<?php echo $_POST['q3']; ?>">
<input type="hidden" name="q4" value="<?php echo $_POST['q4']; ?>">
<input type="hidden" name="q5" value="<?php echo $_POST['q5']; ?>">
<input type="hidden" name="message" value="<?php echo htmlspecialchars($message, ENT_QUOTES, 'UTF-8'); ?>">

<button type="submit" name="del" value="8">Skicka</button>
</form>
<?php
}

?>




<!-- Skicka på mail -->


<?php




if($del == 8){
$mottagare = $_POST["email"];
$rubrik = "Quizresultat";
$meddelande = $_POST["message"];
$mejlhuvud="From: ".$_POST["email"]." \nReply-To: kalle@anka.se";
mail($mottagare, $rubrik, $meddelande, $mejlhuvud);
print_r($mottagare);
print_r($rubrik);
print_r($meddelande);
print_r($mejlhuvud);
}

?>



</body>
</html>