Created
March 3, 2014 11:58
-
-
Save mogetutu/9323564 to your computer and use it in GitHub Desktop.
POST and GET examples
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
<!-- GET --> | |
<?php | |
$firstname = $_GET['firstname']; | |
$lastname = $_GET['lastname']; | |
$subjectone = $_GET['subjectone']; | |
$subjecttwo = $_GET['subjecttwo']; | |
$subjectthree = $_GET['subjectthree']; | |
$subjectfour = $_GET['subjectfour']; | |
$subjectfive = $_GET['subjectfive']; | |
$totalmarks = $subjectone + $subjecttwo + $subjectthree + $subjectfour + $subjectfive; | |
$totalmarkspercentage = $totalmarks * 100 / 500; | |
?> | |
<html> | |
<head> | |
<title>GET</title> | |
</head> | |
<body> | |
<form action="" method="GET"> | |
<input type="text" name="firstname" placeholder="First Name"> | |
<input type="text" name="lastname" placeholder="Last Name"> | |
<p><input type="text" name="subjectone" placeholder="Subject One"></p> | |
<p><input type="text" name="subjecttwo" placeholder="Subject Two"></p> | |
<p><input type="text" name="subjectthree" placeholder="Subject Three"></p> | |
<p><input type="text" name="subjectfour" placeholder="Subject Four"></p> | |
<p><input type="text" name="subjectfive" placeholder="Subject Five"></p> | |
<p> | |
Total Marks: <?php if(isset($totalmarks)) echo $totalmarks; ?> | |
</p> | |
<p> | |
Total Marks(%): <?php if(isset($totalmarkspercentage)) echo $totalmarkspercentage . "%"; ?> | |
</p> | |
<input type="submit"> | |
</form> | |
</body> | |
</html> |
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
<!-- POST --> | |
<?php | |
// shorthand if else | |
// $a = (isset($a)) ? $a : ''; | |
// if(isset($a)) | |
// { | |
// $a = $a; | |
// } | |
// else | |
// { | |
// $a = ''; | |
// } | |
$firstname = (isset($_POST['firstname'])) ? $_POST['firstname'] : ''; | |
$lastname = (isset($_POST['lastname'])) ? $_POST['lastname'] : '' ; | |
$subjectone = (isset($_POST['subjectone'])) ? $_POST['subjectone'] : '' ; | |
$subjecttwo = (isset($_POST['subjecttwo'])) ? $_POST['subjecttwo'] : '' ; | |
$subjectthree = (isset($_POST['subjectthree'])) ? $_POST['subjectthree'] : '' ; | |
$subjectfour = (isset($_POST['subjectfour'])) ? $_POST['subjectfour']: '' ; | |
$subjectfive = (isset($_POST['subjectfive'])) ? $_POST['subjectfive'] : ''; | |
// Array of users marks | |
$marks = array($subjectone, $subjecttwo, $subjectthree, $subjectfour, $subjectfive); | |
// Calculate total marks | |
// how to get total value of items in an array | |
$totalmarks = array_sum(check_marks($marks)); | |
// Get total number of items in an array | |
$totalitems = count($marks); | |
$totalmarkspercentage = percentage($totalmarks, $totalitems); | |
// percentage function | |
function percentage($totalmarks, $totalitems) | |
{ | |
return ($totalmarks * 100) / (100* $totalitems); | |
} | |
// Function that check if the marks are more than 100 set marks to 100 | |
function check_marks($marks) | |
{ | |
foreach ($marks as $mark) | |
{ | |
if($mark > 100) | |
{ | |
$mark = 100; | |
} | |
$newmarks[] = $mark; | |
} | |
return $newmarks; | |
} | |
?> | |
<html> | |
<head> | |
<title>POST</title> | |
</head> | |
<body> | |
<form action="" method="POST"> | |
<input type="text" name="firstname" value="<?php echo $firstname; ?>" placeholder="First Name"> | |
<input type="text" name="lastname" value="<?php echo $lastname; ?>" placeholder="Last Name"> | |
<p><input type="text" name="subjectone" value="<?php echo $subjectone; ?>" placeholder="Subject One"></p> | |
<p><input type="text" name="subjecttwo" value="<?php echo $subjecttwo; ?>" placeholder="Subject Two"></p> | |
<p><input type="text" name="subjectthree" value="<?php echo $subjectthree; ?>" placeholder="Subject Three"></p> | |
<p><input type="text" name="subjectfour" value="<?php echo $subjectfour; ?>" placeholder="Subject Four"></p> | |
<p><input type="text" name="subjectfive" value="<?php echo $subjectfive; ?>" placeholder="Subject Five"></p> | |
<p> | |
Total Marks: <?php if(isset($totalmarks)) echo $totalmarks; ?> | |
</p> | |
<p> | |
Total Marks(%): <?php if(isset($totalmarkspercentage)) echo $totalmarkspercentage . "%"; ?> | |
</p> | |
<input type="submit"> | |
</form> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment