mallen Posted September 22, 2008 Share Posted September 22, 2008 I'm trying to set required fields in my form. The sections between the *** lines are the two sections I added. As soon as the form loads it says "The following required fields have not been filled in:" and lets you send the email. I think I just have it out of order. Can anyone help? <?php //******************************************************** $expected = array('fromname','fromemail','phone'); // set required fields $required = array('fromname','fromemail','phone'); // create empty array for any missing fields $missing = array(); //************************************************************** if ($_SERVER['REQUEST_METHOD']=="POST"){ // we'll begin by assigning the To address and message subject $to="me@you.com"; $subject="E-mail with attachment"; // get the sender's name and email address // we'll just plug them a variable to be used later $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">"; // generate a random string to be used as the boundary marker $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; // store the file information to variables for easier access $tmp_name = $_FILES['filename']['tmp_name']; $type = $_FILES['filename']['type']; $name = $_FILES['filename']['name']; $size = $_FILES['filename']['size']; // here we'll hard code a text message // again, in reality, you'll normally get this from the form submission $message = "Here is your file: $name"; // build the message $message .= "Name: $fromname\n\n"; $message .= "Name: $fromemail\n\n"; $message .= "Name: $phone\n\n"; // if the upload succeded, the file will exist if (file_exists($tmp_name)){ // check to make sure that it is an uploaded file and not a system file if(is_uploaded_file($tmp_name)){ // open the file for a binary read $file = fopen($tmp_name,'rb'); // read the file content into a variable $data = fread($file,filesize($tmp_name)); // close the file fclose($file); // now we encode it and split it into acceptable length lines $data = chunk_split(base64_encode($data)); } // now we'll build the message headers $headers = "From: $from\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\""; // next, we'll build the message body // note that we insert two dashes in front of the // MIME boundary when we use it $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // now we'll insert a boundary to indicate we're starting the attachment // we have to specify the content type, file name, and disposition as // an attachment, then add the file content and set another boundary to // indicate that the end of the file has been reached $message .= "--{$mime_boundary}\n" . "Content-Type: {$type};\n" . " name=\"{$name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; define ("MAX_SIZE","1000"); function file_extension($filename) { $path_info = pathinfo($filename); return $path_info['extension']; } $errors=0; $filename = stripslashes($_FILES['filename']['name']); $type = strtolower( file_extension( $filename)); if (($type != "jpg") && ($type != "jpeg") && ($type != "png") && ($type != "pdf") && ($type != "dwg") && ($type != "gif")) { echo 'Invalid file type! Only gif, jpg, pdf and DWG files are allowed to be uploaded</a>.'; echo $path_parts['extension'], "\n"; $errors=1; die (); } if ($_FILES['filename']['size'] >= 1048576) { echo 'You have exceeded the size limit of 1Mb!'; echo ' Actual size of attachment: '.$_FILES['filename']['size']; $errors=1; die (); } // now we just send the message if (@mail($to, $subject, $message, $headers)) echo "Message was sent successfully."; else echo "Failed to send. Please try again."; } } else { ?> <?php //**************************************************************** if (isset($missing)) { echo '<p>The following required fields have not been filled in:</p>'; echo '<ul>'; foreach($missing as $item) { echo "<li>$item</li>"; } echo '</ul>'; } elseif ($_POST && $mailSent) { echo '<p><strong>Your message has been sent. Thank you.</strong></p>'; } //****************************************** ?> <p>Send an e-mail with an attachment:</p> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1"> <p>From name: <input type="text" name="fromname"></p> <p>From e-mail: <input type="text" name="fromemail"></p> <p>Phone: <input type="text" name="phone"></p> <p>File: <input type="file" name="filename"></p> <p><input type="submit" name="Submit" value="Submit"></p> </form> <?php } ?> Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 22, 2008 Share Posted September 22, 2008 The line if "if (isset($missing))", change it to if (isset($_POST['Submit']&&isset($missing))" Quote Link to comment Share on other sites More sharing options...
mallen Posted September 22, 2008 Author Share Posted September 22, 2008 I got this: Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ',' or ')' Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 23, 2008 Share Posted September 23, 2008 Yeah, my bad. Missed a ). if (isset($_POST['Submit'])&&isset($missing)) Quote Link to comment Share on other sites More sharing options...
mallen Posted September 23, 2008 Author Share Posted September 23, 2008 Still doesn't work. Still sends the form without prompting for missing fields. I added .htmlentities I forgot before. <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1"> <p>From name: <input type="text" name="fromname" <?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['fromname']).'"';} ?>></p> <p>From e-mail: <input type="text" name="fromemail" <?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['fromemail']).'"';} ?>></p> <p>Phone: <input type="text" name="phone"<?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['phone']).'"';} ?> ></p> <p>File: <input type="file" name="filename"></p> <p><input type="submit" name="Submit" value="Submit"></p> </form> Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 23, 2008 Share Posted September 23, 2008 So you're getting this message when the page first loads, not after it's submitted? Quote Link to comment Share on other sites More sharing options...
mallen Posted September 23, 2008 Author Share Posted September 23, 2008 After your suggestion I get no message. It lets me send the form. Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 23, 2008 Share Posted September 23, 2008 I'm sorry, I misunderstood. I thought it was displaying the error message when the page first loads. If you're saying that it doesn't catch the errors like it should, that's different. I would actually suggest adding a JavaScript function that catches that stuff before the form is even submitted. http://www.w3schools.com/js/tryit.asp?filename=tryjs_formvalidate Quote Link to comment Share on other sites More sharing options...
mallen Posted September 23, 2008 Author Share Posted September 23, 2008 I used the same code on another form and it worked. I thought I could work it into this form. I think I just have a "if" statement in the wrong place. ??? Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 23, 2008 Share Posted September 23, 2008 I don't see anything wrong off hand, without going through your code line by line. Is it sending the email? What exactly is it doing when you send in the form (please don't say "nothing")? Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 23, 2008 Share Posted September 23, 2008 Well, here's how I usually do form validation on a bunch of fields: (This is a piece of code from a personal little script I just wrote, so change it up as you see fit): foreach ($_POST as $key=>$value) { if ($key == 'submit') { continue; } switch ($key) { case 'name': case 'notes': if (trim($value) == '') { $notes->addError("$key not properly set!"); } break; case 'subject': if (!in_array($value, $subjects)) { $notes->addError("Subject was invalid!"); } break; default: break; } } Quote Link to comment Share on other sites More sharing options...
mallen Posted September 23, 2008 Author Share Posted September 23, 2008 I don't see anything wrong off hand, without going through your code line by line. Is it sending the email? What exactly is it doing when you send in the form (please don't say "nothing")? It is not sending the email anymore and after you hit submit, the form goes away. So I think it has something to do with the order of my "if" "else" statements. Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 23, 2008 Share Posted September 23, 2008 Well, you just need to troubleshoot your code. Here's how I do that: Step 1) throw a junk echo (like echo "blah" half-way down your code. Step 2) Run your code. If you see "blah" on the page, the problem is between half-way down and all the way down. Otherwise, it's in the top half. Step 3) Repeat the process on the problem half (for example, if it was in the top half in step 2, put it 1/4 of the way down). Continue this process until you've found your problem. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 23, 2008 Share Posted September 23, 2008 Or, add these 4 lines to the very top of the script: <?php ini_set('display_errors', 1); error_reporting(E_ALL); ?> Quote Link to comment Share on other sites More sharing options...
mallen Posted September 25, 2008 Author Share Posted September 25, 2008 Still not working. The code I have entered to try and display missing items keeps the form from being sent and it just displays "Please Complete the missing items" but not highlighting the missing item. The problem is I have my "if" statements conflicting. Its testing if the form is sent, it tests for wrong file type, and it tries to test for missing items. Its just too much for me to get a handle on. The first version of the form sent the mail ok. Its just checking for missing items that is tricking me. If anyone can help it would be great. I want to try to have this working by Friday. <?php ini_set('display_errors', 1); error_reporting(E_ALL); if ($_SERVER['REQUEST_METHOD']=="POST"){ // we'll begin by assigning the To address and message subject $to="me@you.com"; $subject="E-mail with attachment"; $expected = array('fromname','fromemail','filename'); $required = array('fromname'); $missing = array(); // get the sender's name and email address // we'll just plug them a variable to be used later $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">"; // generate a random string to be used as the boundary marker $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; // store the file information to variables for easier access $tmp_name = $_FILES['filename']['tmp_name']; $type = $_FILES['filename']['type']; $name = $_FILES['filename']['name']; $size = $_FILES['filename']['size']; $fromname = $_POST['fromname']; // here we'll hard code a text message // again, in reality, you'll normally get this from the form submission $message = "Here is your file: $name\n"; $message .= "From: $fromname\n\n"; // if the upload succeded, the file will exist if (file_exists($tmp_name)){ // check to make sure that it is an uploaded file and not a system file if(is_uploaded_file($tmp_name)){ // open the file for a binary read $file = fopen($tmp_name,'rb'); // read the file content into a variable $data = fread($file,filesize($tmp_name)); // close the file fclose($file); // now we encode it and split it into acceptable length lines $data = chunk_split(base64_encode($data)); } if (empty($missing)) { // now we'll build the message headers $headers = "From: $from\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\""; // next, we'll build the message body // note that we insert two dashes in front of the // MIME boundary when we use it $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // now we'll insert a boundary to indicate we're starting the attachment // we have to specify the content type, file name, and disposition as // an attachment, then add the file content and set another boundary to // indicate that the end of the file has been reached $message .= "--{$mime_boundary}\n" . "Content-Type: {$type};\n" . " name=\"{$name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; define ("MAX_SIZE","1000"); function file_extension($filename) { $path_info = pathinfo($filename); return $path_info['extension']; } $errors=0; $filename = stripslashes($_FILES['filename']['name']); $type = strtolower( file_extension( $filename)); if (($type != "jpg") && ($type != "jpeg") && ($type != "png") && ($type != "pdf") && ($type != "dwg") && ($type != "gif")) { echo 'Invalid file type! Only gif, jpg, pdf and DWG files are allowed to be uploaded</a>.'; echo $path_parts['extension'], "\n"; $errors=1; die (); } if ($_FILES['filename']['size'] >= 1048576) { echo 'You have exceeded the size limit of 1Mb!'; echo ' Actual size of attachment: '.$_FILES['filename']['size']; $errors=1; die (); } if ($_POST && isset($missing)){ ?> <p>Please Complete the missing items</p> <?php } elseif ($_POST && !$mailSent) { ?> <p>Sorry there was a problem</p> <?php } elseif ($_POST && $mailSent) { if (@mail($to, $subject, $message, $headers)) echo "Message was sent successfully."; } else { }}} ?> <p>Send an e-mail with an attachment:</p> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1"> <p>From name: <input type="text" name="fromname" <?php if (isset($missing) && in_array('fromname', $missing)) { ?>><p>Please enter your name</p><?php } ?> <p>From e-mail: <input type="text" name="fromemail"></p> <p>File: <input type="file" name="filename"></p> <p><input type="submit" name="Submit" value="Submit"></p> </form> Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 25, 2008 Share Posted September 25, 2008 Did you try the echo-troubleshooting I suggested? Try that and you will at least find the line that's causing the problem. Quote Link to comment Share on other sites More sharing options...
mallen Posted September 25, 2008 Author Share Posted September 25, 2008 Yes I did and it happens towards the bottom where all my "if" "else" statements are. I am so mixed up now because I have changed the code so many times. Quote Link to comment Share on other sites More sharing options...
F1Fan Posted September 25, 2008 Share Posted September 25, 2008 OK, in that case, keep troubleshooting until you determine the exact single line that is the problem. If needed, echo something unique on every other line and you'll at least find where the problem is very quickly. Quote Link to comment Share on other sites More sharing options...
mallen Posted September 26, 2008 Author Share Posted September 26, 2008 error: Undefined variable: mailSent <?php ini_set('display_errors', 1); error_reporting(E_ALL); if ($_SERVER['REQUEST_METHOD']=="POST"){ // we'll begin by assigning the To address and message subject $to="me@you.com"; $subject="E-mail with attachment"; $expected = array('fromname','fromemail','filename'); $required = array('fromname'); $missing = array(); // get the sender's name and email address // we'll just plug them a variable to be used later $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">"; // generate a random string to be used as the boundary marker $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; // store the file information to variables for easier access $tmp_name = $_FILES['filename']['tmp_name']; $type = $_FILES['filename']['type']; $name = $_FILES['filename']['name']; $size = $_FILES['filename']['size']; $fromname = $_POST['fromname']; // here we'll hard code a text message // again, in reality, you'll normally get this from the form submission $message = "Here is your file: $name\n"; $message .= "From: $fromname\n\n"; // if the upload succeded, the file will exist if (file_exists($tmp_name)){ // check to make sure that it is an uploaded file and not a system file if(is_uploaded_file($tmp_name)){ // open the file for a binary read $file = fopen($tmp_name,'rb'); // read the file content into a variable $data = fread($file,filesize($tmp_name)); // close the file fclose($file); // now we encode it and split it into acceptable length lines $data = chunk_split(base64_encode($data)); } if (empty($missing)) { // now we'll build the message headers $headers = "From: $from\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\""; // next, we'll build the message body // note that we insert two dashes in front of the // MIME boundary when we use it $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // now we'll insert a boundary to indicate we're starting the attachment // we have to specify the content type, file name, and disposition as // an attachment, then add the file content and set another boundary to // indicate that the end of the file has been reached $message .= "--{$mime_boundary}\n" . "Content-Type: {$type};\n" . " name=\"{$name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; define ("MAX_SIZE","1000"); function file_extension($filename) { $path_info = pathinfo($filename); return $path_info['extension']; } $errors=0; $filename = stripslashes($_FILES['filename']['name']); $type = strtolower( file_extension( $filename)); if (($type != "jpg") && ($type != "jpeg") && ($type != "png") && ($type != "pdf") && ($type != "dwg") && ($type != "gif")) { echo 'Invalid file type! Only gif, jpg, pdf and DWG files are allowed to be uploaded</a>.'; echo $path_parts['extension'], "\n"; $errors=1; die (); } if ($_FILES['filename']['size'] >= 1048576) { echo 'You have exceeded the size limit of 1Mb!'; echo ' Actual size of attachment: '.$_FILES['filename']['size']; $errors=1; die (); } } if ($_POST && isset($missing)){ echo "Please Complete the missing items" ; } if ($_POST && !$mailSent) { echo "Sorry there was a problem"; } if ($_POST && $mailSent) { if (@mail($to, $subject, $message, $headers)) echo "Message was sent successfully."; } } } ?> <p>Send an e-mail with an attachment:</p> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1"> <p>From name: <input type="text" name="fromname" <?php if (isset($missing) && in_array('fromname', $missing)) { ?>><p>Please enter your name</p><?php } ?> <p>From e-mail: <input type="text" name="fromemail"></p> <p>File: <input type="file" name="filename"></p> <p><input type="submit" name="Submit" value="Submit"></p> </form> Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 26, 2008 Share Posted September 26, 2008 <?php if ($_POST['submit']) { if (@mail($to, $subject, $message, $headers)) echo "Message was sent successfully."; } } ?> Quote Link to comment Share on other sites More sharing options...
redarrow Posted September 26, 2008 Share Posted September 26, 2008 try this but your code needs a lot of work....... <?php ini_set('display_errors', 1); error_reporting(E_ALL); if ($_SERVER['REQUEST_METHOD']=="POST"){ // we'll begin by assigning the To address and message subject $to="me@you.com"; $subject="E-mail with attachment"; $expected = array('fromname','fromemail','filename'); $required = array('fromname'); $missing = array(); // get the sender's name and email address // we'll just plug them a variable to be used later $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">"; // generate a random string to be used as the boundary marker $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; // store the file information to variables for easier access $tmp_name = $_FILES['filename']['tmp_name']; $type = $_FILES['filename']['type']; $name = $_FILES['filename']['name']; $size = $_FILES['filename']['size']; $fromname = $_POST['fromname']; // here we'll hard code a text message // again, in reality, you'll normally get this from the form submission $message = "Here is your file: $name\n"; $message .= "From: $fromname\n\n"; // if the upload succeded, the file will exist if (file_exists($tmp_name)){ // check to make sure that it is an uploaded file and not a system file if(is_uploaded_file($tmp_name)){ // open the file for a binary read $file = fopen($tmp_name,'rb'); // read the file content into a variable $data = fread($file,filesize($tmp_name)); // close the file fclose($file); // now we encode it and split it into acceptable length lines $data = chunk_split(base64_encode($data)); } if (empty($missing)) { // now we'll build the message headers $headers = "From: $from\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\""; // next, we'll build the message body // note that we insert two dashes in front of the // MIME boundary when we use it $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // now we'll insert a boundary to indicate we're starting the attachment // we have to specify the content type, file name, and disposition as // an attachment, then add the file content and set another boundary to // indicate that the end of the file has been reached $message .= "--{$mime_boundary}\n" . "Content-Type: {$type};\n" . " name=\"{$name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; define ("MAX_SIZE","1000"); function file_extension($filename) { $path_info = pathinfo($filename); return $path_info['extension']; } $errors=0; $filename = stripslashes($_FILES['filename']['name']); $type = strtolower( file_extension( $filename)); if (($type != "jpg") && ($type != "jpeg") && ($type != "png") && ($type != "pdf") && ($type != "dwg") && ($type != "gif")) { echo 'Invalid file type! Only gif, jpg, pdf and DWG files are allowed to be uploaded</a>.'; echo $path_parts['extension'], "\n"; $errors=1; die (); } if ($_FILES['filename']['size'] >= 1048576) { echo 'You have exceeded the size limit of 1Mb!'; echo ' Actual size of attachment: '.$_FILES['filename']['size']; $errors=1; die (); } } if ($_POST['summit']) { if (isset($_POST['missing'])){ echo "Please Complete the missing items" ; } if (mail($to, $subject, $message, $headers)) echo "Message was sent successfully."; }else{ echo "Sorry there was a problem"; } } } ?> <p>Send an e-mail with an attachment:</p> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1"> <p>From name: <input type="text" name="fromname" <?php if (isset($missing) && in_array('fromname', $missing)) { ?>><p>Please enter your name</p><?php } ?> <p>From e-mail: <input type="text" name="fromemail"></p> <p>File: <input type="file" name="filename"></p> <p><input type="submit" name="Submit" value="Submit"></p> </form> Quote Link to comment Share on other sites More sharing options...
mallen Posted September 26, 2008 Author Share Posted September 26, 2008 Thanks Redarow. Now it sends the email and attachment but it allows the name and email to be empty. It should prompt for missing items. ??? Quote Link to comment Share on other sites More sharing options...
mallen Posted September 27, 2008 Author Share Posted September 27, 2008 I just can't figure it out. Because I need to check for file type, size and empty fields its really making it difficult. Can anyone help? Quote Link to comment Share on other sites More sharing options...
mallen Posted September 27, 2008 Author Share Posted September 27, 2008 Ok I got it to prompt for missing fields. I added this: foreach ($_POST as $key => $value) { $temp = is_array($value) ? $value : trim($value); if (empty($temp) && in_array($key, $required)) { array_push($missing, $key); } elseif (in_array($key, $expected)) { ${$key} = $temp; } } It is sending the mail and saying it has been sent successfully. But it prompts for the missing fields. I don't want it to mail the form obviously if there is something missing. Below is what I got so far. <?php ini_set('display_errors', 1); error_reporting(E_ALL); if ($_SERVER['REQUEST_METHOD']=="POST"){ // we'll begin by assigning the To address and message subject $to="me@you.com"; $subject="E-mail with attachment"; $expected = array('fromname','fromemail','filename'); $required = array('fromname','fromemail','filename'); $missing = array(); // get the sender's name and email address // we'll just plug them a variable to be used later $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">"; // generate a random string to be used as the boundary marker $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; // store the file information to variables for easier access $tmp_name = $_FILES['filename']['tmp_name']; $type = $_FILES['filename']['type']; $name = $_FILES['filename']['name']; $size = $_FILES['filename']['size']; $fromname = $_POST['fromname']; // here we'll hard code a text message // again, in reality, you'll normally get this from the form submission $message = "Here is your file: $name\n"; $message .= "From: $fromname\n\n"; // if the upload succeded, the file will exist if (file_exists($tmp_name)){ // check to make sure that it is an uploaded file and not a system file if(is_uploaded_file($tmp_name)){ // open the file for a binary read $file = fopen($tmp_name,'rb'); // read the file content into a variable $data = fread($file,filesize($tmp_name)); // close the file fclose($file); // now we encode it and split it into acceptable length lines $data = chunk_split(base64_encode($data)); } if (empty($missing)) { // now we'll build the message headers $headers = "From: $from\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\""; // next, we'll build the message body // note that we insert two dashes in front of the // MIME boundary when we use it $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // now we'll insert a boundary to indicate we're starting the attachment // we have to specify the content type, file name, and disposition as // an attachment, then add the file content and set another boundary to // indicate that the end of the file has been reached $message .= "--{$mime_boundary}\n" . "Content-Type: {$type};\n" . " name=\"{$name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; define ("MAX_SIZE","1000"); function file_extension($filename) { $path_info = pathinfo($filename); return $path_info['extension']; } $errors=0; $filename = stripslashes($_FILES['filename']['name']); $type = strtolower( file_extension( $filename)); if (($type != "jpg") && ($type != "jpeg") && ($type != "png") && ($type != "pdf") && ($type != "dwg") && ($type != "gif")) { echo 'Invalid file type! Only gif, jpg, pdf and DWG files are allowed to be uploaded</a>.'; echo $path_parts['extension'], "\n"; $errors=1; die (); } if ($_FILES['filename']['size'] >= 1048576) { echo 'You have exceeded the size limit of 1Mb!'; echo ' Actual size of attachment: '.$_FILES['filename']['size']; $errors=1; die (); } } foreach ($_POST as $key => $value) { $temp = is_array($value) ? $value : trim($value); if (empty($temp) && in_array($key, $required)) { array_push($missing, $key); } elseif (in_array($key, $expected)) { ${$key} = $temp; } } if ($_POST['Submit']) { if (isset($_POST['$missing'])){ echo "Please Complete the missing items" ; } if (mail($to, $subject, $message, $headers)) echo "Message was sent successfully."; }else{ echo "Sorry there was a problem"; } } } ?> <p>Send an e-mail with an attachment:</p> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1"> <p>From name: <input type="text" name="fromname" <?php if (isset($missing) && in_array('fromname', $missing)) { ?>><p>Please enter your name</p><?php } ?> <p>From e-mail: <input type="text" name="fromemail" <?php if (isset($missing) && in_array('fromemail', $missing)) { ?>><p>Please enter your email</p><?php } ?> <p>File: <input type="file" name="filename"><?php if (isset($missing) && in_array('filename', $missing)) { ?>><p>Please upload a file.</p><?php } ?> <p><input type="submit" name="Submit" value="Submit"></p> </form> Quote Link to comment Share on other sites More sharing options...
mallen Posted October 12, 2008 Author Share Posted October 12, 2008 The form is not remembering the fields after I submit the form. If one is missing I want it to remember the other fields so you don't have to fill them in again. <?php ini_set('display_errors', 1); error_reporting(E_ALL); if ($_SERVER['REQUEST_METHOD']=="POST"){ // we'll begin by assigning the To address and message subject $to="emailaddress"; $subject="E-mail with attachment"; $expected = array('fromname','fromemail','filename'); $required = array('fromname','fromemail','filename'); $missing = array(); // get the sender's name and email address // we'll just plug them a variable to be used later $from = stripslashes($_POST['fromname'])."<".stripslashes($_POST['fromemail']).">"; // generate a random string to be used as the boundary marker $mime_boundary="==Multipart_Boundary_x".md5(mt_rand())."x"; // store the file information to variables for easier access $tmp_name = $_FILES['filename']['tmp_name']; $type = $_FILES['filename']['type']; $name = $_FILES['filename']['name']; $size = $_FILES['filename']['size']; $fromname = $_POST['fromname']; // here we'll hard code a text message // again, in reality, you'll normally get this from the form submission $message = "Here is your file: $name\n"; $message .= "From: $fromname\n\n"; // if the upload succeded, the file will exist if (file_exists($tmp_name)){ // check to make sure that it is an uploaded file and not a system file if(is_uploaded_file($tmp_name)){ // open the file for a binary read $file = fopen($tmp_name,'rb'); // read the file content into a variable $data = fread($file,filesize($tmp_name)); // close the file fclose($file); // now we encode it and split it into acceptable length lines $data = chunk_split(base64_encode($data)); } if (empty($missing)) { // now we'll build the message headers $headers = "From: $from\r\n" . "MIME-Version: 1.0\r\n" . "Content-Type: multipart/mixed;\r\n" . " boundary=\"{$mime_boundary}\""; // next, we'll build the message body // note that we insert two dashes in front of the // MIME boundary when we use it $message = "This is a multi-part message in MIME format.\n\n" . "--{$mime_boundary}\n" . "Content-Type: text/plain; charset=\"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" . $message . "\n\n"; // now we'll insert a boundary to indicate we're starting the attachment // we have to specify the content type, file name, and disposition as // an attachment, then add the file content and set another boundary to // indicate that the end of the file has been reached $message .= "--{$mime_boundary}\n" . "Content-Type: {$type};\n" . " name=\"{$name}\"\n" . //"Content-Disposition: attachment;\n" . //" filename=\"{$fileatt_name}\"\n" . "Content-Transfer-Encoding: base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n"; define ("MAX_SIZE","1000"); function file_extension($filename) { $path_info = pathinfo($filename); return $path_info['extension']; } $filename = stripslashes($_FILES['filename']['name']); $type = strtolower( file_extension( $filename)); if (($type != "jpg") && ($type != "jpeg") && ($type != "png") && ($type != "pdf") && ($type != "dwg") && ($type != "gif")) { echo 'Invalid file type! Only gif, jpg, pdf and DWG files are allowed to be uploaded</a>.'; echo $path_parts['extension'], "\n"; } if ($_FILES['filename']['size'] >= 1048576) { echo 'You have exceeded the size limit of 1Mb!'; echo ' Actual size of attachment: '.$_FILES['filename']['size']; } } foreach ($_POST as $key => $value) { $temp = is_array($value) ? $value : trim($value); if (empty($temp) && in_array($key, $required)) { array_push($missing, $key); } elseif (in_array($key, $expected)) { ${$key} = $temp; } } if (isset($_POST['Submit'])) { if ($missing) { echo "Please Complete the missing items" ; } else { (mail($to, $subject, $message, $headers)) ; echo "Message was sent successfully."; } } } } ?> <p>Send an e-mail with an attachment:</p> <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" enctype="multipart/form-data" name="form1"> <p>From name: <input type="text" name="fromname" <?php if (isset($missing) && in_array('fromname', $missing)) { ?>><p>Please enter your name</p><?php } ?> <p>From e-mail: <input type="text" name="fromemail" <?php if (isset($missing) && in_array('fromemail', $missing)) { ?>><p>Please enter your email</p><?php } ?> <p>File: <input type="file" name="filename" <?php if (isset($missing) && in_array('filename', $missing)) { ?>><p>Please upload a file</p><?php } ?> <p><input type="submit" name="Submit" value="Submit"> </form> </body> </html> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.