Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Oct 2012
    Location
    Toronto, Ontario, Canada
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts

    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!!

  • #2
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    335
    Thanks
    2
    Thanked 49 Times in 48 Posts
    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.

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,475
    Thanks
    63
    Thanked 537 Times in 524 Posts
    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.
    I can't really think of anything to write here now...

  • #4
    New Coder
    Join Date
    Oct 2012
    Location
    Toronto, Ontario, Canada
    Posts
    17
    Thanks
    3
    Thanked 0 Times in 0 Posts
    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!!

  • #5
    Regular Coder Redcoder's Avatar
    Join Date
    May 2012
    Location
    /dev/null
    Posts
    335
    Thanks
    2
    Thanked 49 Times in 48 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •