Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 12-26-2012, 12:42 AM   PM User | #1
diggitydang11
New Coder

 
Join Date: Oct 2012
Location: Toronto, Ontario, Canada
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
diggitydang11 is an unknown quantity at this point
Adding File Upload functionality to my Contact Form

I have seen a few posts similar to my question, but was unable to successfully integrate it into my scenario. I anticipate that this is a short, easy fix (for someone with experience) but I'm not embarrassed to say that I'm a total newb.

Ok, so, I have a successful contact form with jquery validation on it. I am just looking to add a "File Upload" to the form, for people to attach their resumes. I was able to add the HTML portion for the file upload field/button, but need some help with integrating the PHP side... if anyone is in a position to help me, I would be very grateful (especially since I'm working on Christmas Day! ).

My form is as follows (I'll leave out the jquery validation portion, along with the CSS, for simplicity):

HTML Code:
<form id="form_id" class="AdvancedForm" action="application_form_submission.php" method="post" onsubmit="javascript:return validate('form_id','email');">
<table cellpadding="3" cellspacing="2">
<tr>
<td>
First Name:
</td>
<td>
<input type="text" name="firstname" id="firstname" />
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<input type="text" name="lastname" id="lastname" />
</td>
</tr>

<tr>
<td>
Primary Phone #:
</td>
<td>
<input type="text" name="mainphone" id="mainphone" />
</td>
</tr>
<tr>
<td>
Alternate Phone #:
</td>
<td>
<input type="text" name="secondphone" id="secondphone" />
</td>
</tr>

<tr>
<td>
Enter Email ID:
</td>
<td>
<input type="text" name="email" id="email" />
</td>
</tr>

<tr>
<td>
Preferred Contact Method:
</td>
<td>
<select name="preferred_contact" id="preferred_contact">
<option value="mainphone" selected="selected">Main Phone</option>
<option value="secondaryphone">Secondary Phone</option>
<option value="Email">Email</option>
</select>

</td>
</tr>

<tr>
<td>
Preferred Contact Time:<br />(if via phone)
</td>
<td>
<select name="preferred_contact_time" id="preferred_contact_time">
<option value="not_specified">Please select...</option>
<option value="morning">Morning</option>
<option value="afternoon">Afternoon</option>
<option value="evening">Evening</option>
</select>

</td>
</tr>

<tr>
<td>
Are you willing to relocate?
</td>
<td>
<span id="relocate" class="InputGroup">
<label for="relocate_1">
<input type="checkbox" name="relocate1" id="relocate1" value="yes" />Yes
</label>

<label for="relocate_2" style="padding-left: 10px;">
<input type="checkbox" name="relocate2" id="relocate2" value="no" />No
</label>
</span>
</td>
</tr>


<tr>
<td>
Please attach your resume:
</td>
<td><input name="upload_1" tabindex="9" type="file" onchange="attachmentChanged()" size="25" /></td>
</tr>

<tr>
<td>
</td>
<td>
<input class="button" type="submit" value="Submit" name="submit"/>
</td>
</tr>
</table>

PHP File (i.e. "application_form_submission.php):
<SCRIPT LANGUAGE="javascript">
self.location="form_submit.html"
</SCRIPT>

<?php
if(isset($_POST['submit'])) {

$to = "testing@hotmail.com";
$subject = "New Employment Application";
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$mainphone = $_POST['mainphone'];
$secondphone = $_POST['secondphone'];
$email = $_POST['email'];
$preferred_contact = $_POST['preferred_contact'];
$preferred_contact_time = $_POST['preferred_contact_time'];
$relocate1 = $_POST ['relocate1'];
$relocate2 = $_POST['relocate2'];


$body = "
$firstname $lastname has sent in an application for employment. Details are as follows: \n \n
From: $firstname $lastname\n
Main Phone: $mainphone\n
Alternate Phone: $secondphone\n
Email: $email\n
Preferred Contact Method: $preferred_contact\n
Preferred Contact Time: $preferred_contact_time\n
Relocate: $relocate1 $relocate2\n

\n";



echo "Use Browser Back Button to return";
mail($to, $subject, $body);


} else {

echo "Please fill out all information!";

}


?>

----------------------------

Thanks in advance for any help provided!!
diggitydang11 is offline   Reply With Quote
Old 12-26-2012, 12:27 PM   PM User | #2
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
You only need to learn of the $_FILES global array.

If you want to send the file as an attachment, you would be better of using PHP Mailer - it is made with this sort of thing in mind. Like this example. See this guy who wanted to send an image.

This is another file upload example.

Good luck mate. Don't hesitate to ask but you need to come up with some code so that we can help you or correct you where you haven't done it right.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Old 12-26-2012, 01:43 PM   PM User | #3
tangoforce
Senior Coder

 
tangoforce's Avatar
 
Join Date: Feb 2011
Location: Your Monitor
Posts: 3,497
Thanks: 44
Thanked 439 Times in 428 Posts
tangoforce will become famous soon enoughtangoforce will become famous soon enough
Quote:
Originally Posted by diggitydang11 View Post
if(isset($_POST['submit'])) {


Where do people keep getting this from? - Please read my signature. This is a very bad piece of code to use because Internet Explorer and play tricks on you with this leading your users to submit the form but your code not processing it.
__________________
Please wrap your code in [php] tags. It is a sticky topic and it HELPS us to HELP YOU!
TIP: Coding styles and $end errors :::::::::: TIP: Warning: Cannot modify header information - headers already sent :::::::::: TIP: Quotes / Parse error: syntax error, unexpected T_..
PHP Code:
//Please don't use this for your form processing:
if (isset($_POST['submit']))
//Internet explorer has a bug and does not always send the submit value. 
Explanation: The IE if(isset($_POST['submit'])) bug explained.
tangoforce is offline   Reply With Quote
Old 12-29-2012, 04:47 AM   PM User | #4
diggitydang11
New Coder

 
Join Date: Oct 2012
Location: Toronto, Ontario, Canada
Posts: 17
Thanks: 3
Thanked 0 Times in 0 Posts
diggitydang11 is an unknown quantity at this point
Quote:
Originally Posted by Redcoder View Post
You only need to learn of the $_FILES global array.

If you want to send the file as an attachment, you would be better of using PHP Mailer - it is made with this sort of thing in mind. Like this example. See this guy who wanted to send an image.

This is another file upload example.

Good luck mate. Don't hesitate to ask but you need to come up with some code so that we can help you or correct you where you haven't done it right.
Thanks Redcoder - Sorry for the delay... I appreciate the tips and I have been looking at all of the links you suggested, but I don't have much exposure at all to PHP, so I'm now embarrassed to say that I get lost reading all of those posts... I know it's written in beginner language, but I think you're giving me too much credit!!

I thought I did provide the code in my initial post, but I'll try putting it in again (I'm also new to this forum, so I don't really know how to use the code buttons - hopefully I did it right!):

For the portion of HTML Code, I have this:
Code:
<form id="form_id" class="AdvancedForm" action="application_form_submission.php" method="post" onsubmit="javascript:return validate('form_id','email');">
<table cellpadding="3" cellspacing="2">
<tr>
<td>
First Name:
</td>
<td>
<input type="text" name="firstname" id="firstname" />
</td>
</tr>
<tr>
<td>
Last Name:
</td>
<td>
<input type="text" name="lastname" id="lastname" />
</td>
</tr>

<tr>
<td>
Primary Phone #:
</td>
<td>
<input type="text" name="mainphone" id="mainphone" />
</td>
</tr>
<tr>
<td>
Alternate Phone #:
</td>
<td>
<input type="text" name="secondphone" id="secondphone" />
</td>
</tr>

<tr>
<td>
Enter Email ID:
</td>
<td>
<input type="text" name="email" id="email" />
</td>
</tr>

<tr>
<td>
Preferred Contact Method:
</td>
<td>
<select name="preferred_contact" id="preferred_contact">
<option value="mainphone" selected="selected">Main Phone</option>
<option value="secondaryphone">Secondary Phone</option>
<option value="Email">Email</option>
</select>

</td>
</tr>

<tr>
<td>
Preferred Contact Time:<br />(if via phone)
</td>
<td>
<select name="preferred_contact_time" id="preferred_contact_time">
<option value="not_specified">Please select...</option>
<option value="morning">Morning</option>
<option value="afternoon">Afternoon</option>
<option value="evening">Evening</option>
</select>

</td>
</tr>

<tr>
<td>
Are you willing to relocate?
</td>
<td>
<span id="relocate" class="InputGroup">
<label for="relocate_1">
<input type="checkbox" name="relocate1" id="relocate1" value="yes" />Yes
</label>

<label for="relocate_2" style="padding-left: 10px;">
<input type="checkbox" name="relocate2" id="relocate2" value="no" />No
</label>
</span>
</td>
</tr>


<tr>
<td>
Please attach your resume:
</td>
<td><input name="upload_1" tabindex="9" type="file" onchange="attachmentChanged()" size="25" /></td>
</tr>

<tr>
<td>
</td>
<td>
<input class="button" type="submit" value="Submit" name="submit"/>
</td>
</tr>
</table>
On the PHP side, I have this so far, but don't know how to get the file attached - is it possible to use this code and add to it, or am I going in a totally wrong direction?


PHP Code:
<SCRIPT LANGUAGE="javascript">
self.location="form_submit.html"
</SCRIPT>

<?php
if(isset($_POST['submit'])) {

$to "testing@hotmail.com";
$subject "New Employment Application";
$firstname $_POST['firstname'];
$lastname $_POST['lastname'];
$mainphone $_POST['mainphone'];
$secondphone $_POST['secondphone'];
$email $_POST['email'];
$preferred_contact $_POST['preferred_contact'];
$preferred_contact_time $_POST['preferred_contact_time'];
$relocate1 $_POST ['relocate1'];
$relocate2 $_POST['relocate2'];


$body "
$firstname $lastname has sent in an application for employment. Details are as follows: \n \n
From: $firstname $lastname\n
Main Phone: $mainphone\n
Alternate Phone: $secondphone\n
Email: $email\n
Preferred Contact Method: $preferred_contact\n
Preferred Contact Time: $preferred_contact_time\n
Relocate: $relocate1 $relocate2\n

\n"
;



echo 
"Use Browser Back Button to return";
mail($to$subject$body);


} else {

echo 
"Please fill out all information!";

}


?>

@tangoforce - I appreciate the advice, but again, my below-beginner skills fail me and I don't understand the fix you suggest.

I'm sorry if I sound like a total doorknob... when it comes to PHP, I actually am... I'm decent with HTML and CSS, but when it comes to the small amount of PHP that I use, I try and find the code online and work through it. I appreciate your patience!!
diggitydang11 is offline   Reply With Quote
Old 12-29-2012, 08:08 AM   PM User | #5
Redcoder
Regular Coder

 
Redcoder's Avatar
 
Join Date: May 2012
Location: /dev/couch
Posts: 309
Thanks: 2
Thanked 46 Times in 45 Posts
Redcoder has a little shameless behaviour in the past
Look into PHPMailer...it will take you sometime to get it but once you do, it will be very easy to use.

http://phpmailer.worxware.com/index.php?pg=tutorial

Getting a file from the $_FILES array is as easy as:

PHP Code:
$path "uploads/";

$path $path basename$_FILES['uploadedfile']['name']); 

if(
move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo 
"The upload of file ".  basename$_FILES['uploadedfile']['name']). 
    
" has succeeded";
} else{
    echo 
"Error uploading, please try again!";

That means that if the user uploaded a file called 'file1.pdf', it will be uploaded to uploads/file1.pdf . basename() gets the files actual name.

move_uploaded_file() moves the temporary file created by PHP to the folder that you define.

You really need to try and understand file uploads in PHP.
Go to:

http://www.tizag.com/phpT/fileupload.php

They have really simplified it there.

If you are the one who coded that code above, then this will be a piece of cake for you.
__________________
For professional Hosting and Web design.....


NetEssentials.co.uk
Redcoder is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 07:29 PM.


Advertisement
Log in to turn off these ads.