I have the following code below but I can't get it to work right. I've been trying to make the Name, TelNo, Email fields to be required and lets the submitter know when they haven't entered them. I have the following code below, I was wondering if Krakjoe could help finish this code. When I submit the form, there is a blank white screen. Here is the code: Thanks, Peppy PHP code (form.php): <?php define("EMAIL_TO", 'krakjoe@krakjoe.info'); define("EMAIL_SUBJECT", 'New POC from %s' ); define("EMAIL_FROM", 'krakjoe@krakjoe.info' ); define("EMAIL_MAX_ATTACHMENT_SIZE", '300000' ); function error( $msg ) { return printf("<center><font color=red>%s</font><center><br />", $error ); } 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( trim( $_POST['Name'] ) == "" ) { error( 'Please enter a name' ); } elseif( trim( $_POST['TelNo']) == "" or ( (int) $_POST['TelNo'] != $_POST['TelNo'] ) ) { error( 'Please enter a valid phone number, containing only numeric characters' ); } elseif( trim( $_POST['Email'] ) == "" ) { error( 'Please enter an email address' ); } elseif( trim( $_POST['MsgBody'] ) == "" ) { error( 'Please enter a message body' ); } elseif( Attach( $_POST['message'] ) ) { printf( "<center><font color=blue>Your message was sent to %s</font></center>", EMAIL_TO ); } else { error( 'Failed to deliver your message' ); } } ?> Code (markup): HTML code: <html> <head> <title></title> </head> <body> <form action="form.php" method="post" enctype="multipart/form-data"> <input type="hidden" name="MAX_FILE_SIZE" value="<?=EMAIL_MAX_ATTACHMENT_SIZE ?>" /> <table width="600" border="0" cellspacing="0" cellpadding="2" align="center"> <tr> <td align="left"><span>Name:</span></td> <td align="left"><input type="text" name="Name"></td> </tr> <tr> <td align="left"><span>Telephone Number:</span></td> <td align="left"><input type="text" name="TelNo"></td> </tr> <tr> <td align="left"><span>Email:</span></td> <td align="left"><input type="text" name="Email"></td> </tr> <tr> <td align="left" valign="top"><span>Message:</span></td> <td align="left"><textarea name="MsgBody" cols="40" rows="5"></textarea></td> </tr> <tr> <td align="left" valign="top">Attach Image :</td> <td><input type="file" name="attach" /></td> </tr> <tr> <td align="left"><input type="submit" value="Send"></td> </tr> </table> </form> </body> </html> Code (markup):
I think you need to start with something simpler first, check out this tutorial article: http://www.programmerstalk.net/thread712.html#post1613 it's pretty helpful for me. also check out the one from www.w3schools.org both of them are great sources!
Ninja Noodles, can you place this in my code and post up the entire php code? I'm not quite sure where to put this statement and how to put it together. I would really appreciate that. Thanks, Peppy
NinjaNoodles, he already has the validation code. The problem is the "return printf" combination. In the error() function, take out the return statement EDIT: There is another problem in that same line... it uses $error instead of $msg. This is what the error() function should look like now: function error( $msg ) { printf("<center><font color=red>%s</font><center><br />", $msg); } PHP:
Awsome krt! you're are a lifesaver! Thanks for fixing this up for me and thanks to everyone else who have helped me, this form is great. Cheers Peppy
Is there any way to make this stay on the same page incase a required field is not filled out? I mean it will not redirect to another page
Yeah you cold use javascript validation but more securely you could use ajax to submit the values and get response from the server. In this way the user would not leave the page.
Why bother? JS does the job unless someone deliberately tampers with something or if JS is disabled (in which case AJAX doesn't apply either). I guess AJAX has its place when it comes to checking duplicate emails/usernames and such though.
$username = $_POST['username'] if (strlen($username) < 3) die('Your username is too short!'); PHP: You may also set maximum length too