Solved

Form only displays thankyou if file uploaded?

Posted on 2012-03-20
8
172 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 21

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 21

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 21

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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

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 21

Expert Comment

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

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
windows 10 bash shell 4 37
PHP Sum Column in Table 3 28
Editing login page in zencart. 2 15
Post xml via PHP Soap Client 3 10
Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

760 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

17 Experts available now in Live!

Get 1:1 Help Now