Hi there, Im trying to achieve a form that requires the user to input their first and last name, loan amount information and their email address. I have put together the following code which currently validates required fields and then sends to my email address if there is information in the text area.
However what I really want to do is add the required fields to the first name, last name and loan amount and then just have the email address validated for valid or invalid address…
I have tried using the following but with no success. Is anybody able to take a look and point out where I am going wrong? At the moment the form will still send if information is entered for the first 3 titles so it just kinda ignores any validation on the last one(email address)
[php]
.error {color: #FF0000;} <?php // Initialize variables and set to empty strings $firstName=$lastName=$Loanamount=$Email=""; $firstNameErr=$lastNameErr=$LoanamountErr=$EmailErr=""; // Validate input and sanitize if ($_SERVER['REQUEST_METHOD']== "POST") { $valid = true; //Your indicator for your condition, actually it depends on what you need. I am just used to this method. if (empty($_POST["firstName"])) { $firstNameErr = "*"; $valid = false; //false } else { $firstName = test_input($_POST["firstName"]); } if (empty($_POST["lastName"])) { $lastNameErr = "*"; $valid = false; } else { $lastName = test_input($_POST["lastName"]); } if (empty($_POST["Loanamount"])) { $LoanamountErr = "*"; $valid = false; } else { $Loanamount = test_input($_POST["Loanamount"]); } if (empty($_POST["email"])) {$emailErr = "Email is required";} else { $email = test_input($_POST["email"]); // check if e-mail address syntax is valid if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $emailErr = "Invalid email format"; } } //if valid then redirect if($valid){ header('Location: http://coffeemachines4u.co.uk/FinanceForms/FinanceBooster/testvalidation2.php'); exit(); } } // Sanitize data function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?>First Name: * <?php echo $firstNameErr; ?>
Last Name: * <?php echo $lastNameErr; ?>
Loan Amount: * <?php echo $LoanamountErr; ?>
E-mail:
* <?php echo $emailErr;?>