podja Posted December 22, 2006 Share Posted December 22, 2006 Hi, I am currently creating an email form that needs some validation. I have used very simple validation before and want to try something better.I have 3 pages for the form.1 page for all the HTML1 page for the PHP, called by the HTML form1 page for the successI want the PHP to check for the errors, and put them all in an array, if there are some errors it should redirect back to the html form and display the errors, if there are no errors it should link to the success page.How would I go about doing this?CheersP.S Please tell me if there is an easier way of doing this!Reply With Quote Quote Link to comment https://forums.phpfreaks.com/topic/31581-solved-form-validation/ Share on other sites More sharing options...
obsidian Posted December 22, 2006 Share Posted December 22, 2006 First question: do you have your server set up to parse HTML files as PHP? If not, you cannot redirect back to your form and display [b]anything[/b] with PHP. IMHO, your best bet with most email forms is to simply handle all the validation and processing on the same page as the form itself. That way, you can show any messages or errors with full control. Another benefit is that, with the aid of a simple if statement, you can even show a confirmation in lieu of the form once it is successfully processed. Take this idea into account:[code]<?php// check to see if the form is submittedif (isset($_POST['submit'])) { // run all your validation and set an array of errors $errors = array(); // Example 1: check for only letters if (!preg_match('|^[a-z]+$|i', $_POST['firstname'])) $errors[] = "First name can contain only letters!"; // Example 2: check for only numbers if (!preg_match('|^[0-9]+$|', $_POST['age'])) $errors[] = "Age can contain only digits!"; // Example 3: check for email address if (!preg_match('|^[A-Z0-9._%-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$|i', $_POST['email'])) $errors[] = "You must provide a valid email address!"; // See if we have any errors if (count($errors) < 1) { // No errors, so process the form! // If processing is successful, set up your confirmation message in the $msg variable } else { // Set up our error message $msg = "<p class=\"error\">The following errors have occurred: " . implode('<br />', $errors) . "</p>\n"; }}// Show our message if we have oneecho isset($msg) ? $msg : '';// Here you display the form with an action of ""?>[/code]You notice that I said to set your action to "". This causes the form to be sent back to the current page upon submission. When this happens, your PHP will kick in and process the form. Now, keep in mind that the $_POST['submit'] is assuming that your form submission button is named "submit". If you have another name for your button, just update the PHP to reflect that.Good luck. Quote Link to comment https://forums.phpfreaks.com/topic/31581-solved-form-validation/#findComment-146409 Share on other sites More sharing options...
podja Posted December 22, 2006 Author Share Posted December 22, 2006 hm ok, thanks alot. I normally have the PHP and HTML on one page but I was trying to be more organised this time. I shall just put all the PHP in an include. And I have never tried using an array for errors, so hopefully I will learn something new. Thanks a lot for the help, I really appreciate it! Quote Link to comment https://forums.phpfreaks.com/topic/31581-solved-form-validation/#findComment-146418 Share on other sites More sharing options...
podja Posted December 22, 2006 Author Share Posted December 22, 2006 OK, I had a go at that, changed it a little bit. Heres the code I used:[code]<?php if(isset($_POST['submit'])) { $name = $_POST['name']; $email = $_POST['email']; $subject = $_POST['subject']; $message = $_POST['message']; $errors = array(); // create the errors array // Validate the input if(strlen($_POST['name']) < 2) { $errors[] = 'Your first name must be greater than two characters.'; } else if (strlen($_POST['email']) < 5) { $errors[] = 'Please enter a valid email'; } else if (strlen($_POST['message']) < 10) { $errors[] = 'Your message must be greater than ten characters'; } if(empty($errors)) { mail('jack@podja.co.uk', $subject, $message, "FROM: $email"); header("Location: http://podja.co.uk/email-sent.php"); header("Location: page.php"); } else { foreach($errors as $error) { echo "<div class='error'>$error</div>"; } } } ?>[/code]It works, but I want it to display all the errors at once, not just one at a time.http://podja.co.uk/contact.php - Here it is Quote Link to comment https://forums.phpfreaks.com/topic/31581-solved-form-validation/#findComment-146420 Share on other sites More sharing options...
podja Posted December 22, 2006 Author Share Posted December 22, 2006 anyone? Quote Link to comment https://forums.phpfreaks.com/topic/31581-solved-form-validation/#findComment-146462 Share on other sites More sharing options...
obsidian Posted December 22, 2006 Share Posted December 22, 2006 You need to see how I'm doing my validation in my first code sample. Instead of using elseif statements, just handle each of your validations in their own if clause like I showed. This will add each one to the array, and you can combine them to display.Good luck! Quote Link to comment https://forums.phpfreaks.com/topic/31581-solved-form-validation/#findComment-146494 Share on other sites More sharing options...
podja Posted December 22, 2006 Author Share Posted December 22, 2006 Oh yes, simple. Thanks a lot!! Quote Link to comment https://forums.phpfreaks.com/topic/31581-solved-form-validation/#findComment-146518 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.