PHP Snippet 1:
<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
<title>PHP exercise 1</title>
<link rel="stylesheet" type="text/css" href="./styling1.css" />
</head>
<body>
<!--E X C E R S I S E 3 !-->
<section id="allContent">
<section>
<button class="hide" id="clic">
<h4> E X E R C I C E 1 </h4>
</button>
</section>
<div id="container" class="hidden">
<div id="Barbox">
<p class="pClass"> Create an application that ask the user for number (1-7) and return the day of the week associate with the number entered. You must validate the number</p>
</div>
<div id="background"></div>
<div class="ball"></div>
<div id="ball2"></div>
<div id="titleBox">
<p id="titlePRG">Enter a number!</p>
</div>
<form method="get" action="<?php $_SERVER['PHP_SELF']; ?>">
<input type="text" value="" id="textField" name="number">
<input type="submit" value="Enter" class="button" name="submit">
</form>
<?php
if (isset($_GET['submit']))
$number = isset($_GET['number']) ? $_GET['number'] : '';
if ($number == 1) {
$returntext = "The day of the week is Monday";
} else if ($number == 2) {
$returntext = "The day of the week is Tuesday";
} else if ($number == 3) {
$returntext = "The day of the week is Wednesday";
} else if ($number == 4) {
$returntext = "The day of the week is Thursday";
} else if ($number == 5) {
$returntext = "The day of the week is Friday";
} else if ($number == 6) {
$returntext = "The day of the week is Saturday";
} else if ($number == 7) {
$returntext = "The day of the week is Sunday";
} else {
$returntext = ("Wrong input, try again!");
}
?>
<div id="valueReturned">
<p id="returnText"><?php echo $returntext; ?><p>
</div>
<a href="index4.html" id="jButton">
<h3> Next Exercise </h3>
</a>
</div></section>
PHP Snippet 2:
<?php
$response = "";
if(isset($_POST) && isset($_POST["mytext"])): /// check if user submitted form
$num = $_POST["mytext"]; //put form mytext variable.. retrieved from html <input name="mytext"....
if(is_numeric($num)):
switch($num):
case "1":
$response = "Monday";
break;
case "2":
$response = "Tuesday";
break;
case "3":
$response = "Wednesday";
break;
case "4":
$response = "Thursday";
break;
case "5":
$response = "Friday";
break;
case "6":
$response = "Saturday";
break;
case "7":
$response = "Sunday";
break;
default:
$response = "Invalid number";
endswitch;
else:
$response = "Invalid number";
endif;
endif;
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PhpFiddle Initial Code</title>
<!--
http://phpfiddle.org
-->
<script type="text/javascript">
</script>
<style type="text/css">
</style>
</head>
<body>
<div style="margin: 30px 10%;">
<h3>My form</h3>
<form id="myform" name="myform" action="" method="post">
<label>Text</label> <input type="text" value="" size="30" maxlength="100" name="mytext" id="" /><br /><br />
<br />
<button id="mysubmit" type="submit">Submit</button><br /><br />
<?php if (isset($_POST)): ?>
<div style="background-color:red;color:white;"><?php echo $response; ?></div>
<?php endif; ?>
</div>
</form>
</div>
<?php
?>
</body>
</html>