【问题标题】:Form sending email on landing/refresh of website在登陆/刷新网站时发送电子邮件的表格
【发布时间】:2013-10-08 10:39:12
【问题描述】:

每次我访问我的网站或刷新它时,我的表单都会向我发送一封空白电子邮件(字段数据为空)。

我查看了一些提到“发布/重定向/获取”的帖子,但这让我感到困惑而不是帮助,我不确定这是否与我的确切问题有关。

这是我在网上找到的一个基本的响应式表单,必须找到 PHP 才能实际发送表单数据。 (根据我的需要进行更改)

我希望只是我在某个地方遗漏了一个错误,但我似乎找不到它。

任何帮助将不胜感激。提前致谢。

这是基本形式(减去 css):

PHP:

<?php
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$message = $_POST['message'];
$formcontent=" From: $name \n Phone: $phone \n Email: $email \n Message: $message";
$recipient = "MY EMAIL HERE";
$subject = "Portfolio Contact Form";
$mailheader = "From: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
echo "Thank You!";
?>

HTML:

<form id="contact-form" action="/" method="post">
            <h3>Say Hello!</h3>
            <h4>Fill in the form below, and I'll get back to you as soon as i can. Thanks!</h4>
            <div>
                <label>
                    <span>Name: (required)</span>
                    <input placeholder="Please enter your name" type="text" name="name" tabindex="1" required autofocus>
                </label>
            </div>
            <div>
                <label>
                    <span>Email: (required)</span>
                    <input placeholder="Please enter your email address" type="text" name="email" tabindex="2" required>
                </label>
            </div>
            <div>
                <label>
                    <span>Telephone: (required)</span>
                    <input placeholder="Please enter your number" type="text" name="phone" tabindex="3" required>
                </label>
            </div>
            <div>
                <label>
                    <span>Message: (required)</span>
                    <textarea placeholder="Include all the details you can" type="text" name="message" tabindex="5" required></textarea>
                </label>
            </div>
            <div>
                <button name="submit" type="submit" id="contact-submit">Send Email</button>
            </div>
        </form>

【问题讨论】:

    标签: php html forms email sendmail


    【解决方案1】:

    使用isset

    <?php
    if(isset($_POST['submit']))
    {
    $name = $_POST['name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $message = $_POST['message'];
    $formcontent=" From: $name \n Phone: $phone \n Email: $email \n Message: $message";
    $recipient = "MY EMAIL HERE";
    $subject = "Portfolio Contact Form";
    $mailheader = "From: $email \r\n";
    mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
    echo "Thank You!";
    }
    ?>
    

    只有当它被任何..提交时,它才会post您的电子邮件。

    【讨论】:

    • 非常感谢:您在 'if(isset($_POST['submit'])' 的末尾缺少了一个额外的 ')',但它奏效了。感谢您的帮助。干杯。
    【解决方案2】:

    我已经在 stackoverflow 链接code link 中为其他人发布了这个 这是我的代码,试试吧,希望对你有帮助

    <?PHP
    $email = $_POST["emailaddress"];
    $to = "you@youremail.com";
    $subject = "New Email Address for Mailing List";
    $headers = "From: $email\n";
    $message = "A visitor to your site has sent the following email address to be added to your mailing list.\n
    
    Email Address: $email";
    $user = "$email";
    $usersubject = "Thank You";
    $userheaders = "From: you@youremailaddress.com\n";
    $usermessage = "Thank you for subscribing to our mailing list.";
    mail($to,$subject,$message,$headers);
    mail($user,$usersubject,$usermessage,$userheaders);
    ?>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-01
      • 2021-01-26
      • 2012-01-12
      • 2014-09-14
      • 2013-12-29
      • 2011-11-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多