PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 6th, 2009, 12:13 PM
eludic eludic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 13 eludic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 m 27 sec
Reputation Power: 0
PHP form mail with attachment - blank page

I am coding a PHP form mail with attachment for a client of mine. The form seems to have run into an issue. After the form is submitted with attachment it displays a blank page. However if I submit the form without any attachment then it works fine. Here is the HTML and PHP code that we are using:

Form code:
Code:
<form action="career.php" method="post" enctype="multipart/form-data">

<?php if(isset($errormsg)){
?>

<div class="errormsg"><strong>ERROR: </strong><?php echo $errormsg; ?></div>
<div class="clear"></div>
<br />
<?php
}
?>

<fieldset><legend><span class="fieldheader">Personal Details</span></legend>
<table align='left' width='100%' cellpadding='2' cellspacing='2' border="0">
<tr>
	<td width='50%' align='left' class='row1'><strong>Full Name: <span class="required">*</span></strong></td>
	<td width='50%' align='left' class='row1'><input type='text' name='name'  /></td>
</tr>
<tr>
	<td width='50%' align='left'><strong>Email: <span class="required">*</span></strong></td>
	<td width='50%' align='left'><input type='text' name='email'  /></td>
</tr>

</table>
</fieldset>

<br />

<fieldset>
<table align='left' width='100%' cellpadding='2' cellspacing='2' border="0">

<tr>
	<td width='50%' align='left'><strong>Upload Resume: </strong><br />
	Allowed file types - MS Word & PDF files
	</td>
	<td width='50%' align='left'><input type="file" name="strresume" /></td>
</tr>
</table>
</fieldset>


<table align='left' width='100%' cellpadding='2' cellspacing='2' border="0">
<tr>
	<td width='50%' align='right'><input type='submit' value='Submit' class='bluebutton' name='submit' /></td>
	<td width='50%' align='left'><input type='reset' value='Reset' class='redbutton' name='reset' /></td>
</tr>
</table>

</form>


PHP Code:
Code:
<?php 
if(isset($_POST['submit']) AND $_POST['submit'] == "Submit"){
	
	print_r($_FILES);
	
	// IF FILE IS SET TO BE UPLOADED THEN GET ITS INFORMATION
	$strresume_name = $_FILES['strresume']['name']; 
    $strresume_type = $_FILES['strresume']['type']; 
    $strresume_size = $_FILES['strresume']['size']; 
    $strresume_temp = $_FILES['strresume']['tmp_name']; 
	
	// CHECK IF ALL REQUIRED FIELDS ARE FILLED
	if(empty($_POST['name']) OR empty($_POST['email']) ){

		$errormsg = "You forgot to fill one or more required fields";
		include("form.inc");
}

	// RUN CHECKS TO CONFIRM IF THE UPLOADED FILE IS ACCEPTABLE FORMAT
	elseif(!empty($strresume_type)){

			if(($strresume_type != "application/pdf") AND ($strresume_type != "application/msword") AND ($strresume_type != "application/vnd.openxmlformats")){
			// FILE IS IN NON-ACCEPTABLE FORMAT THROW ERROR
			$errormsg = "The submitted resume is not in MS Word or PDF format. Please try again";
			include("form.inc");		
				
		}
	}else{
	echo "Send mail";	
	}
}else{

// DISPLAY THE FORM
include("form.inc"); 
}
?>


The above code works fine if I submit the form without attachment. It does throw an error when the filetype is not Word or PDF. But if I attach a PDF file the page comes out blank when it should display the message "Send mail".

The print_r($_FILES); too displays the result just fine.

What am I doing wrong? I spent all day on this and am completely lost right now.

Last edited by eludic : November 6th, 2009 at 12:53 PM. Reason: Better titling

Reply With Quote
  #2  
Old November 6th, 2009, 05:35 PM
msteudel's Avatar
msteudel msteudel is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 609 msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 15 h 53 m 45 sec
Reputation Power: 8
Is error_reporting on?

Try this instead:

PHP Code:
// RUN CHECKS TO CONFIRM IF THE UPLOADED FILE IS ACCEPTABLE FORMAT
    
elseif(!empty($strresume_type) AND strresume_type != "application/pdf" AND $strresume_type != "application/msword" AND $strresume_type != "application/vnd.openxmlformats"){

            
            
// FILE IS IN NON-ACCEPTABLE FORMAT THROW ERROR
            
$errormsg "The submitted resume is not in MS Word or PDF format. Please try again";
            include(
"form.inc");        
            
    }else{
        echo 
"Send mail";    
    } 

Reply With Quote
  #3  
Old November 6th, 2009, 05:41 PM
msteudel's Avatar
msteudel msteudel is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 609 msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 15 h 53 m 45 sec
Reputation Power: 8
Basically what happened was that you uploaded a file, which means $strresume_type is true. But if it's a correct file type, there's nothing left for the code to do ... lots of nested if statements can catch you up very easily.

If I were writing this, I might change things a bit like so (in my mind it makes it clearer AND if your user has multiple validation errors you tell them all at once, versus making them submit it multiple times). You also reduce duplicate code a little bit:

PHP Code:
// CHECK IF ALL REQUIRED FIELDS ARE FILLED
    
if(empty($_POST['name']) OR empty($_POST['email']) ){
        
$errormsg[] = "You forgot to fill one or more required fields";
    }

    
// RUN CHECKS TO CONFIRM IF THE UPLOADED FILE IS ACCEPTABLE FORMAT
    
if(!empty($strresume_type) AND strresume_type != "application/pdf" AND $strresume_type != "application/msword" AND $strresume_type != "application/vnd.openxmlformats"){
        
// FILE IS IN NON-ACCEPTABLE FORMAT THROW ERROR
        
$errormsg[] = "The submitted resume is not in MS Word or PDF format. Please try again";            
    }
    
    if( 
count$errormsg ) > ) {
        
implode$errormsg'<br />' );
        include 
'form.inc';
    }
    else {
        echo 
"Send mail";
    } 

Reply With Quote
  #4  
Old November 6th, 2009, 07:01 PM
eludic eludic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 13 eludic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 m 27 sec
Reputation Power: 0
Thank You so much for replying. I get what you are tryin to suggest. I will implement the changes you have provide and shall see if that does the trick. Thanks again!

Reply With Quote
  #5  
Old November 7th, 2009, 05:55 AM
eludic eludic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 13 eludic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 m 27 sec
Reputation Power: 0
That did the trick. Thank You so much!

Quote:
If I were writing this, I might change things a bit like so (in my mind it makes it clearer AND if your user has multiple validation errors you tell them all at once, versus making them submit it multiple times). You also reduce duplicate code a little bit

You were right. I made changes to my code, thanks again.

Reply With Quote
  #6  
Old November 7th, 2009, 07:27 AM
eludic eludic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 13 eludic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 m 27 sec
Reputation Power: 0
I have one final problem. When the form is submitted using punctuations like apostrophe then PHP cuts the submitted data for instance if the submission is L'Global then PHP only accepts L stripping the rest. I have tried addslashes() but that too does not work. That comes out with the output L\\ while 'Global is yet dropped. How do I fix this?

Reply With Quote
  #7  
Old November 7th, 2009, 11:14 AM
msteudel's Avatar
msteudel msteudel is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 609 msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 15 h 53 m 45 sec
Reputation Power: 8
Where is the last point in your code that the name is still intact?

Reply With Quote
  #8  
Old November 7th, 2009, 11:40 AM
eludic eludic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 13 eludic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 m 27 sec
Reputation Power: 0
Thanks for your help. I managed to fix it.

Reply With Quote
  #9  
Old November 7th, 2009, 01:47 PM
msteudel's Avatar
msteudel msteudel is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 609 msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 3 Days 15 h 53 m 45 sec
Reputation Power: 8
Nice job, remember to post back your solutions in case someone else has similar issues.

Reply With Quote
  #10  
Old November 7th, 2009, 03:30 PM
eludic eludic is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2007
Posts: 13 eludic User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 58 m 27 sec
Reputation Power: 0
True. The solution was that I did not set stripslashes correctly due to which the issue was happening. By going through the code I managed to fix it.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > Email with attachment form - blank page


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
For more Enterprise Application Development news, visit eWeek