?
Solved

PHP checking for blank fields

Posted on 2009-02-23
6
Medium Priority
?
519 Views
Last Modified: 2012-05-06
I have a php mail form that sends an attachment.  I want to implement some kind of php code that will check all of the forms in the field and will return an error if either the name, email, or messages field is left blank.  I tried to implement a java script to do this.  It initially worked but failed on the final release.  Is there a way to implement this into my code attached in either php or html?  
   <?php
 if ($_SERVER['REQUEST_METHOD']=="POST"){
 $path_info = pathinfo($_FILES['filename']['name']);
        if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
                die ("can not upload this type of file");
        }
 
 
   // we'll begin by assigning the To address and message subject
   $to="reddyfire@gmail.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'];
   $nimi =  $_REQUEST['nimi'];
   $toimi =  $_REQUEST['toimi'];
   $Puhelinnumero =  $_REQUEST['Puhelinnumero'];
 
 
   // here we'll hard code a text message
   // again, in reality, you'll normally get this from the form submission
   $message = stripslashes($_POST['message'])."$message";
 
   // 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";
 
      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
         echo "Submission sent. We will review your submission and reply back with a decision if you followed the directions.";
      else
         echo "Failed to send";
	  }
	  }
 
?>
 
	
<p>Submit your stories:</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>Story:<br> <textarea name="message" rows="15" cols="40"></textarea></p>
   <p>File: <input type="file" name="filename"></p>
   <p><input type="submit" name="Submit" value="Submit"></p>
</form>

Open in new window

0
Comment
Question by:reddyfire
  • 3
  • 3
6 Comments
 
LVL 6

Expert Comment

by:psychic_zero
ID: 23718202

<?php
 if ($_SERVER['REQUEST_METHOD']=="POST"){
 
   $fromname = trim(stripslashes($_POST['fromname']));
   $fromemail = trim(stripslashes($_POST['fromemail']));
   $message = trim(stripslashes($_POST['message']));
 
   $status = 1;  // if all OK
   $errormsg = "";
 
   if ($fromname == "")
   {
      $status = 0; // blank field
      $errormsg .= "Please input From Name <br>"
   }
 
   if ($fromemail == "")
   {
      $status = 0; 
      $errormsg .= "Please input From Email <br>"
   }
 
   if ($message == "")
   {
      $status = 0; 
      $errormsg .= "Please input Message <br>"
   }
 
if ($status == 1) // if all field not blank, start process
{
 $path_info = pathinfo($_FILES['filename']['name']);
        if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
                die ("can not upload this type of file");
        }
 
 
   // we'll begin by assigning the To address and message subject
   $to="reddyfire@gmail.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'];
   $nimi =  $_REQUEST['nimi'];
   $toimi =  $_REQUEST['toimi'];
   $Puhelinnumero =  $_REQUEST['Puhelinnumero'];
 
 
   // here we'll hard code a text message
   // again, in reality, you'll normally get this from the form submission
   $message = stripslashes($_POST['message'])."$message";
 
   // 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";
 
      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
         echo "Submission sent. We will review your submission and reply back with a decision if you followed the directions.";
      else
         echo "Failed to send";
          }
          }
 }
 else
 {
    echo($errormsg);
 }
?>
        
<p>Submit your stories:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
   enctype="multipart/form-data" name="form1">
   <p>From name: <input type="text" name="fromname" value="<?php echo($fromname); ?>"></p>
   <p>From e-mail: <input type="text" name="fromemail" value="<?php echo($fromemail); ?>"></p>
   <p>Story:<br> <textarea name="message" rows="15" cols="40">value="<?php echo($message); ?>"</textarea></p>
   <p>File: <input type="file" name="filename"></p>
   <p><input type="submit" name="Submit" value="Submit"></p>
</form>

Open in new window

0
 
LVL 6

Expert Comment

by:psychic_zero
ID: 23718219
Replace line 131 with this:

   <p>Story:<br> <textarea name="message" rows="15" cols="40"><?php echo($message); ?></textarea></p>

Open in new window

0
 

Author Comment

by:reddyfire
ID: 23718229
I tried that exact code you sent me and got the following error:

Parse error: syntax error, unexpected '}' in /home/guanajuato/public_html/Guanajuato/sendmail2.php on line 13
<?php
 if ($_SERVER['REQUEST_METHOD']=="POST"){
 
   $fromname = trim(stripslashes($_POST['fromname']));
   $fromemail = trim(stripslashes($_POST['fromemail']));
   $message = trim(stripslashes($_POST['message']));
 
   $status = 1;  // if all OK
   $errormsg = "";
 
   if ($fromname == "")
   {
      $status = 0; // blank field
      $errormsg .= "Please input From Name <br>"
   }
 
   if ($fromemail == "")
   {
      $status = 0; 
      $errormsg .= "Please input From Email <br>"
   }
 
   if ($message == "")
   {
      $status = 0; 
      $errormsg .= "Please input Message <br>"
   }
 
if ($status == 1) // if all field not blank, start process
{
 $path_info = pathinfo($_FILES['filename']['name']);
        if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
                die ("can not upload this type of file");
        }
 
 
   // we'll begin by assigning the To address and message subject
   $to="reddyfire@gmail.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'];
   $nimi =  $_REQUEST['nimi'];
   $toimi =  $_REQUEST['toimi'];
   $Puhelinnumero =  $_REQUEST['Puhelinnumero'];
 
 
   // here we'll hard code a text message
   // again, in reality, you'll normally get this from the form submission
   $message = stripslashes($_POST['message'])."$message";
 
   // 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";
 
      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
         echo "Submission sent. We will review your submission and reply back with a decision if you followed the directions.";
      else
         echo "Failed to send";
          }
          }
 }
 else
 {
    echo($errormsg);
 }
?>
        
<p>Submit your stories:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
   enctype="multipart/form-data" name="form1">
   <p>From name: <input type="text" name="fromname" value="<?php echo($fromname); ?>"></p>
   <p>From e-mail: <input type="text" name="fromemail" value="<?php echo($fromemail); ?>"></p>
   <p>Story:<br> <textarea name="message" rows="15" cols="40">value="<?php echo($message); ?>"</textarea></p>
   <p>File: <input type="file" name="filename"></p>
   <p><input type="submit" name="Submit" value="Submit"></p>
</form>

Open in new window

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 6

Accepted Solution

by:
psychic_zero earned 1000 total points
ID: 23718253
Try again
<?php
 if ($_SERVER['REQUEST_METHOD']=="POST"){
 
   $fromname = trim(stripslashes($_POST['fromname']));
   $fromemail = trim(stripslashes($_POST['fromemail']));
   $message = trim(stripslashes($_POST['message']));
 
   $status = 1;  // if all OK
   $errormsg = "";
 
   if ($fromname == "")
   {
      $status = 0; // blank field
      $errormsg .= "Please input From Name <br>";
   }
 
   if ($fromemail == "")
   {
      $status = 0; 
      $errormsg .= "Please input From Email <br>";
   }
 
   if ($message == "")
   {
      $status = 0; 
      $errormsg .= "Please input Message <br>";
   }
 
if ($status == 1) // if all field not blank, start process
{
 $path_info = pathinfo($_FILES['filename']['name']);
        if (!in_array(strtolower($path_info['extension']),array('pdf','zip'))) {
                die ("can not upload this type of file");
        }
 
 
   // we'll begin by assigning the To address and message subject
   $to="reddyfire@gmail.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'];
   $nimi =  $_REQUEST['nimi'];
   $toimi =  $_REQUEST['toimi'];
   $Puhelinnumero =  $_REQUEST['Puhelinnumero'];
 
 
   // here we'll hard code a text message
   // again, in reality, you'll normally get this from the form submission
   $message = stripslashes($_POST['message'])."$message";
 
   // 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";
 
      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
         echo "Submission sent. We will review your submission and reply back with a decision if you followed the directions.";
      else
         echo "Failed to send";
          }
   }
   else
   {
      echo($errormsg);
   }
}
?>
        
<p>Submit your stories:</p>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" 
   enctype="multipart/form-data" name="form1">
   <p>From name: <input type="text" name="fromname" value="<?php echo($fromname); ?>"></p>
   <p>From e-mail: <input type="text" name="fromemail" value="<?php echo($fromemail); ?>"></p>
   <p>Story:<br> <textarea name="message" rows="15" cols="40"><?php echo($message); ?></textarea></p>
   <p>File: <input type="file" name="filename"></p>
   <p><input type="submit" name="Submit" value="Submit"></p>
</form>

Open in new window

0
 

Author Closing Comment

by:reddyfire
ID: 31550450
Great thank you for your help.  You saved hours of headaches and pain.
0
 

Author Comment

by:reddyfire
ID: 23719484
The code works and does what I want it to do but when the form is submitted I get the following code appearing in the message forum after submission. And when I get the email the comments in the message box are repeated twice.  What can you do to fix this issue?
This is a multi-part message in MIME format.
 
--==Multipart_Boundary_x5670852611a602c651ba22a823c4a06cx
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit
 
here is a test story here is a test story
 
--==Multipart_Boundary_x5670852611a602c651ba22a823c4a06cx
Content-Type: application/pdf;
 name="Crime Scene Log Sheet (Editable Form).pdf"
Content-Transfer-Encoding: base64
 
JVBERi0xLjMNJeLjz9MNCjYgMCBvYmoNPDwgDS9MaW5lYXJpemVkIDEgDS9PIDkgDS9IIFsgMTI4
MyAyMzcgXSANL0wgODY3MjMgDS9FIDg1ODA2IA0vTiAxIA0vVCA4NjQ4NiANPj4gDWVuZG9iag0g
ICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month13 days, 9 hours left to enroll

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question