PHP联系表单修改

时间:2014-01-11 18:36:13

标签: javascript php html forms contact

我有一个基本的PHP脚本,用于简单的联系表格,包括姓名,电子邮件和消息输入 我希望在其中包含更多选项,但不知道如何。我搜索过但无法找到所有解决方案。我想:

1。将副本发送给发件人电子邮件
我希望为发件人提供输入,如果他在表单中检查输入,可以选择接收他提交给他的电子邮件的副本。

2。上传文件
如果可能的话,在同一个php脚本中,我希望发送者在提交表单时附加文件(最好只是img扩展名)。

3。谢谢你留言
不确定这一点,但现在我在回传表单中提供了一条简单的谢谢消息。如果可能,我希望此消息保持可见状态5秒钟,然后重定向到index.html。

以下是表单的php:

<?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>";
?>

和表单设置的演示jsfiddle

感谢您的帮助。

1 个答案:

答案 0 :(得分:3)

这是一个全局设置,只是为了让你知道我将如何做到这一点(如果我想在1页上做这个,但最好是制作函数等)。

编辑:请注意,我不知道这是否有效。也许有错误,但我这样做只是为了让你开始。

    <?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;
}
}
?>