Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Participant ,
Mar 08, 2018 Mar 08, 2018

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>

1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 08, 2018 Mar 08, 2018
Translate
Community Expert ,
Mar 08, 2018 Mar 08, 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
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2018 Mar 08, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2018 Mar 08, 2018

As an ACP can you redact my email from my original post? I forgot to edit that section of code before hitting post.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 08, 2018 Mar 08, 2018

kathleeno43607728  wrote

As an ACP can you redact my email from my original post? I forgot to edit that section of code before hitting post.

Done.

For this project, I suggest you use PHPMailer which supports attachments as well SMTP mail.

GitHub - PHPMailer/PHPMailer: The classic email sending library for PHP

Nancy

Nancy O'Shea— Product User, Community Expert & Moderator
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2018 Mar 08, 2018

I will attempt to do that. I haven't used GitHub before so I'll have a poke around and see what I can find. Thank you! Also, thank you for editing my first post!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 08, 2018 Mar 08, 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,

^ _ ^

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 08, 2018 Mar 08, 2018

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

V/r,

^ _ ^

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2018 Mar 08, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 08, 2018 Mar 08, 2018

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

HTH,

^ _ ^

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2018 Mar 08, 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>';

    }

}

?>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 08, 2018 Mar 08, 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,

^ _ ^

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2018 Mar 08, 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!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 08, 2018 Mar 08, 2018

I found six instances of fileToUpload.  Did you get ALL of them? 

^ _ ^

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2018 Mar 08, 2018

I did indeed. Ctrl+F is my friend in these cases.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 08, 2018 Mar 08, 2018

I just noticed.. I see where your PHP code uploads the file, but I don't see PHP code in the email section where the file is being attached to the email.

HTH,

^ _ ^

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2018 Mar 08, 2018

No doubt that's part of the problem. I've been scouring the web trying to find some sort of tutorial that makes sense. As I mentioned to Nancy in an earlier comment I did go to the PHP 5 File Upload section of w3schools and tried to use the content I found there to modify my original code. I didn't see anything about the file getting attached to anything and that's when I came here. Like I said, I figured there was something missing, haha.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 08, 2018 Mar 08, 2018

Ah, HA!  Okay, so you've got the upload part working (most likely.. have you checked the folder to see if the file makes it?)  Now, find an email tutorial and find out how to attach a file to an email. 

V/r,

^ _ ^

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2018 Mar 08, 2018

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 08, 2018 Mar 08, 2018
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 08, 2018 Mar 08, 2018

I Googled php email attachment and found many, many results, including SO threads.

HTH,

^ _ ^

UPDATE: Found another good one, I think.  https://www.texelate.co.uk/blog/send-an-email-attachment-with-php

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 08, 2018 Mar 08, 2018

I'll add it to my list to check out, thanks! Like I said, sometimes it's just about finding one that makes sense to you on the day. I think I've been working on this too long today and it's fried my brain, lol. Thanks for all your help!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Mar 09, 2018 Mar 09, 2018

perhaps I'm later on that one, and didn't read carrefuly all the previous mail... but did you trys PHPMailer ?

and you have plenty working exemple there

PHPMailer/send_file_upload.phps at master · PHPMailer/PHPMailer · GitHub

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 09, 2018 Mar 09, 2018
LATEST

Yep, I know how that feels. 

V/r,

^ _ ^

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines