Contact Form with Attachment

Discussion in 'Programming' started by Adnan959, Oct 4, 2011.

  1. #1
    Hey guys,

    I'm having a bit of an issue setting up my contact form - the problem being the file attachment.

    This is the form I have (bare in mind, I'm using a CAPTCHA as well):

    
    
                           	<form action="quick_contact.php" method="post" name="form" id="form" onsubmit="return checkform(this);" >
                                Full Name <br />
                                <input name="name" class="mini_input" type="text" id="name" alt="Enter Full Name Here." />
                                Date of Birth <br />
                                <input name="dob" type="text" class="mini_input" id="dob" alt="Enter Date of Birth." />
                                Address <br />
                                <textarea name="address" cols="3" rows="3" class="mini_comments" id="address"></textarea>
                                Mobile<br />
                                <input name="mobile" class="mini_input" type="text" id="mobile" alt="Enter Full Name Here." />
                                E-Mail<br />
                                <input name="email" type="text" class="mini_input" id="email" alt="Enter Date of Birth." />                                               
                                Preferred Department<br />
    							<input name="department" type="text" class="mini_input" id="email" alt="Enter Date of Birth." />
                                Upload CV<br />
                                <input type="file" id="attachment" name="attached" class="file-upload" />
                                Please enter the correct validation code:<br />                                 
                                <div style="width:300px;">
                                <img style="margin:10px 10px 10px 0px; float:left;" src="CaptchaSecurityImages.php?width=100&amp;height=40&amp;characters=5" alt="Captcha Security Images" />
                                <input id="security_code" class="mini_input_02" name="security_code" style="margin:12px 0px 0px 0px;" type="text" />
                                </div>
                                <div class="clear"></div>
                                <input name="submit" type="submit" id="button" value="Submit" alt="Submit" class="button"/>
                                <input name="reset" type="reset" id="reset" value="Reset" alt="Reset" class="button"  />
    	                    </form>
    
    Code (markup):
    This is the PHP I have setup:
    
    <?php
       session_start();
       if(($_SESSION['security_code'] == $_POST['security_code']) && (!empty($_SESSION['security_code'])) ) {
          // Insert you code for processing the form here, e.g emailing the submission, entering it into a database. 
          unset($_SESSION['security_code']);
    	
    $message = "
    Full Name: {$_POST['name']} 
    Date of Birth: {$_POST['dobl']} 
    Address: {$_POST['address']} 
    Mobile: {$_POST['mobile']}
    Email: {$_POST['email']}
    Preferred Department: {$_POST['department']}
    
    ";
    		
    $recipient = "email@domain.com";
    $subject = "Contact Us Enquiry";
    $mailheader = "From: {$_POST['email']}";
    
    mail($recipient, $subject, $message, $mailheader) or die ("Failure");
    		
       } 
       
       else {
    		sleep(2); 
    		header("location:failure.html");
       }
         
    ?>
    
    
    Code (markup):

    My question is, how do I assign the variable in my PHP for the file attachment - being this one:
    <input type="file" id="attachment" name="attached" class="file-upload" />
    Code (markup):
    Thanks!
     
    Adnan959, Oct 4, 2011 IP
  2. mrmuggles

    mrmuggles Greenhorn

    Messages:
    58
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    18
    #2
    First, you must add enctype="multipart/form-data" in your <form> tag or it will never work.
    Then use something like this (from http://www.w3schools.com/php/php_file_upload.asp)

    <?php
    // To check if a file was uploaded :
    if ($_FILES["file"]["error"] > 0)
    {
    echo "Error: " . $_FILES["file"]["error"] . "<br />";
    }

    $filename = $_FILES["file"]["name"];
    $fullpath = $_FILES["file"]["tmp_name"];

    ?>
     
    mrmuggles, Oct 7, 2011 IP