robmo Posted August 23, 2010 Share Posted August 23, 2010 Hi, I am trying to setup a PHP script that will validate an html form. The form and the PHP script are separate from each other. The code that I have was modified from a tutorial on YouTube. The example used there works but as I have began to tailor it to my purpose, it is now broke. I just added one line for validation to simplify troubleshooting. If I find an empty field on the form, I would like to display the error message on the form itself rather than start a new html page with the PHP script. I am not sure if that is possible. Here is my form: http://www.tallfirshoa.com/adform.htm YouTube videos that I was working from. The video's example has the form and PHP combined as one. In my case, I need the form and PHP separate. http://www.youtube.com/watch?v=yuLpSospbBk&feature=search http://www.youtube.com/watch?v=LF5zTWthpn0&feature=search The code works fine if I leave out the validation. As soon as I add if(!$fname), the script becomes broken. I'm guessing that it has something to do with $errorstring and how it works. I am new to PHP and have tried as many things as I can think of but I can't make any forward progress. Thanks for your help! Rob <html> <h1>Tall Firs Ad Submission</h1> <?php if ($_POST['submit']) { //Get form data $fname = $_POST['fname']; $lname = $_POST['lname']; $email = $_POST['email']; $displayemail = $_POST['displayemail']; $phone = $_POST['phone']; $category = $_POST['category']; $description = $_POST['description']; //Declare variables $to = "email@gmail.com"; $subject = "Request"; $headers = "From: $email \r\n Reply-To: $email"; } //Setup form validation $errorstring = "" //default value of error string //Begin validation if (!$fname) $errorstring = $errorstring."First Name<br>"; ?> </html> Quote Link to comment https://forums.phpfreaks.com/topic/211543-form-validation/ Share on other sites More sharing options...
hcdarkmage Posted August 23, 2010 Share Posted August 23, 2010 First off, you need to move the if(!fname) into the if($_POST['submit']). if ($_POST['submit']) { //Get form data $fname = $_POST['fname']; $lname = $_POST['lname']; $email = $_POST['email']; $displayemail = $_POST['displayemail']; $phone = $_POST['phone']; $category = $_POST['category']; $description = $_POST['description']; //Begin validation if (!$fname){ $errorstring = $errorstring."First Name<br>"; } //Declare variables $to = "email@gmail.com"; $subject = "Request"; $headers = "From: $email \r\n Reply-To: $email"; } //Setup form validation $errorstring = "" //default value of error string The reason it wasn't working was because you were not declaring $fname anyhwere except in the $_POST. Quote Link to comment https://forums.phpfreaks.com/topic/211543-form-validation/#findComment-1102839 Share on other sites More sharing options...
Pikachu2000 Posted August 23, 2010 Share Posted August 23, 2010 You've left the line terminating semicolon off the $errorstring = "" on line 25. Also, you'd be better off to store validation errors in an array, then you can loop through the array to list them. Much more flexible way of doing it, IMHO. Quote Link to comment https://forums.phpfreaks.com/topic/211543-form-validation/#findComment-1102840 Share on other sites More sharing options...
robmo Posted August 23, 2010 Author Share Posted August 23, 2010 I can't believe I did that. I guess you go crazy and miss simple things when you look at it too hard and too long. I added some additional code and it does tell you if you need to fill out something. For now it's setup so the PHP script generates a new html page and displays which fields require information. From there, users would have to hit the back button to correct the fields called out. Is it possible to display the error messages on the form itself rather than generating a new page? I'd basically like the Submit to fail and not change the page at all yet display the error messages where appropriate. I like your idea with the array. There are a number of variables and it could really reduce lines of code by looping through them. Quote Link to comment https://forums.phpfreaks.com/topic/211543-form-validation/#findComment-1102851 Share on other sites More sharing options...
hcdarkmage Posted August 23, 2010 Share Posted August 23, 2010 You could do something like this: if($_POST['submit']{ //get your form data foreach($_POST as $key=>$value){ $$key = $value; } //set a default $errors = false; //form validate if(!fname){ $errors = true; $errormessage = "Your message"; } //if no errors, send information if($errors == false){ //Information things } } Then, somewhere above your form you put echo $errormessage; Quote Link to comment https://forums.phpfreaks.com/topic/211543-form-validation/#findComment-1102856 Share on other sites More sharing options...
robmo Posted August 23, 2010 Author Share Posted August 23, 2010 First off, you need to move the if(!fname) into the if($_POST['submit']). The reason it wasn't working was because you were not declaring $fname anyhwere except in the $_POST. hcdarkmage, I'm not sure I understand what you're saying here. Are you saying you have to declare the variables in advance to using them? Like using DIM in visual basic? When I added the missing semicolon after the $errorstring statement, the script started working so I'm not sure how your explanation helps me. Do you think you could clarify your meaning? Thanks a lot! Rob Quote Link to comment https://forums.phpfreaks.com/topic/211543-form-validation/#findComment-1102877 Share on other sites More sharing options...
jcbones Posted August 23, 2010 Share Posted August 23, 2010 First off, you need to move the if(!fname) into the if($_POST['submit']). The reason it wasn't working was because you were not declaring $fname anyhwere except in the $_POST. hcdarkmage, I'm not sure I understand what you're saying here. Are you saying you have to declare the variables in advance to using them? Like using DIM in visual basic? When I added the missing semicolon after the $errorstring statement, the script started working so I'm not sure how your explanation helps me. Do you think you could clarify your meaning? Thanks a lot! Rob No what he is saying is, if $_POST['submit'] was not set, you would always get the $errorstring for fname. Moving it inside of the $_POST if statement would only return the error string, IF the form had been posted. Quote Link to comment https://forums.phpfreaks.com/topic/211543-form-validation/#findComment-1102881 Share on other sites More sharing options...
robmo Posted August 23, 2010 Author Share Posted August 23, 2010 jcbones, Ok, that makes sense now. Unless Submit was clicked though the PHP script would never be called so does it really matter? It sounds like the way I have it is not best practice even if does happen to work in my example. I will move the code as suggested. Should I also move the $errorstring = ""; into that area as well? Thanks again, Rob Quote Link to comment https://forums.phpfreaks.com/topic/211543-form-validation/#findComment-1102895 Share on other sites More sharing options...
robmo Posted August 24, 2010 Author Share Posted August 24, 2010 Would anyone be able to comment on my last post? I will post my new code once I have working if anyone is interested. Quote Link to comment https://forums.phpfreaks.com/topic/211543-form-validation/#findComment-1103229 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.