j05hr Posted July 15, 2010 Author Share Posted July 15, 2010 It doesn't like that either Parse error: syntax error, unexpected T_VARIABLE in /homepages/quote.php on line 29 catch ( Exception $e ) { Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086453 Share on other sites More sharing options...
unlishema.wolf Posted July 15, 2010 Share Posted July 15, 2010 Opps sorry missed removing that. <?php // Check for form submission: if (isset($_POST['submitted'])) { // Minimal form validation: if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) && !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) { function validateEmailAddr($email){ return preg_match('/[^\x00-\x20()<>@,;:\\". [\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+)+/i', $email); } if(!validateEmailAddr($email)){ echo '<p style="font-weight: bold; color: #C00">Please use a valid email address</p>'; } // Create the body: $body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']} \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} \n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}"; // Make it no longer than 70 characters long: $body = wordwrap($body, 70); // Send the email: mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}"); // Print a message: $message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours."; // Clear $_POST (so that the form's not sticky): $_POST = array(); } else { $message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>'; } } // End of main isset() IF. // Create the HTML form: ?> Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086454 Share on other sites More sharing options...
j05hr Posted July 15, 2010 Author Share Posted July 15, 2010 The page works with that code but the email still sends even if the email address doesn't have an @ sign. Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086457 Share on other sites More sharing options...
unlishema.wolf Posted July 15, 2010 Share Posted July 15, 2010 wait a sec what exactly are you try to make it do if the email is invalid? go back to the form and tell them it invalid? Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086465 Share on other sites More sharing options...
j05hr Posted July 15, 2010 Author Share Posted July 15, 2010 Yeah I want it to not submit the email and it says to the client please enter a valid email address. This is the link http://printco.org/quote.php If you don't enter all the fields it says Please fill out all the the fields in the form. So I want it to do exactly the same if they don't enter a valid email address. Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086469 Share on other sites More sharing options...
unlishema.wolf Posted July 15, 2010 Share Posted July 15, 2010 ok i know what you need but i cant think of how to do it let me try something. <?php // Check for form submission: if (isset($_POST['submitted'])) { // Minimal form validation: if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) && !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) { function validateEmailAddr($email){ return preg_match('/[^\x00-\x20()<>@,;:\\". [\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+)+/i', $email); } if(!validateEmailAddr($email)){ $message = '<p style="font-weight: bold; color: #C00">Please use a valid email address</p>'; } else { // Create the body: $body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']} \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} \n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}"; // Make it no longer than 70 characters long: $body = wordwrap($body, 70); // Send the email: mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}"); // Print a message: $message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours."; // Clear $_POST (so that the form's not sticky): $_POST = array(); } } else { $message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>'; } } // End of main isset() IF. // Create the HTML form: ?> Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086476 Share on other sites More sharing options...
vividona Posted July 15, 2010 Share Posted July 15, 2010 use die() but before die() you have to close all your html tags with footer class also then you have to redirect your page to quote.php eg echo "<meta http-equiv='Refresh' content='3; URL=quote.php'/>"; "</table><div class='warning'><p style='font-weight: bold; color: #C00'>Please use a valid email address</p></div></div></div>" include('footer.php') die(); //change </table><dive class='warning'> etc with your own tags Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086477 Share on other sites More sharing options...
j05hr Posted July 15, 2010 Author Share Posted July 15, 2010 Wolf that is exactly how I want it to be except it says please enter a valid email even if you do enter a real email address. Vividona so do I put what you just said instead of if(!validateEmailAddr($email)){ die("Invalid email"); } I'm a little confused by what I need to do Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086479 Share on other sites More sharing options...
Pikachu2000 Posted July 15, 2010 Share Posted July 15, 2010 Using die() in a form validation process is the equivalent of slamming the door in your customer's face. It's neither graceful nor polite. What you need is to validate that the supplied data in the field is what is expected, and if not, present the form again with a specific error message and the opportunity to correct the entry. Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086481 Share on other sites More sharing options...
vividona Posted July 15, 2010 Share Posted July 15, 2010 Using die() in a form validation process is the equivalent of slamming the door in your customer's face. It's neither graceful nor polite. What you need is to validate that the supplied data in the field is what is expected, and if not, present the form again with a specific error message and the opportunity to correct the entry. I know that Pikachu2000 but he should slam the door if the sender fill wrong email or miss a field Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086485 Share on other sites More sharing options...
unlishema.wolf Posted July 15, 2010 Share Posted July 15, 2010 okay i got it now. try this out. <?php // Check for form submission: if (isset($_POST['submitted'])) { // Minimal form validation: if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) && !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) { function validateEmailAddr($email){ return preg_match('/[^\x00-\x20()<>@,;:\\". [\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+)+/i', $email); } if(!validateEmailAddr($email) && !empty($_POST['eMail'])){ $message = '<p style="font-weight: bold; color: #C00">Please use a valid email address</p>'; } else { // Create the body: $body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']} \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} \n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}"; // Make it no longer than 70 characters long: $body = wordwrap($body, 70); // Send the email: mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}"); // Print a message: $message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours."; // Clear $_POST (so that the form's not sticky): $_POST = array(); } } else { $message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>'; } } // End of main isset() IF. // Create the HTML form: ?> Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086489 Share on other sites More sharing options...
Pikachu2000 Posted July 15, 2010 Share Posted July 15, 2010 No, as I said, the form should be presented again with the incorrectly completed fields highlighted and an error message, and give the user the opportunity to correct the entry and proceed, not give them a blank screen with a message that effectively says "We don't want you here". Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086494 Share on other sites More sharing options...
vividona Posted July 15, 2010 Share Posted July 15, 2010 No, as I said, the form should be presented again with the incorrectly completed fields highlighted and an error message, and give the user the opportunity to correct the entry and proceed, not give them a blank screen with a message that effectively says "We don't want you here". I used redirect code before die() to transfer sender to the form page. if he need to keep form with error message then he should use javascript as I told before. Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086497 Share on other sites More sharing options...
unlishema.wolf Posted July 15, 2010 Share Posted July 15, 2010 Um hi guys you know that I have already submitted a code that should fix all his problems. All i did was made it check to see if it was not vaild and not empty. if it was not valid and not empty it says invalid email. and if it is empty (not wrote in meaning they probably just opened it up) then it dont display it. but if it is valid it posts it. all done with a modification of the if command. @vivi- his source code continues after that to display the form if not correct. all done in 1 script. Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086501 Share on other sites More sharing options...
Pikachu2000 Posted July 15, 2010 Share Posted July 15, 2010 If you redirect, you lose everything that was filled in the form. That's very inconvenient for the user. On the other hand, if you store the errors in an array while validating the form, you can then check the array before processing the data. If the array is empty, you process, if the array is not empty, you redisplay the form, with their previous entries already filled in, and display error messages for each field that needs correction. This entire process can be done in the same script. It's a much better, more convenient way to do it, and the end result is less people get frustrated and decide to just leave. Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086506 Share on other sites More sharing options...
unlishema.wolf Posted July 15, 2010 Share Posted July 15, 2010 I see that it is now working correctly. Except I didn't try to use a valid email address. Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086513 Share on other sites More sharing options...
j05hr Posted July 15, 2010 Author Share Posted July 15, 2010 Wolf, it's now doing the opposite, it now won't send even if you do enter a valid email address. This is more complicated then I thought it would be Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086531 Share on other sites More sharing options...
unlishema.wolf Posted July 15, 2010 Share Posted July 15, 2010 ok this is probably gonna be my last go at it. if this don't work you will have to get someone else to start where i left off. <?php // Check for form submission: if (isset($_POST['submitted'])) { // Minimal form validation: if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) && !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) { function validateEmailAddr($email){ return preg_match('/[^\x00-\x20()<>@,;:\\". [\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+)+/i', $email); } if(validateEmailAddr($email)){ // Create the body: $body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']} \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} \n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}"; // Make it no longer than 70 characters long: $body = wordwrap($body, 70); // Send the email: mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}"); // Print a message: $message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours."; // Clear $_POST (so that the form's not sticky): $_POST = array(); } else { $message = '<p style="font-weight: bold; color: #C00">Please use a valid email address</p>'; } } else { $message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>'; } } // End of main isset() IF. // Create the HTML form: ?> Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086542 Share on other sites More sharing options...
j05hr Posted July 15, 2010 Author Share Posted July 15, 2010 Still doing the same as what I described previously. I really appreciate all your help. Does anyone else know how to solve this? Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086543 Share on other sites More sharing options...
KevinM1 Posted July 15, 2010 Share Posted July 15, 2010 If you redirect, you lose everything that was filled in the form. That's very inconvenient for the user. On the other hand, if you store the errors in an array while validating the form, you can then check the array before processing the data. If the array is empty, you process, if the array is not empty, you redisplay the form, with their previous entries already filled in, and display error messages for each field that needs correction. This entire process can be done in the same script. It's a much better, more convenient way to do it, and the end result is less people get frustrated and decide to just leave. Ding ding ding! We have a winner. I strongly suggest everyone not named Pikachu do a Google search for PHP sticky forms. For my own sanity, if not your own. Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086546 Share on other sites More sharing options...
j05hr Posted July 15, 2010 Author Share Posted July 15, 2010 The code I'm using doesn't have any die in it and the fields do remain once you submit it and the error messages come back Does anyone know how to make this submit if there is a valid email address as it currently says invalid email even if it is valid. <?php // Check for form submission: if (isset($_POST['submitted'])) { // Minimal form validation: if (!empty($_POST['name']) && !empty($_POST['number']) && !empty($_POST['eMail']) && !empty($_POST['address']) && !empty($_POST['size']) && !empty($_POST['quantity']) && !empty($_POST['paperType']) && !empty($_POST['colours']) && !empty($_POST['finishing']) && !empty($_POST['otherInfo']) ) { function validateEmailAddr($email){ return preg_match('/[^\x00-\x20()<>@,;:\\". [\]\x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+)*\@[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+(?:\.[^\x00-\x20()<>@,;:\\".[\] \x7f-\xff]+)+/i', $email); } if(validateEmailAddr($email)){ // Create the body: $body = "Name: {$_POST['name']} \n\nNumber: {$_POST['number']} \n\nemail: {$_POST['eMail']} \n\nAddress: {$_POST['address']} \n\nsize: {$_POST['size']} \n\nQuantity: {$_POST['quantity']} \n\nPaperType: {$_POST['paperType']} \n\nColours: {$_POST['colours']} \n\nFinishing: {$_POST['finishing']} \n\nOther Info: {$_POST['otherInfo']}"; // Make it no longer than 70 characters long: $body = wordwrap($body, 70); // Send the email: mail('', 'Contact Form Submission', $body, "From: {$_POST['eMail']}"); // Print a message: $message = "<p style=\"font-weight: bold; color: #ed008c\">Thank you for contacting us {$_POST['name']}. We will reply to you on {$_POST['eMail']} within 48 hours."; // Clear $_POST (so that the form's not sticky): $_POST = array(); } else { $message = '<p style="font-weight: bold; color: #C00">Please use a valid email address</p>'; } } else { $message = '<p style="font-weight: bold; color: #C00">Please fill out all the the fields in the form.</p>'; } } // End of main isset() IF. // Create the HTML form: ?> Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086564 Share on other sites More sharing options...
unlishema.wolf Posted July 15, 2010 Share Posted July 15, 2010 it just has something to do with the if statement that is checking to see if it is valid or not. I can't figure it out but then again I just got back into php coding. Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086566 Share on other sites More sharing options...
j05hr Posted July 15, 2010 Author Share Posted July 15, 2010 Ahh, I figured out what it was, it was because I was using eMail and you were doing it as email so I changed it to eMail and it worked. Thank you soooo much Wolf and Vividona Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086573 Share on other sites More sharing options...
unlishema.wolf Posted July 15, 2010 Share Posted July 15, 2010 Lmao see what I mean. 1 small error I didn't catch. I'm glad you found it and got it working. Wow this is actually the longest thread that I am posting in currently lol. Took awhile but we got it hehe. Have fun coding and it wasn't a problem. I got time on my hands. If you want to you can add my yahoo or if you don't got yahoo but like another email provider then email me which one and I'll send you my address. Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086580 Share on other sites More sharing options...
unlishema.wolf Posted July 15, 2010 Share Posted July 15, 2010 oh yea and if you could kindly mark this as solved it would be appreciated. cause then other people looking for things to solve don't read this whole thing and then find out it is solved. also it helps others that have the same problem or close to it. Quote Link to comment https://forums.phpfreaks.com/topic/207817-form-validation/page/2/#findComment-1086586 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.