Jump to content

require form fields


mallen

Recommended Posts

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>

Link to comment
https://forums.phpfreaks.com/topic/128464-require-form-fields/
Share on other sites

it check to see if the file was uploaded

see the 2 //<--- REMOVE comment lines

 

<?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)){ //<--- REMOVE

      // 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.";
    }
  }
//}  //<----- REMOVE

}

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

Link to comment
https://forums.phpfreaks.com/topic/128464-require-form-fields/#findComment-665723
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/128464-require-form-fields/#findComment-666729
Share on other sites

i have quick rewrite

<?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();
   
   $HasFile = false;

   // 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
   $HasFile = ($_FILES['filename']['size']>0);
   if($HasFile)
   {
   $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";
   }else{
	$message = "No File attached\n";

   }
   $message .= "From: $from\n\n";
   // if the upload succeded, the file will exist
   //if (file_exists($tmp_name)){ //<--- REMOVE

      // check to make sure that it is an uploaded file and not a system file
      if($HasFile && 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 .= ($HasFile)?"--{$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"); 

if($HasFile)
{
	$filename = stripslashes($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.";
    }
  }
//}  //<----- REMOVE

}

function file_extension($filename) {
    $path_info = pathinfo($filename);
    return $path_info['extension'];
}
?>
<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>

Link to comment
https://forums.phpfreaks.com/topic/128464-require-form-fields/#findComment-666937
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.