I am trying to make all my fields required on a registration form. If a visitor doesn't fill out the form, I would like a small window to pop up and let them know they have not filled out the form correctly and take them back to the page without deleting info they put in. If there is any Javascript involved, I would like this on the PHP portion and not on my HTML page due to search engine issues. Could someone please take a look at my PHP and HTML codes below and post what they SHOULD look like in order to have all my fields required? I would really appreciate it. Thank You, Peppy HTML Portion (registration-page.html): <form action="form.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="<?=EMAIL_MAX_ATTACHMENT_SIZE ?>" /> <table border="0" cellpadding="4" cellspacing="0" align="right"> <tr> <td valign="top" align="right"> <em class="small">Company Name: </em><input type="text" name="Name" style="LEFT: 0px; TOP: 2px" size="40"></td> </tr> <tr> <td valign="top" align="right"> <em class="small">Address line 1: </em><input type="text" name="AddressLine1" style="LEFT: 0px; TOP: 2px" size="40"></td> </tr> <tr> <td valign="top" align="right"> <em class="small">Address line 2: </em><input type="text" name="AddressLine2" style="LEFT: 0px; TOP: 2px" size="40"></td> </tr> <tr> <td valign="top" align="right"> <em class="small">City: </em><input type="text" name="City" style="LEFT: 0px; TOP: 2px" size="40"></td> </tr> <tr> <td valign="top" align="right"> <em class="small">State/Country: </em><input type="text" name="State" style="LEFT: 0px; TOP: 2px" size="40"></td> </tr> <tr> <td valign="top" align="right"> <em class="small">Postal Code/Zip Code: </em><input type="text" name="Zip" style="LEFT: 0px; TOP: 2px" size="40"></td> </tr> <tr> <td valign="top" align="right"> <em class="small">Phone: </em><input type="text" name="Phone" style="LEFT: 0px; TOP: 2px" size="40"></td> </tr> <tr> <td valign="top" align="right"> <em class="small">Fax: </em><input type="text" name="Fax" style="LEFT: 0px; TOP: 2px" size="40"></td> </tr> <tr> <td valign="top" align="right"> <em class="small">Website: </em><input type="text" name="Website" style="LEFT: 0px; TOP: 2px" size="40"></td> </tr> <tr> <td valign="top" align="right"> <em class="small">Contact Name: </em><input type="text" name="ContactName" style="LEFT: 0px; TOP: 2px" size="40"></td> </tr> <tr> <td valign="top" align="right"> <em class="small">Contact Title: </em><input type="text" name="ContactTitle" style="LEFT: 0px; TOP: 2px" size="40"></td> </tr> <tr> <td valign="top" align="right"> <em class="small"><left>Email:</left> </em><input type="text" name="Email" style="LEFT: 0px; TOP: 2px" size="40"></td> </tr> <tr> <td valign="top" align="right"> <em class="small"><left>Attach File:</left> </em><input type="file" name="file" style="LEFT: 0px; TOP: 2px" size="27" id="file" /></td> </tr> <tr> <td valign="top" align="left"> <em class="small">Company Description: </em> <textarea style="width: 570; height:250" rows="6" name="CompanyDescription"></textarea></td> </tr> <tr> <td valign="top" align="center"><input type="submit" value="Submit"> <input type="reset" value="Clear"></td> </tr> </table> </form> Code (markup): PHP Portion (form.php): <?php define("EMAIL_TO", 'bigsnap@bizmail.com'); define("EMAIL_SUBJECT", 'Registration' ); define("EMAIL_FROM", 'bigsnap@bizmail.com' ); define("EMAIL_MAX_ATTACHMENT_SIZE", '300000' ); function Attach( $message, $headers = null ) { // Build your message from your posted form $message = nl2br( sprintf( "Name : %s\n". "Telephone : %s\n". "Email : %s\n". "%s", $_POST['Name'], $_POST['TelNo'], $_POST['Email'], $message ) ); // start to add to headers $headers .= sprintf( "From: %s\n", EMAIL_FROM ); // check to see if we are attaching if( is_uploaded_file( $_FILES['attach']['tmp_name'] ) ) { // the content needs a boundary so that email clients know where to get data $boundary = sprintf( '==Multipart_Boundary_x%s', md5( time( ) ) ) ; // continue to build headers, adding the boundary for attached data $headers .= sprintf( "MIME-Version: 1.0\n". "Content-Type: multipart/mixed;\n boundary=\"%s\"\n", $boundary ); // edit message to include data and boundaries $message = sprintf( "This is a multi-part message in MIME format.\n\n". "--%s\n". "Content-Type: text/html; charset=\"iso-8859-1\"\n". "Content-Transfer-Encoding: 7bit\n\n". "%s\n\n". "--%s\n". "Content-Type: %s;\n name=\"%s\"\n". "Content-Disposition: attachment;\n filename=\"%s\"\n". "Content-Transfer-Encoding: base64\n\n". "%s\n\n". "--%s-\n", $boundary, $message, $boundary, $_FILES['attach']['type'], $_FILES['attach']['name'], $_FILES['attach']['name'], chunk_split( base64_encode( file_get_contents( $_FILES['attach']['tmp_name'] ) ) ), $boundary ); } // send the original message with the file attached return mail( EMAIL_TO, sprintf( EMAIL_SUBJECT, $_SERVER['REMOTE_ADDR'] ), $message, $headers ); } if( $_POST ) { if( Attach( $_POST['message'] ) ) { printf(" <html> <head> <meta http-equiv=REFRESH content=5;url=http://www.oursite.com/> <title>Thank You!</title> <style> a:hover {color:#FF0000;} BODY{font-size:10pt;font-family:Verdana,Arial,Helvetica,sans-serif} TD{font-size:10pt;font-family:Verdana,Arial,Helvetica,sans-serif} .ltxt{font-size:14pt;font-family:Verdana,Arial,Helvetica,sans-serif}</style> </head> <body bgcolor=FFFFFF link=009900 vlink=009900> <div align=center><center> <table align=center> <tr> <td height=30></td> </tr> <tr> <td align=center><b><font color=000080 class=ltxt>Thank you for contacting us!</font></b></td> </tr> <tr> <td height=10></td> </tr> <tr> <td align=center><b><i><font color=FF0000>The information you submitted has been sent successfully!</font></i></b></td> </tr> <tr> <td height=15></td> </tr> <tr> <td align=center><a href=http://www.oursite.com/>Click here if your browser does not automatically redirect you back to the Member's Page.</a></td> </tr> <tr> <td height=15></td> </tr> </table> </center></div> <p align=center><br><font face=Arial size=1>© Information Service Network brought to you by our site. All Rights Reserved.</font></p> </body> </html>"); } else { print("<center><font color=red>Failed to deliver your message</font></center>"); } } ?> Code (markup):
The quickest way is to install form validation is a free JavaScript called FOUL. It takes 2 minutes to install and if you follow the (very simple) directions, it works like a charm.
The disadvantage of checking your forms in Javascript (client-side) is that if the user has javascript disabled, there is no checking. To create a robust form processor, you merge the form and the processor into one php file which does server-side form validation. When you do it this way you can echo all of the valid form data (you get it from the $_POST array) so the user doesn't have to reenter it. It's more work but it's worth it because it allows you to create a form that's robust but also user-friendly.
Thank you for your replies, I do agree with lemaitre, this is one reason I don't like javascript in the html. Another reason I don't like it is the grey area topic of getting penalties from the search engine if the code is doing something that doesn't agree with Googlebot or other search engine spiders. I'm sorry I came back so soon after you guys working on the problem but I figured it out myself now. For anyone interested in this, I just added the following code in the php right before "function attach" and added this input statement below the maximum file size one in html. Thanks for the help Cheers! Peppy extra HTML code: <input type="hidden" name="required" value="Name,AddressLine1,AddressLine1,City,State,Zip,Phone,Website,ContactName,ContactTitle,Email,CompanyDescription,file"> Code (markup): extra PHP code: if ($required) { $ra=explode(",", $required); $num=count($ra); } $results=""; reset ($HTTP_POST_VARS); while (list ($key, $val) = each ($HTTP_POST_VARS)) { if (($key!="recipient") && ($key!="recipient_name") && ($key!="reply_subject") && ($key!="reply_text") && ($key!="subject") && ($key!="required") && ($key!="redirect")) { for($i=0;$i<$num;$i++) { if (($key==$ra[$i]) && ($val=="")) DIE ("<html><script language='JavaScript'>alert('Please fill in the $ra[$i] field!'),history.go(-1)</script></html>"); } $results.=$key.": ".stripslashes($val)."\n"; }} Code (markup):
Clever hack but it still depends on Javascript I'm not going to write any code to do it the way I suggested because it's too much work, but when you need to do things right I think it's the best way to go.
I would not store what is required in the form itself. Here is what I do to get around your validation. All I do is make my own form remove the hidden required field and submit. Hard code what is required in some way into your script opposed to reading some field. I hope I understand your code right from the quick glance.