【问题标题】:PHP contact form modification [closed]PHP联系表格修改[关闭]
【发布时间】:2014-01-11 18:36:13
【问题描述】:

我有一个基本的 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

感谢您的帮助。

【问题讨论】:

标签: javascript php html forms contact


【解决方案1】:

这是一个全局设置,只是为了让您知道我将如何执行此操作(如果我想在 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;
}
}
?>

【讨论】:

  • 嘿,谢谢,我会试试看,希望它运作良好:)
  • 好吧..它有错误..因为我没有仔细看它,但只是将此作为指导,当错误弹出时让我知道,我们可以看到问题所在..如果这导致你走上正确的道路,请接受遮阳篷
  • 嗨 rZaaaa,抱歉回复晚了 - 标记为已接受。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多