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.
Result of php executing
Full code of Final Question 17.php
- <!DOCTYPE HTML>
- <html>
- <head>
- <style>
- .error {color: #FF0000;}
- </style>
- </head>
- <body>
- <?php
- // define variables and set to empty values
- $fnameErr = $lnameErr = $emailErr = $anameErr = $phoneErr = $ampErr = $ptErr = "";
- $fname = $lname = $email = $aname = $comment = $phone = $amp = $pt = "";
- if ($_SERVER["REQUEST_METHOD"] == "POST") {
- if (empty($_POST["first name"])) {
- $fnameErr = "Name is required";
- } else {
- $fname = test_input($_POST["name"]);
- // check if name only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
- $fnameErr = "Only letters and white space allowed";
- }
- }
- if (empty($_POST["name"])) {
- $lnameErr = "Name is required";
- } else {
- $lname = test_input($_POST["name"]);
- // check if name only contains letters and whitespace
- if (!preg_match("/^[a-zA-Z-' ]*$/",$name)) {
- $lnameErr = "Only letters and white space allowed";
- }
- }
- if (empty($_POST["email"])) {
- $emailErr = "Email is required";
- } else {
- $email = test_input($_POST["email"]);
- // check if e-mail address is well-formed
- if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
- $emailErr = "Invalid email format";
- }
- }
- if (empty($_POST["phone"])) {
- $phoneErr = "Phone number required";
- } else {
- $phone = test_input($_POST["phone"]);
- }
- }
- if (empty($_POST["comment"])) {
- $comment = "";
- } else {
- $comment = test_input($_POST["comment"]);
- }
- if (empty($_POST["Account name"])) {
- $anameErr = "Account number is required";
- } else {
- $aname = test_input($_POST["Account name"]);
- }
- if (empty($_POST["Ammount of Payment"])) {
- $ampErr = "Payment amount is required";
- } else {
- $amp = test_input($_POST["Ammount of Payment"]);
- }
- if (empty($_POST["Payment type"])) {
- $ptErr = "Payment type is required";
- } else {
- $pt = test_input($_POST["Payment type"]);
- }
- function test_input($data) {
- $data = trim($data);
- $data = stripslashes($data);
- $data = htmlspecialchars($data);
- return $data;
- }
- ?>
- <h2>PHP Form Validation Example</h2>
- <p><span class="error">* required field</span></p>
- <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
- First Name: <input type="text" name="fname" value="<?php echo $fname;?>">
- <span class="error">* <?php echo $fnameErr;?></span>
- <br><br>
- Last Name: <input type="text" name="lname" value="<?php echo $lname;?>">
- <span class="error"><?php echo $lnameErr;?></span>
- <br><br>
- E-mail: <input type="text" name="email" value="<?php echo $email;?>">
- <span class="error">* <?php echo $emailErr;?></span>
- <br><br>
- Phone: <input type="text" name="Phone" value="<?php echo $phone;?>">
- <span class="error"><?php echo $phoneErr;?></span>
- <br><br>
- Account Number: <input type="text" name="aname" value="<?php echo $aname;?>">
- <span class="error"><?php echo $anameErr;?></span>
- <br><br>
- Amount of Payment: <input type="text" name="amp" value="<?php echo $amp;?>">
- <span class="error"><?php echo $ampErr;?></span>
- <br><br>
- Comment: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea>
- <br><br>
- Payment type:
- <input type="radio" name="pt" <?php if (isset($pt) && $pt=="Visa") echo "checked";?> value="Visa">Visa
- <input type="radio" name="pt" <?php if (isset($pt) && $pt=="Mastercard") echo "checked";?> value="Mastercard">Mastercard
- <input type="radio" name="pt" <?php if (isset($pt) && $pt=="Cash") echo "checked";?> value="Cash">Cash
- <span class="error">* <?php echo $ptErr;?></span>
- <br><br>
- <input type="submit" name="submit" value="Submit">
- </form>
- <?php
- echo "<h2>Your Input:</h2>";
- echo $fname;
- echo "<br>";
- echo $lname;
- echo "<br>";
- echo $email;
- echo "<br>";
- echo $phone;
- echo "<br>";
- echo $aname;
- echo "<br>";
- echo $pt;
- echo "<br>";
- echo $amp;
- echo "<br>";
- echo $comment;
- ?>
- </body>
- </html>