简体   繁体   English

电邮表格问题

[英]Email form problems

I have three email forms, which is allowing the user to send a message to one of my three emails, without having to write their own email. 我有三个电子邮件表单,允许用户向我的三封电子邮件之一发送邮件,而无需编写自己的电子邮件。 The forms are working and is sending the email, the problem is that the information such as the email sender and the message for the email, only works on my "support" form but not for the two other forms ("business","other"). 表格正在运行并且正在发送电子邮件,问题是电子邮件发件人和电子邮件信息等信息仅适用于我的“支持”表单,但不适用于其他两种表单(“商家”,“其他”) “)。 I am not sure what exactly is wrong. 我不确定到底出了什么问题。

Important note, the reason there is three forms like I have done it, is because I have made three buttons called "business" "support" "other" and then when you click one of the buttons, the specific form appears. 重要提示,有三种形式的原因,比如我已经完成了,因为我已经制作了三个按钮,称为“业务”“支持”“其他”,然后当您单击其中一个按钮时,将显示特定的表单。

html script with forms inside. 里面有表单的html脚本。

<!-- SUPPORT CONTACT FORM START-->
            <div class="contactSupportButton"><input type="image" src="supportContactButtonNew.png" id="contactSupportBut" alt="contact support button" style="height: 40px; width: 100px" onClick="showSupForm()"/>
                    <div id="contactSupportForm">

                        <form action="supFormSend.php" method="post" id="contactForms">

                        <div id="nameLabelForm">
                            <label for="name">Name:</label><br>
                            <input type="text" id="nameInput" name="nameInput"/>
                        </div>

                        <div id="emailLabelForm">
                            <label for="mail">E-mail:</label><br>
                            <input type="email" id="mailInput" name="mailInput"/>
                        </div>

                        <div id="messageLabelForm">
                            <label for="msg">Support Message:</label><br>
                            <textarea id="messageInput" name="messageInput"></textarea>
                        </div>

                        <div class="submitEmailButton">
                            <button type="submit" id="submitButton">Send message</button>
                        </div>

                        </form>

                    </div>
                </div>

            <!-- SUPPORT CONTACT FORM ENDING-->

            <!-- BUSINESS CONTACT FORM START-->
            <div class="contactBusinessButton"><input type="image" src="businessContactButtonNew.png" id="contactBusinessBut" alt="contact business button" style="height: 40px; width: 110px" onClick="showBusForm()"/>
                    <div id="contactBusinessForm"> 

                        <form action="busFormSend.php" method="post" id="contactForms">

                        <div id="nameLabelForm">
                            <label for="name">Name:</label><br>
                            <input type="text" id="nameInput"/>
                        </div>

                        <div id="emailLabelForm">
                            <label for="mail">E-mail:</label><br>
                            <input type="email" id="mailInput" />
                        </div>

                        <div id="messageLabelForm">
                            <label for="msg">Business Message:</label><br>
                            <textarea id="messageInput"></textarea>
                        </div>

                        <div class="submitEmailButton">
                            <button type="submit" id="submitButton">Send message</button>
                        </div>

                        </form>

                    </div>
                </div>
            <!-- BUSINESS CONTACT FORM ENDING-->

            <!-- OTHER CONTACT FORM START-->
            <div class="contactOtherButton"><input type="image" src="otherContactButtonNew.png" id="contactOtherBut" alt="contact other button" style="height: 40px; width: 110px" onClick="showOtherForm()"/>
                    <div id="contactOtherForm">

                        <form action="otherFormSend.php" method="post" id="contactForms">

                        <div id="nameLabelForm">
                            <label for="name">Name:</label><br>
                            <input type="text" id="nameInput"/>
                        </div>

                        <div id="emailLabelForm">
                            <label for="mail">E-mail:</label><br>
                            <input type="email" id="mailInput" />
                        </div>

                        <div id="messageLabelForm">
                            <label for="msg">Other Message:</label><br>
                            <textarea id="messageInput"></textarea>
                        </div>

                        <div class="submitEmailButton">
                            <button type="submit" id="submitButton">Send message</button>
                        </div>

                        </form>

                    </div>
                </div>
            <!-- OTHER CONTACT FORM ENDING-->

php script's which sends the message to my emails. php脚本将消息发送到我的电子邮件。

Support form (supFormSend.php): 支持表格(supFormSend.php):

<?php
$field_name = $_POST['nameInput'];
$field_email = $_POST['mailInput'];
$field_message = $_POST['messageInput'];

$mail_to = 'support@myemail.com';
$subject = 'Message regarding support from '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
        window.location = 'contact.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send an email to support@myemail.com');
        window.location = 'contact.html';
    </script>
<?php
}
header('Location: index.html');
exit;
?>

Business form (busFormSend.php): 业务表单(busFormSend.php):

<?php
$field_name = $_POST['nameInput'];
$field_email = $_POST['mailInput'];
$field_message = $_POST['messageInput'];

$mail_to = 'business@myemail.com';
$subject = 'Message regarding business from '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
        window.location = 'contact.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send an email to business@myemail.com');
        window.location = 'contact.html';
    </script>
<?php
}
header('Location: index.html');
exit;
?>

Other form (otherFormSend.php): 其他形式(otherFormSend.php):

<?php
$field_name = $_POST['nameInput'];
$field_email = $_POST['mailInput'];
$field_message = $_POST['messageInput'];

$mail_to = 'other@myemail.com';
$subject = 'Message regarding other from '.$field_name;

$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message;

$headers = 'From: '.$field_email."\r\n";
$headers .= 'Reply-To: '.$field_email."\r\n";

$mail_status = mail($mail_to, $subject, $body_message, $headers);

if ($mail_status) { ?>
    <script language="javascript" type="text/javascript">
        alert('Thank you for the message. We will contact you shortly.');
        window.location = 'contact.html';
    </script>
<?php
}
else { ?>
    <script language="javascript" type="text/javascript">
        alert('Message failed. Please, send an email to other@myemail.com');
        window.location = 'contact.html';
    </script>
<?php
}
header('Location: index.html');
exit;
?>

HTML forms rely on the name attribute to post the data to the backend (in this case PHP). HTML表单依赖于name属性将数据发布到后端(在本例中为PHP)。

When you reference the variable $_POST['nameInput'] in PHP, it's value will take the value of the field in the form where the name attribute is 'nameInput'. 当您在PHP中引用变量$_POST['nameInput']时,它的值将采用name属性为'nameInput'的表单中的字段值。 For example, <input type="text" id="nameInput" name="nameInput"/> . 例如, <input type="text" id="nameInput" name="nameInput"/>

Your form #contactSupportForm is working because the name attributes are set for the the values you are referencing in your PHP. 您的表单#contactSupportForm正在运行,因为为您在PHP中引用的值设置了名称属性。

To get the other two forms working add the name attribute to all your inputs in the HTML (and make sure the value of the name attribute matches what you reference in your PHP) just like you did in the contactSupportForm. 要使其他两个表单正常工作,请将name属性添加到HTML中的所有输入(并确保name属性的值与您在PHP中引用的值相匹配),就像在contactSupportForm中一样。

Jon Stirling gave the answer to your problem. 乔恩斯特林给出了你的问题的答案。 Additionally, if the forms are on one page, you should change the id values of all the different html elements. 此外,如果表单在一个页面上,您应该更改所有不同html元素的id值。 An id should be unique on a page. 一个id在页面上应该是唯一的。 It doesn't have to give you an error, but it's wrong and if you start using jquery/javascript it might lead to problems in the future. 它不必给你一个错误,但它是错误的,如果你开始使用jquery / javascript它可能会导致将来出现问题。

For example you have three times id="nameInput" 例如,您有三次id =“nameInput”

Since you set the From: to be from your visitor, your webserver will originate an email with a From: for just about any address out there. 由于您将From:设置为来自访问者,因此您的网络服务器将发送一封电子邮件,其中包含From:几乎任何地址。

This is a problem as the anti-spam measures in place will prevent messages from getting delivered if your email server or service checks SPF records. 这是一个问题,因为如果您的电子邮件服务器或服务检查SPF记录,现有的反垃圾邮件措施将阻止邮件传递。

Better to send the email from a noreply@example.com and have those that would want to react use the reply-to: header ... (not that hard). 最好从noreply@example.com发送电子邮件,让那些想要反应的人使用reply-to:header ...(不是很难)。

The main difference: now it's your domain's SPF record that matters and more importantly, the originating domain won't start to complain about bounces they get for mail they didn't originate. 主要区别:现在这是你的域名的SPF记录很重要,更重要的是,原始域名不会开始抱怨他们没有发起的邮件反弹。

Use "name" on all your forms to identify input fields, ie not "id" 在所有表单上使用“name”来标识输入字段,即不是“id”

correct in first form: 以第一种形式更正:

<input type="text" id="nameInput" name="nameInput"/>

incorrect in other forms: 其他形式不正确:

<input type="email" id="mailInput" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM