Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

<!-- 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.

Support form (supFormSend.php):

<?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;
?>

Business form (busFormSend.php):

<?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;
?>

Other form (otherFormSend.php):

<?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;
?>
like image 739
Fulgut98 Avatar asked May 02 '15 15:05

Fulgut98


1 Answers

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.

like image 118
Katie Avatar answered Oct 05 '22 23:10

Katie