Jump to content

php form validator


orr16875

Recommended Posts

i need the form validator to check the form without the user need to refill the whole form from scrach, just popping up an alert to fill up required fields. this is my file:<?php$sendto = "info@inhomeestimates.com";$Date = $_POST['Date'];$Project: = $_POST['Project:'];$Description = $_POST['Description'];$schedule = $_POST['schedule'];$FirstName = $_POST['FirstName'];$LastName = $_POST['LastName'];$Address = $_POST['Address'];$City = $_POST['City'];$State = $_POST['State'];$Zip = $_POST['Zip'];$HomePhone = $_POST['HomePhone'];$WorkPhone = $_POST['WorkPhone'];$Cell = $_POST['Cell'];$eMail = $_POST['eMail'];$Estimates = $_POST['Estimates'];$Budget = $_POST['Budget'];$Advertising = $_POST['Advertising'];$agree = $_POST['agree'];$sendmail= mail("$sendto", "$Project:", from: $eMail");$if($FirstName || $City || $HomePhone || $eMail == "" ) { echo ("please fill in First Name, City, Home phone # and Email address"); }else { $sendmail = mail("$sendto", "$Date", "$Project:", "$Description", "$schedule", "$FirstName", "$LastName", "$Address", "$City", "$State", "$Zip", "$HomePhone"?>another thing:when i click "submit" in the form file, windows offers to open the mail.php file. why is that?this is the part of the form code:<form method="post" action="mail.php" enctype="application/x-www-form-urlencoded">

Link to comment
Share on other sites

i need the form validator to check the form without the user need to refill the whole form from scrach, just popping up an alert to fill up required fields.
if you dont want the user to fill in the fields over agin you should use javascript. some users will block the use of javascript so you should aslo check again in your php. another option would be when the user submits, set a cookie for each correct field. then send them back to the form. on the form check for that cookie and if it is set then have it echo that value into the input field.
Link to comment
Share on other sites

Actually, I've experimented with using PHP to write dynamic javascript form-validation code, it would work something like this:The page with the form includes the following line

<script type="text/javascript" src="createValidation.php?form=theformName&nonblank=a,b,c&email=d>
function produceValidationScript($FormName, $Required=array(), $Email=array() $ScriptName='validate'){  $Code = "function validate(thisForm)"  $Code .= 'var valid = true';  $firstLine = true;  foreach($Required as $Field)  {	if($firstLine)	  $firstLine = false;	else	  $Code .= 'else ';	$Code .= 'if(!(' . getNonBlankJS($Field) . '))';	$Code .= getAlertAndFocusJS($Field, 'Please specify a value for ' . $Field);	$Code .= '{ valid = false; }'';  }  foreach($Email as $SingleEmail)  {	if($firstLine)	  $firstLine = false;	else	  $Code .= 'else ';	$Code .= 'if(!(' . getValidEmailJS($Email) . '));	$Code .= getAlertAndFocusJS($Field, 'Please specify an appropriate e-mail address');	$Code .= '{ valid = false; }';  }  $Code .= 'return valid;\r\n';}function getNonBlankJS($ElementID){//Return javascript code that returns whether an element is blank or not}function getAlertAndFocusJS($ElementID, $Message){//Return javascript alert code that focuses on the item in question and alerts the message}function getValidEmailJS($ElementID){//Return javascript code that returns whether an e-mail is valid or not}

And then you could just call the function in the validation.php file like this:

print validate(explode($_GET['form'], explode(',', $_GET['required']), explode(',', $_GET['email'));

Granted, this seems like almost as much a pain as just writing the JS yourself, but if you have a website dealing with alot of forms it might bre pretty useful.You could also make this code more efficient by including the javascript code snippets in a separate file with a instead of producing them on the fly every time, for one thing.

Link to comment
Share on other sites

An easy way to do this is just have the form and validator on the same page. Here's my online example:http://manchine.net/w3/formdemo.phpAnd I posted the code here:http://w3schools.invisionzone.com/index.ph...44&hl=howtoIt's best to validate in PHP. You can validate in Javascript also if you want to, but you should definately do PHP no matter what.Also, for your form tag, if you are uploading a file then the enctype needs to be "multipart/form-data". If you are not uploading a file, then you can leave the enctype off the form tag, the default will be fine. That will fix the file download box popping up.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...