Solved

Form only displays thankyou if file uploaded?

Posted on 2012-03-20
8
179 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

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

 
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
PHP query / monitor data from Telnet to MySQL 7 50
php mysql check email already in database. (second check) 7 67
PHP Echo with auto submit 8 29
Wordpress Pagination 1 29
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

829 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