Final Question 17 - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of Final Question 17.php

  1.  
  2. <!DOCTYPE HTML>  
  3. <html>
  4. <head>
  5. <style>
  6. .error {color: #FF0000;}
  7. </style>
  8. </head>
  9. <body>  
  10.  
  11. <?php
  12. // define variables and set to empty values
  13. $fnameErr = $lnameErr = $emailErr = $anameErr = $phoneErr = $ampErr = $ptErr = "";
  14. $fname = $lname = $email = $aname = $comment = $phone = $amp = $pt = "";
  15.  
  16. if ($_SERVER["REQUEST_METHOD"] == "POST") {
  17.   if (empty($_POST["first name"])) {
  18.     $fnameErr = "Name is required";
  19.   } else {
  20.     $fname = test_input($_POST["name"]);
  21.     // check if name only contains letters and whitespace
  22.     if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
  23.       $fnameErr = "Only letters and white space allowed";
  24.     }
  25.   }
  26.   
  27.   if (empty($_POST["name"])) {
  28.     $lnameErr = "Name is required";
  29.   } else {
  30.     $lname = test_input($_POST["name"]);
  31.     // check if name only contains letters and whitespace
  32.     if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
  33.       $lnameErr = "Only letters and white space allowed";
  34.     }
  35.   }
  36.   
  37.   if (empty($_POST["email"])) {
  38.     $emailErr = "Email is required";
  39.   } else {
  40.     $email = test_input($_POST["email"]);
  41.     // check if e-mail address is well-formed
  42.     if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
  43.       $emailErr = "Invalid email format";
  44.     }
  45.   }
  46.     
  47.   if (empty($_POST["phone"])) {
  48.     $phoneErr = "Phone number required";
  49.   } else {
  50.     $phone = test_input($_POST["phone"]);
  51.     
  52.     }
  53.   }
  54.  
  55.   if (empty($_POST["comment"])) {
  56.     $comment = "";
  57.   } else {
  58.     $comment = test_input($_POST["comment"]);
  59.   }
  60.  
  61.   if (empty($_POST["Account name"])) {
  62.     $anameErr = "Account number is required";
  63.   } else {
  64.     $aname = test_input($_POST["Account name"]);
  65.   }
  66.    
  67.     if (empty($_POST["Ammount of Payment"])) {
  68.     $ampErr = "Payment amount is required";
  69.   } else {
  70.     $amp = test_input($_POST["Ammount of Payment"]);
  71.   }
  72.       if (empty($_POST["Payment type"])) {
  73.     $ptErr = "Payment type is required";
  74.   } else {
  75.     $pt = test_input($_POST["Payment type"]);
  76.   }
  77.  
  78.  
  79. function test_input($data) {
  80.   $data = trim($data);
  81.   $data = stripslashes($data);
  82.   $data = htmlspecialchars($data);
  83.   return $data;
  84. }
  85. ?>
  86.  
  87. <h2>PHP Form Validation Example</h2>
  88. <p><span class="error">* required field</span></p>
  89. <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">  
  90.   First Name: <input type="text" name="fname" value="<?php echo $fname;?>">
  91.   <span class="error">* <?php echo $fnameErr;?></span>
  92.   <br><br>
  93.     Last Name: <input type="text" name="lname" value="<?php echo $lname;?>">
  94.   <span class="error"><?php echo $lnameErr;?></span>
  95.   <br><br>
  96.   E-mail: <input type="text" name="email" value="<?php echo $email;?>">
  97.   <span class="error">* <?php echo $emailErr;?></span>
  98.   <br><br>
  99.   Phone: <input type="text" name="Phone" value="<?php echo $phone;?>">
  100.   <span class="error"><?php echo $phoneErr;?></span>
  101.   <br><br>
  102.   Account Number: <input type="text" name="aname" value="<?php echo $aname;?>">
  103.   <span class="error"><?php echo $anameErr;?></span>
  104.   <br><br>
  105. Amount of Payment: <input type="text" name="amp" value="<?php echo $amp;?>">
  106.   <span class="error"><?php echo $ampErr;?></span>
  107.   <br><br>
  108.   Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
  109.   <br><br>
  110.   Payment type:
  111.   <input type="radio" name="pt" <?php if (isset($pt) && $pt=="Visa") echo "checked";?> value="Visa">Visa
  112.   <input type="radio" name="pt" <?php if (isset($pt) && $pt=="Mastercard") echo "checked";?> value="Mastercard">Mastercard
  113.   <input type="radio" name="pt" <?php if (isset($pt) && $pt=="Cash") echo "checked";?> value="Cash">Cash 
  114.   <span class="error">* <?php echo $ptErr;?></span>
  115.   <br><br>
  116.   <input type="submit" name="submit" value="Submit">  
  117. </form>
  118.  
  119. <?php
  120. echo "<h2>Your Input:</h2>";
  121. echo $fname;
  122. echo "<br>";
  123. echo $lname;
  124. echo "<br>";
  125. echo $email;
  126. echo "<br>";
  127. echo $phone;
  128. echo "<br>";
  129. echo $aname;
  130. echo "<br>";
  131. echo $pt;
  132. echo "<br>";
  133. echo $amp;
  134. echo "<br>";
  135. echo $comment;
  136. ?>
  137.  
  138. </body>
  139. </html>
File Description
  • Final Question 17
  • PHP Code
  • 07 Apr-2021
  • 3.96 Kb
You can Share it: