Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/exercises/repetition/
hemligheter.php
repetition.php
repetition1.php
repetition2.php
repetition3.php
repetition3_medel.php
repetition4.php
repetition5.php
repetition6.php
repetition7.php
repetition8.php
repetition8.php
71 lines UTF-8 Windows (CRLF)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Repetition</title>
</head>
<body>
<?php
$attempts = 0;
$nameSet = false;
if (isset($_GET["namn"])) {
if ($_GET["namn"] != "") {
if (isset($_GET["attempts"])) {
$attempts = $_GET["attempts"] + 1;
}
$nameSet = true;
echo "<p>Hej ".$_GET["namn"]."!</p>";
}
}
$numIsRight = false;
if (isset($_GET["number"])) {
if ($_GET["number"] == rand(1,10)) {
$numIsRight = true;
}
}
if ($nameSet) {
if ($numIsRight) {
echo "Det tog $attempts försök.";
} else {
echo "Fel, du har nu gissat $attempts gånger";
}
}
?>
<form method="get" action="">
<?php
if (!$numIsRight) {
echo "<input type='hidden' name='attempts' value='$attempts'>";
if ($nameSet) {
echo "<input type='hidden' name='namn' value='".$_GET["namn"]."'>";
} else {
echo "<input type='text' name='namn' placeholder='Namn'>";
}
}
?>
<br>
<?php
if (!$numIsRight) {
echo '<input type="number" name="number" placeholder="Nummer">
<br>
<input type="submit" value="Skicka">';
} else {
echo '<input type="submit" value="Kör igen">';
}
?>
</form>
</body>
</html>