首页 文章

PHP联系表单修改[关闭]

提问于
浏览
0

我有一个简单的联系表格的基本PHP脚本与名称,电子邮件和消息输入 .
我希望在其中包含更多的选项,但是没有搜索到但是找不到所有的解决方案 . 我想要:

1. Send a copy to senders email
我希望为发件人提供输入,如果他检查表单中的输入,可以选择接收他的电子邮件副本 .

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

3. Thank you message
不确定这一点,但现在我在回传表单中提供了一条简单的谢谢消息 . 如果可能,我希望此消息保持可见状态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 回答

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

相关问题