Solved

Form only displays thankyou if file uploaded?

Posted on 2012-03-20
8
186 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

 
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
 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

696 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