Solved

Form only displays thankyou if file uploaded?

Posted on 2012-03-20
8
173 Views
Last Modified: 2012-03-21
I have a form working fine but the form only submits and the user only sees a 'thankyou' message when they upload a file through a fileupload field that's on the form.

How get I get this form to submit and the message to display regardless of whether they upload a file or not?

<?php
   if ($_SERVER['REQUEST_METHOD']=="POST"){

   // we'll begin by assigning the To address and message subject
   $to="test@medical-electives.net";

   $subject="CV Submission";

   // 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 = "This CV has been submitted\n\n
   Name: $_POST[fromname]\n
   Surname: $_POST[surname]\n
   Date of Birth: $_POST[dob]\n
   Address: $_POST[address]\n
   Email: $_POST[fromemail]\n
   Telephone: $_POST[tel]\n
   Skype: $_POST[skype]\n
   Occupation or Program?:$_POST[occupation]\n
   Friend?: $_POST[friend]\n\n\n\n
   
   Accomodation Preferences:$_POST[accompref]\n
   Do you smoke?:$_POST[smoke]\n
   Dietry Requirments?:$_POST[dietry]\n\n\n\n
   
   Preferred Clinical Placement:$_POST[clinic]\n
   Preferred Medical Facility:$_POST[medical]\n
   Start Date:$_POST[start]\n
   End:$_POST[end]\n
   Project to complete?:$_POST[project]\n\n\n\n
   
   Other Information:$_POST[howhear]\n
   Preferred Medical Facility:$_POST[questions]\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";

      // now we just send the message
      if (@mail($to, $subject, $message, $headers))
         echo "Thanks for sending your details. We will be in touch soon.";
      else
         echo "Failed to send";
   }
} else {
?>

Open in new window

0
Comment
Question by:BrighteyesDesign
  • 5
  • 3
8 Comments
 
LVL 22

Expert Comment

by:Kim Walker
ID: 37745008
The form must be submitted by the browser in order for the PHP script to do anything at all. If the form won't submit, there must be something in the HTML or Javascript embedded in the HTML preventing it from submitting. Please post the HTML or a link to the page in question.
0
 

Author Comment

by:BrighteyesDesign
ID: 37746223
Hi, the form submits fine if the user uploads a file.

It's only when they don't attach a file. The php must be saying somewhere...if user uploads file submit form if not do not?

Here's the form live...http://www.medical-electives.net/bookingform.php
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 37746830
I was able to submit the form without choosing a file. You must be talking about something else. The form is submitted when you click the Book button. When I clicked the Book button, the form was submitted and a blank booking form displayed. At what point is the process failing?
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 37746874
I just looked at your PHP. I assume this PHP is processing what is submitted by the form. It ends prematurely. The last line is:
} else {

Open in new window

Try adding the following between lines 102 and 103:
echo 'No file was uploaded.';
}

Open in new window

so that the last four lines are:
} else {
echo 'No file was uploaded.';
}
?>

Open in new window

0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:BrighteyesDesign
ID: 37746944
Thanks for that, the form looks like it prematurely because the whole form is wrapped in the php code. Should have mentioned that sorry!

So, after the code I originally posted you get all of the form fields and it then ends with....      

    <td bgcolor="#FFFFFF"><input type="image" src="images/submit.jpg" border="0" name="submit"alt="book" /></td>
                                </tr>
                                </table></td>
                            </tr>
                          </table>
                          <p><br />
          
              <br />
          </p>
</form>
            <?php } ?>

Open in new window



Would I add that code you suggest here somewhere? I have tried but am probably placing it in the wrong place.
0
 
LVL 22

Expert Comment

by:Kim Walker
ID: 37746963
That makes sense. But it changes things. I'll have to look a little closer.
0
 
LVL 22

Accepted Solution

by:
Kim Walker earned 500 total points
ID: 37747012
I've made some minor changes to your code to send the email regardless of whether a file is uploaded. I can't test it, though, so keep a copy of your original. Also, I don't have a lot of experience with file attachments and I've tried to keep the code that attaches the file inside the if statement. Hopefully I've got it in the right place.
<?php
   if ($_SERVER['REQUEST_METHOD']=="POST"){

   // we'll begin by assigning the To address and message subject
   $to="test@medical-electives.net";

   $subject="CV Submission";

   // 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 = "This CV has been submitted\n\n
   Name: $_POST[fromname]\n
   Surname: $_POST[surname]\n
   Date of Birth: $_POST[dob]\n
   Address: $_POST[address]\n
   Email: $_POST[fromemail]\n
   Telephone: $_POST[tel]\n
   Skype: $_POST[skype]\n
   Occupation or Program?:$_POST[occupation]\n
   Friend?: $_POST[friend]\n\n\n\n
   
   Accomodation Preferences:$_POST[accompref]\n
   Do you smoke?:$_POST[smoke]\n
   Dietry Requirments?:$_POST[dietry]\n\n\n\n
   
   Preferred Clinical Placement:$_POST[clinic]\n
   Preferred Medical Facility:$_POST[medical]\n
   Start Date:$_POST[start]\n
   End:$_POST[end]\n
   Project to complete?:$_POST[project]\n\n\n\n
   
   Other Information:$_POST[howhear]\n
   Preferred Medical Facility:$_POST[questions]\n";

   $headers = "From: $from\r\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 .=  "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 "Thanks for sending your details. We will be in touch soon.";
   } else {
      echo "Failed to send";
   }
} else {
?>

Open in new window

0
 

Author Closing Comment

by:BrighteyesDesign
ID: 37747168
Perfect, thanks a million for your help xmediaman!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

937 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

Need Help in Real-Time?

Connect with top rated Experts

4 Experts available now in Live!

Get 1:1 Help Now