Advertisement
Guest User

Full PHP Coding

a guest
Nov 14th, 2015
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. <!DOCTYPE HTML>
  2. <html>
  3. <head>Validation example
  4. </head>
  5. <body>
  6. <?php
  7. //?
  8. $nameErr = $CustomeremailErr = $MonitorqtyErr = $PCqtyErr = $MiceqtyErr = $KeyboardqtyErr = $SpeakerqtyErr = "";
  9. $PCqty = $Miceqty = $Keyboardqty = $Monitorqty = $Speakerqty = 0;
  10. $name = $Customeremail = "";
  11. DEFINE ("PCPRICE" , 300.01);
  12. DEFINE ("MONITORPRICE" , 100);
  13. DEFINE ("KEYBOARDPRICE" , 5);
  14. DEFINE ("MICEPRICE" , 5);
  15. DEFINE ("SPEAKERPRICE" , 20);
  16. DEFINE ("DODGYDELIVERY", 35.05);
  17.  
  18.  
  19. //?
  20. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  21.  
  22. if (empty($_POST["name"])) {
  23. $nameErr = "Name is required";
  24. } else {
  25. $name = clean_input($_POST["name"]);
  26. if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
  27. $nameErr = "Only letters and white space allowed";
  28. }
  29. }
  30. if (empty($_POST["customeremail"])) {
  31. $customeremailErr = "Email is required";
  32. } else {
  33. $Customeremail = clean_input($_POST["customeremail"]);
  34. //check if email is well-formed
  35. if (!filter_var($Customeremail, FILTER_VALIDATE_EMAIL)) {
  36. $CustomeremailErr = "Invalid email format";
  37. }
  38. }
  39. if (empty($_POST["PCqty"])) {
  40. $PCqty = clean_input($_POST["PCqty"]);
  41. } else if (preg_match("/^[a-zA-Z ]*$/",$PCqty)) {
  42. $PCqtyErr = "Only Numbers Allowed";
  43. }
  44. if (empty($_POST["Monitorqty"])) {
  45. $Monitorqty = clean_input($_POST["Monitorqty"]);
  46. } else if (preg_match("/^[a-zA-Z ]*$/",$Monitorqty)) {
  47. $MonitorqtyErr = "Only Numbers Allowed";
  48. }
  49.  
  50. if (empty($_POST["Keyboardqty"])) {
  51. $Keyboardqty = clean_input($_POST["Keyboardqty"]);
  52. } else if (preg_match("/^[a-zA-Z ]*$/",$Keyboardqty)) {
  53. $KeyboardqtyErr = "Only Numbers Allowed";
  54. }
  55. if (empty($_POST["Miceqty"])) {
  56. $Miceqty = clean_input($_POST["Miceqty"]);
  57. } else if (preg_match("/^[a-zA-Z ]*$/",$Miceqty)) {
  58. $MiceqtyErr = "Only Numbers Allowed";
  59. }
  60. //?Fix change to speaker
  61. if (empty($_POST["Speakerqty"])) {
  62. $Speakerqty = clean_input($_POST["Speakerqty"]);
  63. } else if (preg_match("/^[a-zA-Z ]*$/",$Speakerqty)) {
  64. $SpeakerqtyErr = "Only Numbers Allowed";
  65. }
  66. }
  67.  
  68. // function test_input cleans up data by trimming off blank space, removing slashes and
  69. //coding characters like < to &lt to prevent hacking
  70. function clean_input ($data) {
  71. $data = trim($data);
  72. $data = stripslashes($data);
  73. $data = htmlspecialchars($data);
  74. return $data;
  75. }
  76. ?>
  77.  
  78. <h2>PHP Form Validation Example</h2>
  79. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
  80.  
  81.  
  82. Name: <input type="text" name="name" value="<?php echo $name;?>">
  83. <span class="error">*<?php echo $nameErr;?></span>
  84. <br><br>
  85. email: <input type="text" name = "customeremail" value="<?php echo $Customeremail;?>">
  86. <span class="error">*<?php echo $CustomeremailErr;?></span>
  87. <br><br>
  88. PC quantity: <input type="text" name="PCqty" value="<?php echo $PCqty;?>">
  89. <span class="error">*<?php echo $PCqtyErr;?></span>
  90. <br><br>
  91. Keyboard quantity: <input type="text" name="Keyboardqty" value="<?php echo $Keyboardqty;?>">
  92. <span class="error">*<?php echo $KeyboardqtyErr;?></span>
  93. <br><br>
  94. Mice Quantity: <input type="text" name="Miceqty" value="<?php echo $Miceqty;?>">
  95. <span class="error">*<?php echo $MiceqtyErr;?></span>
  96. <br><br>
  97. Speaker Quantity: <input type="text" name="Speakerqty" value="<?php echo $Speakerqty;?>">
  98. <span class="error">*<?php echo $SpeakerqtyErr;?></span>
  99. <br><br>
  100. Monitor Quantity: <input type="text" name="Monitorqty" value="<?php echo $Monitorqty;?>">
  101. <span class="error">*<?php echo $MonitorqtyErr;?></span>
  102. <br><br>
  103.  
  104. <input type="submit" name="submit" value="Submit">
  105. </form>
  106. <?php
  107.  
  108. $PcTotalCost = $SpeakerTotalCost = $MonitorTotalCost = $MiceTotalCost = $KeyboardTotalCost = 0 ;
  109. // Has this form been submitted and was data entered then do the calculations
  110. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  111.  
  112. // check there are no errors in the entries
  113.  
  114. if (($nameErr == "") && ($CustomeremailErr == "") && ($PCqtyErr == "") && ($MonitorqtyErr == "") && ($MiceqtyErr == "") && ($KeyboardqtyErr == "") && ($SpeakerqtyErr == "") )
  115. {
  116.  
  117. echo "<h2>Quote</h2>";
  118. // Do required calculation
  119. $PcTotalCost = $PCqty * PCPRICE;
  120. $KeyboardTotalCost = $Keyboardqty * KEYBOARDPRICE;
  121. $MonitorTotalCost = $Monitorqty * MONITORPRICE;
  122. $SpeakerTotalCost = $Speakerqty * SPEAKERPRICE;
  123. $MiceTotalCost = $Miceqty * MICEPRICE;
  124. $GoodsTotalCost = $MiceTotalCost + $SpeakerTotalCost + $MonitorTotalCost + $KeyboardTotalCost + $PcTotalCost;
  125. $VatRate = $GoodsTotalCost / 100;
  126. $GrossCost = $GoodsTotalCost + $VatRate + DODGYDELIVERY;
  127. // Displaying the result
  128. echo 'Dodgy Dan’s quote for ' . $name . ' on ' . date(1) . "<br/>" ;
  129. echo $PCqty . " PC's at a cost of £ ". number_format(PCPRICE, 2) . " each would cost £ " . number_format($PcTotalCost, 2) . "<br/>";
  130. echo $Keyboardqty . " Keyboards at a cost of £ ". number_format(KEYBOARDPRICE, 2) . " each would cost £ " . number_format($KeyboardTotalCost, 2) . "<br/>";
  131. echo $Miceqty . " Mice at a cost of £ ". number_format(MICEPRICE, 2) . " each would cost £ " . number_format($MiceTotalCost, 2) . "<br/>";
  132. echo $Speakerqty . " Speakers at a cost of £ ". number_format(SPEAKERPRICE, 2) . " each would cost £ " . number_format($SpeakerTotalCost, 2) . "<br/>";
  133. echo $Monitorqty . " Monitors at a cost of £ ". number_format(MONITORPRICE, 2) . " each would cost £ " . number_format($MonitorTotalCost, 2) . "<br/>";
  134. echo " The Delivery charge would be £" . number_format (DODGYDELIVERY, 2) . "<br/>" ;
  135. echo ' The total cost of goods is £' . $GoodsTotalCost . "<br/>" ;
  136. echo " The VAT(10%)rate for this purchase is £" . $VatRate . "<br/>" ;
  137. echo ' The total gross cost after VAT for this order is £' . $GrossCost . "<br/>" ;
  138.  
  139.  
  140. }
  141. }
  142. ?>
  143. </body>
  144. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement