mallen
Members-
Posts
316 -
Joined
-
Last visited
Everything posted by mallen
-
Would creating an array be a better approach?
-
5,000-9,999 = .60 would be is they were ordering 5,000 CDs up to 9,999 the cost would be 60 cents each.
-
Here is the function I am trying to figure out the choice they made and assign a price. <?php // functions to generate labeling choice, packaging choice and printing choice. //assign a value for labeling choice price function get_cdLabel () { switch ($cdQuantity) { case 1: if $cdLabel == '1-10' blk_on_silv ='.0'; blk_on_wht ='.01'; col_on_silv ='.5'; col_on_white '.6'; if $cdLabel == '11-24' blk_on_silv ='.1'; blk_on_wht ='.02'; col_on_silv ='.03'; col_on_white '.04'; //and so on..... break; } //assign a value for packaging price function get_cdPackage () { } //assign a value for printing price function get_cdPrint () { } ?>
-
I am developing page that will allow the user to select quantities of CDs, choose printing choices such as color, jewel case, and packaging. I have never creating anything like this before so here is what I ahve so far. One page called estimate.php and another page called calculate.php <form name="form1" id="form1" method="post" action="calculate.php"> <table width="217" border="1" cellspacing="0" cellpadding="0"> <tr> <td width="114">CD</td> <td width="573"><select name="cdQuantity" id="cdQuantity"> <option value="8.00">1-10</option> <option value="7.00">11-24</option> <option value="6.00">25-49</option> <option value="5.00">50-99</option> <option value="3.00">100-249</option> <option value="2.00">250-499</option> <option value="1.50">500-999</option> <option value="1.15">1,000-2,499</option> <option value=".75">2,500-4,999</option> <option value=".60">5,000-9,999</option> <option>10,000 +</option> </select></td> </tr> <tr> <td>Labelling</td> <td><select name="cdLabel" id="cdLabel"> <option value="0">None</option> <option value=".10">Black on silver</option> <option value=".20">Black on white</option> <option value=".30">Color on Silver</option> <option value=".40">Color on white</option> </select></td> </tr> <tr> <td>Packaging</td> <td><select name="cdPackage" id="cdPackage"> <option value="0">Bulk</option> <option value=".03">Paper Envelope</option> <option value=".23">Clam Shell</option> <option value=".25">Slim Jewel Case</option> <option value=".38">Full Jewl Case</option> <option value=".75">DVD Box</option> </select></td> </tr> <tr> <td>Printing</td> <td><select name="cdPrint" id="cdPrint"> <option value="0">None</option> <option value="1.00">Slim Jewel Case Top</option> <option value=".85">Full Jewel case Top and Back</option> </select></td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td> </td> <td> </td> </tr> <tr> <td><input type="submit" name="Submit" value="Submit" /></td> <td> </td> </tr> </table> </form> And here is the calculate page: <?php // address error handling. ini_set ('display_errors',1); error_reporting (E_ALL & ~E_NOTICE); $cdQuantity = $POST['cdQuantity']; $cdLabel = $POST['cdLabel']; $cdPackage = $POST['cdPackage']; $cdPrint = $POST['cdPrint']; //Calculate total. $total = $cdQuantity * $price; //Print out results. print "You have selected to order $cdQuantity CDs at <br /> $<b>$cdPrice</b> Your estimated cost is $<b>$total;" ?> I assume I need to create separate functions that will assign a value to $price since price changes per quantity. Can someone lookit over and tell me if I am doing this right?
-
Thanks but what part did you change?
-
I figured out what line he was showing me to remove. I did that. Now if you leave the file field blank it displays "Invalid file type! Only gif, jpg, pdf and DWG files are allowed to be uploaded. Please Complete the missing items" I want it to send the form and not display anything if you leave that field empty.
-
//} //<----- REMOVE I don't see the comment line in my code. If I remove the } I get an error.
-
I have a simple form that has name, email and file to send. I removed the requirements for the file. Why does it still not send until you enter a file to send? <?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'); $required = array('fromname','fromemail'); $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" ; echo '<p>The following required fields have not been filled in:</p>'; echo '<ul>'; foreach($missing as $item) { echo "<li>$item</li>"; } echo '</ul>'; } 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)) { echo 'value="'.htmlentities($_POST['fromname']).'"';} ?> /> <p>From e-mail: <input type="text" name="fromemail" <?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['fromemail']).'"';} ?> /> <p>File: <input type="file" name="filename" /> <p><input type="submit" name="Submit" value="Submit"> </form>
-
Thanks I am going to close this thread and start a new one.
-
Thanks F1FAN. I had this before I saw your post. It works but doesn't remember the "filename" field. <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)) { echo 'value="'.htmlentities($_POST['fromname']).'"';} ?> <p>From e-mail: <input type="text" name="fromemail" <?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['fromemail']).'"';} ?> <p>File: <input type="file" name="filename" <?php if (isset($missing)) { echo 'value="'.htmlentities($_POST['filename']).'"';} ?> <p><input type="submit" name="Submit" value="Submit">
-
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>
-
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="[email protected]"; $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>
-
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?
-
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. ???
-
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="[email protected]"; $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>
-
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.
-
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="[email protected]"; $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>
-
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.
-
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. ???
-
After your suggestion I get no message. It lets me send the form.
-
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>
-
I got this: Parse error: syntax error, unexpected T_BOOLEAN_AND, expecting ',' or ')'
-
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="[email protected]"; $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 } ?>
-
Thanks MrAdam and everyone for all your help. I learned allot on this exercise. Now I have to build up the form with all the fields and required fields but the main portion I know is working.
-
Ok got it working I think. Now I need to set the file size properly. I want to set a limit of 1MB.