Reputation: 323
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.
<!-- 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
$field_name = $_POST['nameInput'];
$field_email = $_POST['mailInput'];
$field_message = $_POST['messageInput'];
$mail_to = '[email protected]';
$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 [email protected]');
window.location = 'contact.html';
</script>
<?php
}
header('Location: index.html');
exit;
?>
<?php
$field_name = $_POST['nameInput'];
$field_email = $_POST['mailInput'];
$field_message = $_POST['messageInput'];
$mail_to = '[email protected]';
$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 [email protected]');
window.location = 'contact.html';
</script>
<?php
}
header('Location: index.html');
exit;
?>
<?php
$field_name = $_POST['nameInput'];
$field_email = $_POST['mailInput'];
$field_message = $_POST['messageInput'];
$mail_to = '[email protected]';
$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 [email protected]');
window.location = 'contact.html';
</script>
<?php
}
header('Location: index.html');
exit;
?>
Upvotes: 10
Views: 299
Reputation: 2308
Use "name" on all your forms to identify input fields, ie not "id"
correct in first form:
<input type="text" id="nameInput" name="nameInput"/>
incorrect in other forms:
<input type="email" id="mailInput" />
Upvotes: 0
Reputation:
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.
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.
Better to send the email from a [email protected] and have those that would want to react use the reply-to: header ... (not that hard).
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.
Upvotes: 0
Reputation: 66
HTML forms rely on the name attribute to post the data to the backend (in this case 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'. For example,
<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.
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.
Upvotes: 2
Reputation: 826
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. An id should be unique on a page. 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.
For example you have three times id="nameInput"
Upvotes: 0