Form only displays thankyou if file uploaded?

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

BrighteyesDesignAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kim WalkerWeb Programmer/TechnicianCommented:
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.
BrighteyesDesignAuthor Commented:
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
Kim WalkerWeb Programmer/TechnicianCommented:
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?
CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Kim WalkerWeb Programmer/TechnicianCommented:
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

BrighteyesDesignAuthor Commented:
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.
Kim WalkerWeb Programmer/TechnicianCommented:
That makes sense. But it changes things. I'll have to look a little closer.
Kim WalkerWeb Programmer/TechnicianCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
BrighteyesDesignAuthor Commented:
Perfect, thanks a million for your help xmediaman!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.