Contact thank you page - php

Currently My contact form goes to another page when submitted with an alert that says thankyou for contacting us but, what I want it to do is say thankyou for contact us as a pop up or text on the screen, anywhere to say thank you but just on the same page as the contact form
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = ' dsaas#asadsd.asdasd';
$subject = 'Message from a site visitor '.$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_page.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to gordon#template-help.com');
window.location = 'contact_page.html';
</script>
<?php
}
?>
<form action="contact.php" style="height:auto;" class="form" method="post">
<h1>Your name</h1>
<input type="text" class="form textbox" name="cf_name"><br>
<h1>Your e-mail</h1>
<input type="text" class="form textbox" name="cf_email"><br>
<h1>Message</h1>
<textarea class="form textbox" name="cf_message"></textarea><br>
<input type="submit" value="Send">
I have tried replacing the if statement to
if ($mail_status) { ?>
<div><h1>thank you for submitting the contcat form</h1> </div>
<?php
}
But this still went to another page

You need to validate your data before sending.
if(isset($_POST['submit']){
$field_name = isset($_POST['cf_name'])? $_POST['cf_name']:'';
$field_email = isset($_POST['cf_email'])? $_POST['cf_email']:'';
$field_message = isset($_POST['cf_message'])? $_POST['cf_message']:'';
if(empty($field_name){
$error = 'Field Name is blank';
}elseif(empty($field_email) || filter_var($field_email, FILTER_VALIDATE_EMAIL)){
$error = 'Email is invalid';
}elseif(empty($field_message)){
$error = 'Message is blank';
}else{
$mail_to = ' dsaas#asadsd.asdasd';
$subject = 'Message from a site visitor '.$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.');
document.getElementById("myForm").reset();//reset the form
</script>
<?php }else{ ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to gordon#template-help.com');
//document.getElementById("myForm").reset();//Reset the form
</script>
<?php }
}
}
Since you don't want to go to another, you can post in the same page by specifying the action like below or you don't necessarily need to do.
<form action="<?php echo $_SERVER['PHP_SELF'];?>"
style="height:auto;" class="form" id="myForm" method="post">
<h1>Your name</h1>
<input type="text" class="form textbox" name="cf_name"><br>
<h1>Your e-mail</h1>
<input type="text" class="form textbox" name="cf_email"><br>
<h1>Message</h1>
<textarea class="form textbox" name="cf_message"></textarea><br>
<input type="submit" value="Send">
<div class="error">
<?php if(!empty($error)){ echo $error; } ?>
</div>
Hope this may help.

Related

Contact Form Not Working - php file is returning the "else" failed response

I have tried looking in similar questions posted here, but cannot find an answer (got about 1900 questions popping up).
I have a contact form on a page at a site I am creating on the webpage hosting site
https://www.one.com/en/
Here is the form on the page contact/contact.shtml
<form action="contact/mail.php" method="post"><br>
Your name<br>
<input type="text" name="cf_name"><br>
Your e-mail<br>
<input type="text" name="cf_email"><br>
Message<br>
<textarea name="cf_message"></textarea><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
Here is the content of the contact/mail.php file
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'clashton#gmail.com';
$subject = ' Contact Form '.$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.shtml';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to clashton#gmail.com');
window.location = 'contact.shtml';
</script>
<?php
}
?>
When I try to send a message a window pops up containing the error message "Message failed. Please, send an email to clashton#gmail.com". In other words the "else" action is happening.
If it seems that this question has a trivial answer, please keep in mind that I am completely new at this.
Thanks for any help.

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 = '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):
<?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):
<?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).
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.
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"
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 noreply#example.com 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.
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" />

I've written a newsletter subscriprion in php based on a script i had for a mail form, but $_Post is not working

I only get empty mails from the server...
This is the code:
<?php
$field_email = $_POST['sub-email'];
$mail_to = 'alex.dapunt#gmail.com';
$subject = 'Newsletter Subscriprion '.$field_email;
$body_message .= 'E-mail: '.$field_email."\n";
$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("We'll notify you as soon the new website is online.");
window.location = 'index.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Subscription failed. Please, send an email to alex.dapunt#gmail.com');
// window.location = 'contact_page.html';
</script>
<?php
}
?>
and this the html:
<form action="send.php" method="post" id="newsletter" data-scroll-reveal="wait 0.5s, then enter bottom and move 20px over 1s">
<div class="lj-block-center">
<input type="text" value="mail" name="sub-email" id="mail" placeholder="Enter your e-mail">
<input type="submit" value="Submit" name="submit">
</div>
</form>
I really don't get why it is not working. But I have to say that I'm a newbie in PHP
The problem was in this line
$body_message .= 'E-mail: '.$field_email."\n";
you used concatenation instead of assigning the value.
This is your code with my modifications
<?php
$field_email = $_POST['sub-email'];
$mail_to = 'alex.dapunt#gmail.com';
$subject = 'Newsletter Subscriprion '.$field_email;
$body_message = 'E-mail: '.$field_email."\n";
$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) {
echo '
<script language="javascript" type="text/javascript">
alert("We\'ll notify you as soon the new website is online.");
window.location = \'index.html\';
</script>';
}
else {
echo '<script language="javascript" type="text/javascript">
alert(\'Subscription failed. Please, send an email to alex.dapunt#gmail.com\');
// window.location = \'contact_page.html\';
</script>';}?>
but i have to say that if this is just an example it's ok but if you'r gonna use it in your site you have to do some input validation.

Email form showing ,PHP Content instead of executing and sending email

I'm trying to get a 'contact us email form' to work on my website but when I press the 'send' button, it doesnt send the email and it shows the entire contents of the .php file thats supposed to make it send? Can anybody help me please? my html page i called 'Contact_Us.html' and the PHP file is called 'contact.php".
Here is the HTML
<form action="contact.php" method="post">
Your name<br>
<input type="text" name="cf_name"><br>
Your e-mail<br>
<input type="text" name="cf_email"><br>
Message<br>
<textarea name="cf_message"></textarea><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
and here is the .PHP
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'user#domain.com';
$subject = 'Message from a visitor on The New Moston Club Wesbite '.$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, we have received your message. We will contact you shortly!');
window.location = 'Contact_Us.html';
</script>
<?php
}
else { ?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to thenewmostonclub#outlook.com sorry for any inconvenience');
window.location = 'Contact_Us.html';
</script>
<?php
}
?>
does it show in your browsers URL bar c:/user/.../yourscript.php if yes then try localhost/yourscript.php

Contact form with check boxes having troubles [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I need help with add check boxes to my contact form I have this as the html.
<form action="contact.php" method="post">
Your name<br><input type="text" name="cf_name"><br>
Your e-mail<br><input type="text" name="cf_email"><br>
Message<br><textarea name="cf_message"></textarea><br>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
And here is my php part of the code
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
$mail_to = 'example#example.com';
$subject = 'Message from a site visitor '.$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 help#helpme.com');
window.location = 'contact.html';
</script>
<?php
}
?>
I need help adding groups of check boxes so I can check one box from each of the groups and it would send an email with a list of what check boxes where picked from each group.
Use this code
<form action="contact.php" method="post">
Your name<br><input type="text" name="cf_name"><br>
Your e-mail<br><input type="text" name="cf_email"><br>
Message<br><textarea name="cf_message"></textarea><br>
Check boxes: <input type="checkbox" name="abc[]" value="Car">Car<br/>
<input type="checkbox" name="abc[]" value="Bus">Bus<br/>
<input type="submit" value="Send">
<input type="reset" value="Clear">
</form>
<?php
$field_name = $_POST['cf_name'];
$field_email = $_POST['cf_email'];
$field_message = $_POST['cf_message'];
// check boxess
$check_box_values = "Check box value: ";
if(isset($_POST['abc'])){
foreach($_POST['abc'] as $value){
$check_box_values .= $value;
$check_box_values .= ', ';
}
}
// end check boxess
$mail_to = 'example#example.com';
$subject = 'Message from a site visitor '.$field_name;
$body_message = 'From: '.$field_name."\n";
$body_message .= 'E-mail: '.$field_email."\n";
$body_message .= 'Message: '.$field_message."\n".$check_box_values;
$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 help#helpme.com');
window.location = 'contact.html';
</script>
<?php
}
?>

Categories