Hide Email form after sumbission - php

I have a PHP email form embedded at the top of the HTML of my contact form page (index.php):
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form';
$to = 'email#example.com';
$subject = 'Message Contact Form ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div>Thank You! I will be in touch</div>';
} else {
$result='<div>Sorry there was an error sending your message.</div>';
}
}
}
?>
HTML:
<form role="form" method="post" action="index.php">
...
</form>
and I wanted to hide the form only after successful submission. How can I do this?

In your CSS file;
.hide {
display:none;
}
(If unsure how to do this, this question should help)
Then inside your PHP if block;
$class = "";
if (mail ($to, $subject, $body, $from)) {
$result='<div>Thank You! I will be in touch</div>';
$class = 'class="hide"';
} else {
$result='<div>Sorry there was an error sending your message.</div>';
}
and finally, on index.php:
<form role="form" method="post" action="index.php" <?php echo $class ?>>
Now you can hide the entire form, with a little bit of CSS and a dynamic variable.
Edit:
If you wanted to avoid using an external or internal CSS file entirely, you could apply the CSS inline to your html element directly, like so;
$style = 'style = "display:none;"';
An alternative approach, if you're not using style sheets or any other CSS.

To hide form after submission and email success,give id to form(here its id="myForm", then use style to display:none as shown below)
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form';
$to = 'email#example.com';
$subject = 'Message Contact Form ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div>Thank You! I will be in touch</div>';
echo "<style> #myForm{ display:none; } </style>";
} else {
$result='<div>Sorry there was an error sending your message.</div>';
echo "<style> #myForm{ display:block; } </style>";
}
}
}
?>
<form role="form" method="post" action="index.php" id="myForm">
</form>

Related

Recaptcha in Form

I have almost the same form as given the following URL:
URL: PHP form + Google reCAPTCHA
But after including the PHP code to my website-(www.obzservices.com) given in the answer, my website is just showing me the pre-loader icon i.e. my website is not loading up. I have edit my PHP Code previously by adding my Site Key, adding $name, $title, $location etc but it didn't help. Here is my PHP code:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$title = $_POST['title'];
$location = $_POST['location'];
$industry = $_POST['industry'];
$quantity = $_POST['quantity'];
$message = $_POST['message'];
$from = 'From: QuoteForm';
$to = 'obaid#obzservices.com';
$subject = 'Quote Request';
$body = "From: $name\n E-Mail: $email\n Title: $title\n Location: $location\n Industry: $industry\n Quantity: $quantity\n Message:\n $message";
if ($_POST['submit']) {
if ($email != '') {
if(mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
}
}?>
Add following simpleScript
<script src='https://www.google.com/recaptcha/api.js'></script>
<script>
function get_action(form)
{
var v = grecaptcha.getResponse();
if(v.length == 0)
{
document.getElementById('captcha').innerHTML="You can't leave Captcha Code empty";
return false;
}
else
{
document.getElementById('captcha').innerHTML="Captcha completed";
return true;
}
}
</script>
and add this HTML before your submit button:
<span id="captcha" style="color:red" /></span>
<div class="g-recaptcha" id="rcaptcha" data-sitekey="site key"></div>
Add this to your form
onSubmit="return get_action()" such As below
<form action="" method="post" onSubmit="return get_action()">

Add a subject field to PHP email form

I have an email form in PHP and I want to add a subject to the body of the email:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form';
$to = 'emailo#example.com';
$subject = 'Message from Camino.bo ';
$body ="From: $name\n E-Mail: $email\n Message:\n $message";
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="success">Thank You! I will be in touch</div>';
} else {
$result='<div class="danger">Sorry there was an error sending your message. Please try again.</div>';
}
}
}
?>
This form submits an email with the subject "Message from Camino.bo", that's ok but I want to submit in the body of the email the value the user selects from a dropdown menu:
HTML:
<select name="subject" id="email_subject">
<option value="default subject">Select a subject</option>
<option>Product A</option>
<option>Product B</option>
<option>Product C</option>
</select>
How can I do this?
PS: the subject field in my form is optional with no validation required.
Your variables are a bit mixed up. The 4th parameter of the mail() function is not 'From', it is 'Email Headers', which can be any number of header values in the correct format, each separated with a new line. The 'From' will just be one of those headers..
$to = 'you#youremailaddress.com';
$subject = 'My Email Subject';
$body = 'The body text of your email....';
$headers = "MIME-Version: 1.0\r\n".
"Content-type: text/html; charset=iso-8859-1\r\n".
"From: YOU <your#email.com>\r\n";
// and others as required
mail($to, $subject, $body, $headers);
Add your POSTed options to the $body tag as required
I'm not sure if I'm missing something, but in your HTML, you want to add values to the remainder of your options:
<select name="subject" id="email_subject">
<option value="default subject">Select a subject</option>
<option value="Product A">Product A</option>
<option value="Product B">Product B</option>
<option value="Product C">Product C</option>
</select>
Then your PHP could be as follows:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Camino Contact Form <youremailaddress#domain.com>';
$to = 'emailo#example.com';
$subject = 'Message from Camino.bo ';
$select_subject = $_POST['subject'];
$body ="From: $name\n E-Mail: $email\n Message: $message\n Subject: $select_subject";
$headers = "From: " . $from;
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $headers)) {
$result='<div class="success">Thank You! I will be in touch</div>';
} else {
$result='<div class="danger">Sorry there was an error sending your message. Please try again.</div>';
}
}
}
?>
Notice that I've added $select_subject and added it to your email $body variable. Also please replace 'youremailaddress#domain.com' with the FROM email address you wish to use.
Also, I don't know if you already handle this elsewhere in your code, but I think you should display a message to screen if either $errName, $errEmail or $errMessage evaluate to true. Right now, if either one of those is true (assuming the code you pasted here is complete) your end user would get nothing displayed on their screen upon form submission and they might be confused by what happened and try to resubmit.

Bootstrap Contact Form - How to send html email

I am using a bootstrap contact form. The form works fine but whatever I do I cannot get it to send html emails. I have added headers and css inlined to different parts of the script but it never sends the email with any html formatting. I am banging my head against the wall and would really appreciate any help. Where do I put the headers variable and where do I put the html?
Here is the php code.
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Mywebsite';
$to = 'me#email.com';
$subject = 'Contact Email ';
$headers = "";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "To: ". $to. "\r\n";
$headers .= "From: ". $from;
$body = " $headers, From: $name\n E-Mail: $email\n Message: $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if content has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Your anti-spam is incorrect';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! </div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
And here is the HTML.
<div style="padding:10px; background:#000; font-size:46px; font-weight: 900; color:#fff; ">
My Website
</div>
<div style="padding:24px; font-size:17px; background:#DCDCDC ; ">
This is a message from My website.
<br>
<br>
<br>
Login in to your account
<br>
<br>
</div>
<br>
<br>
</div>
replace mail($to, $subject, $body, $from); with mail($to, $subject, $body, $headers);

Contact form is not working?

I'm trying to create a simple contact page however when I submit the form, it leads me to the blank page contact-form.php. I used this article as reference: http://tangledindesign.com/how-to-create-a-contact-form-using-html5-css3-and-php/
What am I doing wrong?
Code on index.php (corner divs are purely for styling purposes)
<form action="contact-form.php" method="post" autocomplete="off" id="contact">
<div class="corner"></div>
<input type="text" required name="name" placeholder="Name" value="">
<div class="corner"></div>
<input type="text" required name="email" placeholder="Email" value="">
<div class="corner"></div>
<input type="text" required name="check" placeholder="Question of the day: What's 2 + 2 ?" value="">
<div class="corner"></div>
<textarea name="message" rows="25" cols="50" placeholder="Drop me a line!"></textarea>
<button class="send" type="submit" name="submit">Send</button>
</form>
Code on contact-form.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: MessageAnita';
$to = 'myemail#gmail.com';
$subject = 'Hello';
$check = $_POST['check'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit'] && $check == '4') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $check != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
change the button tag to an input tag
< input type ="submit" name="submit"/>
In your form, change:
<button class="send" type="submit" name="submit">Send</button>
to:
<input type="submit" name="submit" value="Submit">
Plus, you may want to use this PHP mailing method, since the mail came into my SPAM when testing.
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['email'];
$to = 'myemail#gmail.com';
$subject = 'Hello';
$check = $_POST['check'];
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$headers = "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n";
if ($_POST['submit'] && $check == '4') {
if (mail ($to, $subject, $body, $headers)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else if ($_POST['submit'] && $check != '4') {
echo '<p>You answered the anti-spam question incorrectly!</p>';
}
?>
And if you want to send as HTML use:
$headers = "MIME-Version: 1.0\n";
$headers .= "Content-Type: text/html; charset=iso-8859-1\n";
$headers .= "From: $from" . "\r\n" .
"Reply-To: $from" . "\r\n";

php mail() script not working

Hey guys I cannot for the life of me figure out where I went wrong here. When the form submits it takes it to a blank page. No errors or confirmed. Just Blank. Im assuming it is a syntax error but I just cant see it for some reason. Do you guys see anything?
Here is my form code:
<form method="POST" action="mailtest.php">
<label>Name<span class="req">*</span></label>
<input name="name" placeholder="Type Here">
<label>Email<span class="req">*</span></label>
<input name="email" type="email" placeholder="Type Here">
<label>Subject</label>
<input name="subjectf" placeholder="Type Here">
<label>Message<span class="req">*</span></label>
<textarea name="message" placeholder="Type Here"></textarea>
<input class="submit" name="submit" type="submit" value="">
<span class="right" style="color:red">* represents a mandatory field.</span>
</form>
And here is the php script I have on the mailtest.php page:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['name'];
$to = 'rkoolman#bellsouth.net';
$subject= 'new enquiry on website';
$subjectf= $_POST['subjectf'];
$body = "From: $name\n E-Mail: $email\n Subject: $subjectf\n Message:\n $message";
if ($_POST['submit']) {
if ($name != '' && $email != '' && $message != '') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else {
echo '<p>You need to fill in all required fields!!</p>';
}
}
?>
You should always first check whether the form was submitted so
remove if ($_POST['submit']) and put it on the top of the script..
also you should check whther the $_POST["submit"] was set.. the script should look like this:
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = $_POST['name'];
$to = 'test#test.com';
$subject= 'new enquiry on website';
$subjectf= $_POST['subjectf'];
$body = "From: $name\n E-Mail: $email\n Subject: $subjectf\n Message:\n $message";
if ($name != '' && $email != '' && $message != '') {
if (mail ($to, $subject, $body, $from)) {
echo '<p>Your message has been sent!</p>';
} else {
echo '<p>Something went wrong, go back and try again!</p>';
}
} else {
echo '<p>You need to fill in all required fields!!</p>';
}
}
Set your submit input value to submit
<input class="submit" type="submit" name="submit" value="submit">

Categories