| Home | Register | FAQ | Members List | Search | Today's Posts | Mark Forums Read |
|
|
#1 (permalink) |
|
Registered User
Join Date: Feb 2007
Posts: 7
|
Php
Hello all, I have a question regarding php validation for a form i have made, im new to programming and can understand what the php does by looking at it but can't seem to be able to write any yet. The problem I have is that I have searched for loads of tutorials and free scripts and they are all good but each one lacks something I want. The form only consists of Name, Telephone, Email and Enquiry and i want to make sure each field is completed properly, if an error occurs in the form i just want red writing to be highlighted above the textbox stating the field has an error. Once all fields pass validation to be directed to my thanks page. But I can't seem to find one that does this. The other option was to purchase a php form generator and use it for all sites from now on so if wondered if anyone knew any good ones to buy and recommendations?? Any help with the coding would be great! : ) Thanks in advance |
|
|
|
|
|
#2 (permalink) |
|
Will work for Marmite
Join Date: May 2007
Location: Sapporo, Japan
Posts: 574
|
That's kinda strange. I would have said that form validation is just about the most common PHP tutorial out there. However, from your description, it sounds like you want client side validation before the form data is submitted. For this you need to use javascript, not PHP. |
|
|
|
#3 (permalink) |
|
Senior Member
Join Date: Oct 2006
Posts: 2,097
|
Don't buy a generator, you can validate forms quite easily in PHP. There are plenty of tutorials and examples out there. If you are looking to have instant validation, you can also do it in Javascript, but the priority would be to do it in PHP, then Javascript, because people can turn off Javascript then get past the validation otherwise. |
|
|
|
#4 (permalink) |
|
Registered User
Join Date: Feb 2007
Posts: 7
|
I first thought about javascript validation but decided against it as it wont block all efforts if user has it switched off. So I guess php was the only option, also would you recommend coding in a seperate page to the form or include all the php into the form? |
|
|
|
#5 (permalink) |
|
Will work for Marmite
Join Date: May 2007
Location: Sapporo, Japan
Posts: 574
|
I would code it all into the form page since you're going to want to redisplay the partially populated form if they need to re-enter something. Have the page post the form input data to itself and use PHP to highlight areas that had bad/missing input. |
|
|
|
#6 (permalink) |
|
blam blam
Join Date: Aug 2006
Location: ann arbor, mi usa
Posts: 527
|
the reason you haven't found anything is because that can't be done in PHP. by the time the user is able to manipulate a form, the PHP has long since finished being compiled. you need to do that with javascript. if you want to make sure that the contents of the fields after they are submitted are validated, you can check the request vars with php in the submit script. but only after the form is submitted. |
|
|
|
#10 (permalink) |
|
Will work for Marmite
Join Date: May 2007
Location: Sapporo, Japan
Posts: 574
|
For each element of the form you want to check the predefined associative array $_POST to a) see if it has any value, and b) perhaps strip out non alphanumeric characters possible security issues (depending what you're doing with it.) e.g. Code:
Email is a little more complex because you want to check if it's in a valid format, as often the server will send a confirmation email or use the address for some purpose. I use this little function: Code:
Pass the function $_POST['email'] or whatever you've called it, and it'll check the validity. A lot of people just check the email format but this also looks up the mx record for the domain to see if it's valid. Sorry, I don't really have time to do much more. Gotta go to bed. Last edited by Snowshiro : 15-06-2007 at 17:43. |
|
|
|
#11 (permalink) |
|
Website Developer
Join Date: Jun 2007
Location: Pacific Northwest
Posts: 369
|
I always separate the the form, and the form processor... mainly due to refresh issues. And by using SESSION you can keep the information and pull it back if the processor fails and the user is sent back to the form. |
|
|
|
#12 (permalink) |
|
www.intrinzicdesignz.com
Join Date: Aug 2007
Posts: 15
|
Try this On your PHP Code that will handle the form. PHP Code:
On your form, type the error variables above each appropriate fields. Make sure that your form have the value of what was posted so the users don't have to retype whatever they already typed. |
|
![]() |