【问题标题】:Sending an email using php使用 php 发送电子邮件
【发布时间】:2014-10-02 08:40:58
【问题描述】:

给定一个标准的 html 表单,如下所示;

<form method="post" action=<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>>
                            <div class="row half">
                                <div class="6u"><input type="text" placeholder="Name" name="name" /></div>
                                <div class="6u"><input type="text" placeholder="Email" name="email" /></div>
                            </div>
                            <div class="row half">
                                <div class="12u"><textarea name="message" placeholder="Message" name="message"></textarea></div>
                            </div>
                            <div class="row">
                                <div class="12u">
                                    <ul class="actions">
                                        <li><input type="submit" class="button" value="Send Message" name="submit" /></li>
                                        <li><input type="reset" class="button alt" value="Clear Form" name="clear"/></li>
                                    </ul>
                                </div>
                            </div>
                        </form>

还有下面的php

if(isset($_POST['submit'])){
    $to = "enquiries@appcloudkent.com"; // this is your Email address
    $from = $_POST['email']; // this is the sender's Email address
    $name = $_POST['name'];
    $subject = "Website Enquiry";
    $message = $name . " wrote the following:" . "\n\n" . $_POST['message'];
    $headers = "From:" . $from;
    $headers2 = "From:" . $to;

    if(isValidString($from) && isValidString($name) && isValidString($message)){
        mail($to,$subject,$message,$headers);
    }
}

可以发送电子邮件,但是这种方法会导致一些问题。因为我的表单在页面底部,当点击发送时页面被重定向回来,焦点回到页面顶部。

提供足够的用户反馈以让用户知道电子邮件已发送的正确方法是什么,是否可以导航回页面并自动滚动到底部 - 允许我将发送按钮更改为绿色或其他东西?

如果做不到这一点,有没有更好的方法来做到这一点?

谢谢

【问题讨论】:

  • 如果我理解您只想添加反馈。你也许可以做类似(只是一个例子)if($mail) { header('Location: form.php?status=success'); } 的事情,在你的 form.php 中你可以添加$response = $_GET['status']; if($response=='success') { echo "Mail has been sent"; }

标签: php html email hci


【解决方案1】:

在表单前添加锚链接。 &lt;a id="anchorName"&gt;&lt;/a&gt;

将您的表单发布到锚点。

&lt;form method="post" action="&lt;?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?&gt;#anchorName"&gt;

【讨论】:

    【解决方案2】:

    表单操作应该指向页面本身(就像在您的示例中所做的那样),以便您可以显示来自验证的错误消息(就像您没有那样)。

    如果表单不在页面顶部,您可以添加锚点:

    <h2><a name="theForm">The form</a></h2>
    <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>#theForm">
    

    如果表单处理正确(=邮件已发送),最好重定向到另一个页面,显示成功消息。所以按 F5 不会再次提交表单(并发送另一封邮件)。

    if (mail($to,$subject,$message,$headers)) {
        // redirect to page showing success message and exit script
    } else {
        // redirect to page showing error message and exit script
    }
    

    我通常会再次重定向到同一页面,但附加一个参数 ($_SERVER["PHP_SELF"].'?mail_success=1'),然后在模板中我决定是显示成功消息、错误消息还是表单:

    if (isset($_REQUEST['mail_success'])) {
        // show success message
    } elseif (isset($_REQUEST['mail_error'])) {
        // show error message, technical issues, try again bla bla
    } else {
        // show the form (including validation errors if necessary)
    }
    

    【讨论】:

      【解决方案3】:

      您可以使用 XMLHttpRequest 提交表单,并取消按提交的默认操作。 jQuery 的 .post() 和 event.preventDefault() 方法在这方面特别擅长。

      【讨论】:

        猜你喜欢
        • 2013-07-13
        • 1970-01-01
        • 2011-09-16
        • 1970-01-01
        • 2013-09-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多