Created
May 7, 2017 01:20
-
-
Save phpfiddle/0f52733d1c3837438e8fa5e3403a3c85 to your computer and use it in GitHub Desktop.
[ Posted by Johnny Cheng ] this is php file
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Start the session | |
session_start(); | |
?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<title></title> | |
<link rel="stylesheet" type="text/css" href="lab7.css"> | |
<link rel="stylesheet" type="text/css" href="https://fonts.googleapis.com/css?family=Inconsolata"> | |
</head> | |
<body> | |
<form id="theForm" method="post" action=""> | |
<fieldset> | |
<legend>Add an Employee</legend> | |
<label>First Name</label><br> | |
<input type="text" name="firstName" required></inpt><br><br> | |
<label>Last Name</label><br> | |
<input type="text" name="lastName" required></inpt><br><br> | |
<label for="result">Result</label><br> | |
<input list="deparments" name="dept" required><br><br> | |
<datalist id="deparments"> | |
<option value="Engineering"/> | |
<option value="HR"/> | |
<option value="Management"/> | |
</datalist> | |
<input id="submit" type="submit" value="Submit"> | |
</fieldset> | |
</form> | |
<script src="lab7.js"></script> | |
<?php | |
if ($_SERVER["REQUEST_METHOD"]=="POST"){ | |
$employee = new \stdClass(); | |
$employee->firstName = $_POST["firstName"]; | |
$employee->lastName = $_POST["lastName"]; | |
$employee->deptarment = $_POST["dept"]; | |
$date = getdate(); | |
$employee->date = $date["weekday"]." ".$date["month"]." ".$date["mday"]." ".$date["year"] ; | |
$id = rand(0,99999999); | |
if(isset($_SESSION["employeeArr"])){ | |
while (in_array($id,$_SESSION["id"])){ | |
$id = rand(0,99999999); | |
} | |
array_push($_SESSION["id"],$id); | |
$employee->id = empId($id); | |
$myJSON = json_encode($employee); | |
array_push($_SESSION["employeeArr"],$myJSON); | |
} | |
else{ | |
//array of employee id stored internally for redundancy check | |
$_SESSION["id"]=array($id); | |
$employee->id = empId($id); | |
$myJSON = json_encode($employee); | |
$_SESSION["employeeArr"]=array($myJSON); | |
} | |
/* | |
foreach ($_SESSION["employeeArr"] as $a){ | |
echo $a; | |
} | |
*/ | |
echo "<h1>Employee Added</h1>"; | |
echo "Name: ".$_POST["firstName"],", ".$_POST["lastName"]."<br>"; | |
echo "Department: ".$_POST["dept"]."<br>"; | |
echo "Employee ID: ".$employee->id."<br>"; | |
echo "Hire Date: ".$employee->date."<br>"; | |
echo "Total Employee: ".count($_SESSION["employeeArr"]); | |
} | |
function empId($id){ | |
$idString = (string)$id; | |
$strOf0=""; | |
for ($i=0 ; $i<8-strlen($idString); $i++){ | |
$strOf0 = "0".$strOf0; | |
} | |
return $strOf0.$idString; | |
} | |
?> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment