Old 15-06-2007, 08:11   #1 (permalink)
Kelukina
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
  Reply With Quote
Old 15-06-2007, 08:38   #2 (permalink)
Snowshiro
Will work for Marmite
 
Snowshiro's Avatar
 
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.
  Reply With Quote
Old 15-06-2007, 08:42   #3 (permalink)
freelancr
Senior Member
 
freelancr's Avatar
 
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.
  Reply With Quote
Old 15-06-2007, 08:54   #4 (permalink)
Kelukina
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?
  Reply With Quote
Old 15-06-2007, 09:22   #5 (permalink)
Snowshiro
Will work for Marmite
 
Snowshiro's Avatar
 
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.
  Reply With Quote
Old 15-06-2007, 09:28   #6 (permalink)
dtrenz
blam blam
 
dtrenz's Avatar
 
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.
  Reply With Quote
Old 15-06-2007, 09:30   #7 (permalink)
Kelukina
Registered User
 
Join Date: Feb 2007
Posts: 7
I def agree Snowshiro. By doing that i get to keep the errors displayed nicely on the form and keeps its currrent content. Now to find some code for this........ Does anyone use a script similar to these specifications?
  Reply With Quote
Old 15-06-2007, 09:32   #8 (permalink)
dtrenz
blam blam
 
dtrenz's Avatar
 
Join Date: Aug 2006
Location: ann arbor, mi usa
Posts: 527
spend 10 minutes on php.net and code it yourself.
  Reply With Quote
Old 15-06-2007, 09:44   #9 (permalink)
Kelukina
Registered User
 
Join Date: Feb 2007
Posts: 7
I dont know php code that well to even attempt to write it so if anyone could help i would be really greatful.
  Reply With Quote
Old 15-06-2007, 09:49   #10 (permalink)
Snowshiro
Will work for Marmite
 
Snowshiro's Avatar
 
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:
if(!$_POST['first_name']) { /* code to highlight the form field that has no input */ } else { /* strip non-alphanumeric chars and do whatever with the input */ } ... <FORM> <!-- HTML to display the form with embedded PHP to highlight missing/invalid fields --> </FORM>

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:
function validate_email($email) { // Create the syntactical validation regular expression $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)(\.[a-z0-9-]+)*(\.[a-z]{2,4})$"; // Presume that the email is invalid $valid = 0; // Validate the syntax if (eregi($regexp, $email)) { list($username,$domaintld) = split("@",$email); // Validate the domain if (getmxrr($domaintld,$mxrecords)) $valid = 1; } else { $valid = 0; } return $valid; }

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.
  Reply With Quote
Old 18-06-2007, 14:50   #11 (permalink)
cjgraphix
Website Developer
 
cjgraphix's Avatar
 
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.
  Reply With Quote
Old 19-08-2007, 21:35   #12 (permalink)
intrinzic
www.intrinzicdesignz.com
 
intrinzic's Avatar
 
Join Date: Aug 2007
Posts: 15

Try this

On your PHP Code that will handle the form.

PHP Code:
if(empty($_POST['name'])){
    
    
$name_error "<font color=red>Please type your name!</font>";
    
}elseif(empty(
$_POST['email'])){
    
    
$email_error "<font color=red>Please type your email address!</font>";
    
}elseif(empty(
$_POST['telephone'])){
    
    
$telephone_error "<font color=red>Please type your email address!</font>";
    
}elseif(empty(
$_POST['enquiry'])){
    
    
$enquiry_error "<font color=red>Please type your enquiry!</font>";


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.
  Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search


Contact Us - Web Design Forums - Archive - Top
Search Engine Optimization by vBSEO 3.0.0 RC8