Jump to content

[SOLVED] Required fields in form


mallen

Recommended Posts

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 } ?> 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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;
		}					
	}

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

  • 2 weeks later...

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>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.