Skip to main content
Inspiring
March 8, 2018
Answered

Trying to create a contact form using html and php with a file upload button

  • March 8, 2018
  • 4 replies
  • 1708 views

I've created a simple contact form for a website that I am working on, but I need to alter it slightly for another area of the website. I need to be able to add an upload section where the user can upload an image that will then be sent along with their comments. This is my current html and php for the basic form.

<form method="post" action="RMArequest.php">

      

    <label>Name</label>

    <input name="name" placeholder="Type Here">

          

    <label>Email</label>

    <input name="email" type="email" placeholder="Type Here">

          

    <label>Message</label>

    <textarea name="message" placeholder="Type Here"></textarea>

    

    <label>*What is 2+2? (Anti-spam)</label>

    <input name="human" placeholder="Type Here">

    <input id="submit" name="submit" type="submit" value="Submit">

     

</form>

_____

<!doctype html>

<html>

<head>

<meta charset="utf-8">

<title>Untitled Document</title>

</head>

<body>

<?php

    $name = $_POST['name'];

    $email = $_POST['email'];

    $message = $_POST['message'];

    $from = 'From: localhost';

    $to = 'name@email.com';

    $subject = 'RMA Request';

    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

    if ($_POST['submit']) {

    if ($name != '' && $email != '') {

        if ($human == '4') {

            if (mail ($to, $subject, $body, $from)) {

header('Location:thanks.html');

    } else {

        echo '<p>Something was entered incorrectly. Please try again.</p>';

    }

} else if ($_POST['submit'] && $human != '4') {

    echo '<p>You answered the anti-spam question incorrectly! Please go back and try again.</p>';

}

    } else {

        echo '<p>You need to fill in all required fields!!</p>';

    }

}

?>

</body>

</html>

This topic has been closed for replies.
Correct answer WolfShade

Would that it were so simple! I've been looking and all I can find is examples of how to make the form (which I already have) and how to upload the file (which I already have). Nothing about getting it into the email.


https://www.tutdepot.com/php-e-mail-attachment-script/

HTH,

^ _ ^

4 replies

Inspiring
March 8, 2018

Here's the code that I had tried to modify. My goal is to receive the uploaded image along with the user comment. I know I've just put something out of place or not done something server side. This is my first time trying to create a form that requires an upload section.

html:

<form method="post" action="RMArequest.php" enctype="multipart/form-data">

       

    <label>Name</label>

    <input name="name" placeholder="Type Here">

           

    <label>Email</label>

    <input name="email" type="email" placeholder="Type Here">

           

    <label>Message</label>

    <textarea name="message" placeholder="Type Here"></textarea>

 

     <label>Attach image:</label>

     <input type="file" name="imageUpload" id="imageUpload">

     

     <label>*What is 2+2? (Anti-spam)</label>

     <input name="human" placeholder="Type Here"> 

    <input id="submit" name="submit" type="submit" value="Submit">

</form>

____

.php

<?php

    $name = $_POST['name'];

    $email = $_POST['email'];

    $message = $_POST['message'];

    $from = 'From: localhost';

    $to = 'name@email.com';

    $subject = 'RMA Request';

    $human = $_POST['human'];

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";

$target_dir = "uploads/";

$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

$uploadOk = 1;

$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));

// Check if image file is a actual image or fake image

if(isset($_POST["submit"])) {

    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);

    if($check !== false) {

        echo "File is an image - " . $check["mime"] . ".";

        $uploadOk = 1;

    } else {

        echo "File is not an image.";

        $uploadOk = 0;

    }

}

// Check if file already exists

if (file_exists($target_file)) {

    echo "Sorry, file already exists.";

    $uploadOk = 0;

}

// Check file size

if ($_FILES["fileToUpload"]["size"] > 500000) {

    echo "Sorry, your file is too large.";

    $uploadOk = 0;

}

// Allow certain file formats

if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"

&& $imageFileType != "gif" ) {

    echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";

    $uploadOk = 0;

}

// Check if $uploadOk is set to 0 by an error

if ($uploadOk == 0) {

    echo "Sorry, your file was not uploaded.";

// if everything is ok, try to upload file

} else {

    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {

        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";

    } else {

        echo "Sorry, there was an error uploading your file.";

    }

}

    if ($_POST['submit']) {

    if ($name != '' && $email != '') {

        if ($human == '4') { 

            if (mail ($to, $subject, $body, $from)) {

header('Location:thanks.html');

    } else {

        echo '<p>Something was entered incorrectly. Please try again.</p>';

    }

} else if ($_POST['submit'] && $human != '4') {

    echo '<p>You answered the anti-spam question incorrectly! Please go back and try again.</p>';

}

    } else {

        echo '<p>You need to fill in all required fields!!</p>';

    }

}

?>

WolfShade
Brainiac
March 8, 2018

It's been a long time since I worked with PHP.  I have mostly tutorial knowledge.  However, I did notice that in the form, you named the file input "imageUpload", but in your PHP code, it looks like you're trying to access $_FILES["fileToUpload"].  I don't know if that is part of the issue, or not.

HTH,

^ _ ^

Inspiring
March 8, 2018

I've done about five different versions thus far so who knows. I think at this point I'm getting my various versions confused. I did try changing that around as you noted, but when the email came through there was still no attached image. So I know there's something I'm missing where it's not pulling all the information through. Thanks for catching that, though!

WolfShade
Brainiac
March 8, 2018

PS.. if your code samples include your actual email address, you might want to redact that.

V/r,

^ _ ^

Inspiring
March 8, 2018

Yeah I noticed that once I hit enter that I'd forgotten to fix that but I can't edit the post now. Ah well.

WolfShade
Brainiac
March 8, 2018

You can get the attention of a moderator or ACP and ask them to redact it, for you.

HTH,

^ _ ^

WolfShade
Brainiac
March 8, 2018

In order to upload files, you need to add an attribute to the form:

<form method="post" action="RMArequest.php" enctype="multipart/form-data">

You also need to add another input for the file to be selected.

<label>File</label>

<input type="file" name="thisFile" />

Adjust your PHP accordingly to accept the file, but be sure you are using server-side validation to make sure the file is what you are expecting.  Javascript is no good because the user can disable Javascript, voiding the validation.

HTH,

^ _ ^

Nancy OShea
Community Expert
March 8, 2018

To use PHP code, you must save the file with a .php extension.   See link below.

PHP 5 File Upload

Nancy O'Shea— Product User, Community Expert & Moderator
Inspiring
March 8, 2018

I actually went there first. I had issues trying to incorporate it into my code and the file did not come through to my email when I tested it. I do have have a separate html and php documents.  In my current html you can see the referenced .php file (RMArequest.php). That is where I have all of my current .php code. As I said, my current form works just fine. I'm having issues adding an upload area for images that will then send along with the user comment.