0

i've got a basic php script for simple contact form with name, email and message inputs.
I wish to include few more options in it but don't know how to. I've searched but couldn't find all in one solution for. I would like to:

1. Send a copy to senders email
I would like to include input for sender to have an option to receive a copy of he's submit to he's email if he checkes that input in the form.

2. Upload a file
Also if possible in the same php script i wish to give a possibility for the sender to attach a file (preferably img extensions only) when submiting a form.

3. Thank you message
Not sure about this, but now i have a simple thank you message in echo when form is submited. If possible, i wish for this message to stay visible for 5 seconds then redirect to index.html.

Here is php for the form:

<?php

$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];

$formcontent="Name: $name \nEmail: $email \nMessage: $message";
$recipient = "test123@...";

$subject = "Contact";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");

echo
    "<div style='display: block; text-align: center;'>" .
        "<span style='font-size: 14px;'>You message has been sent!</span>" . 
        "<a href='index.html'>Go back</a>" . 
    "</div>";
?>

and demo jsfiddle of the form setup.

Thanks for any help.

g5wx
  • 700
  • 1
  • 10
  • 30
  • 3
    This is really 3 questions, all of which have been answered: [checking if a POST variable has been sent](http://stackoverflow.com/questions/3496971/check-if-post-exists) for sending an optional copy to sender, [uploading and attaching file to an email](http://stackoverflow.com/questions/826265/simple-php-form-attachment-to-email-code-golf), and finally, [redirecting a page after 5 seconds](http://stackoverflow.com/questions/6119451/page-redirect-after-certain-time-php). – Ryan Brodie Jan 11 '14 at 18:40
  • Thanks Brodie for the links, i've already checked 2 of them before, but i'm not familiar with php and combining these methods, that's why i've asked for a 3/1 solution if possible. – g5wx Jan 11 '14 at 18:43

1 Answers1

3

This is a global setup just to let you know how I would do this (if I wanted to do this on 1 page, but it's better to make functions, etc.)

EDIT: Please note also that I don't know if this works. Maybe there are errors but I have done this just to get you started.

    <?php 
            //Check if form submitted
            if (isset($_POST)) {

            //this all will run when form is submitted


            //First sanitize you data thats been posted
            $name = htmlentities($_POST['name'], ENT_QUOTES, 'UTF-8');
            $email = htmlentities($_POST['email'], ENT_QUOTES, 'UTF-8');
            $message = htmlentities($_POST['message'], ENT_QUOTES, 'UTF-8');

            //make a error array to hold errors
            $error = array();
            //check if fields are not empty you can also do other checks
            if (!empty($name) || !empty($email) || !empty($message))

            //here you could do extra checks.. like check if emai is really a email...
                if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
                    //email invalid
            array_push($error, 'email not valid');
                }
    //for image you could also do a if... 
            if(isset($_FILES)) {
    $uploads_dir = 'YOUR DIR'
                $name = $_FILES['image']['name'];
                $type = $_FILES['image']['type'];
                $size = $_FILES['image']['size'];
                $temp = $_FILES['image']['tmp_name'];
                $error = $_FILES['image']['error'];

                if ($error === 4) {
                    //No file was selected
                    return false;
                }
                else
                {
    //do your stuff with the image here... 
    move_uploaded_file($temp, "$uploads_dir/$temp");
    }

        ///you could do more ifs.. but if all is good then do the mail

        $subject = 'new contact form message';
        $headers = 'From: webmaster@example.com' . "\r\n" .
            'Reply-To: webmaster@example.com' . "\r\n" .
            'X-Mailer: PHP/' . phpversion();

        mail($email, $subject, $message, $headers);
        $success = "here the success message";
            } else {
            //some fields are empty
            array_push($error, 'some fields are empty');
            }
            ?>
            <!-- THE ENCTYPE IS NEEDED FOR IMAGES -->


            <form action="submit.php" name="contact-form" id="contact-form" method="post" enctype="multipart/form-data">
            <input name="name" placeholder="Name" type="text" id="name" required />
            <input name="email" placeholder="Email" type="email" id="email" required />
            <textarea name="message" placeholder="Message" id="message" required></textarea>
            <input type="file" id="upload-file" accept="image/*" />
            <div class="clear"></div>
            <input type="checkbox" id="copy" name="copy" />
            <label for="copy">Send a copy to my email</label>
            <div class="clear"></div>
            <input type="submit" value="Submit" form="contact-form" name="submit-form" />
            </form>

<?php
if (isset($success) && !empty($success)) {
//echo the success
echo $success
}
if (isset($error) && !empty($error)) {
//loop trough error
foreach ($error as $e) {
echo $e;
}
}
?>
Qantas 94 Heavy
  • 15,750
  • 31
  • 68
  • 83
Reza
  • 880
  • 1
  • 10
  • 29
  • hey thanks, i'll try it out and hope it works well :) – g5wx Jan 12 '14 at 09:03
  • well.. it has errors.. because I dit not look close at it,, but just use this as a guideline and when errors pop up let me know and we can see whats the problem.. and if this leaded u to the right way pls accept the awnser – Reza Jan 12 '14 at 16:06
  • hi rZaaaa, sorry for late reply - marked as accepted. Thanks! – g5wx Jan 20 '14 at 07:54