How to clear form after ajax submit? - php

I have a small contact form. Sending messages is working but the form stays with text after submitting. I searched and tried some code to clear it with no success.
// JavaScript Document
$('#contact-form').submit(function (e) {
"use strict";
e.preventDefault();
$.ajax({
type: "POST",
url: "contactform.php",
data: $(this).serialize(), //get form values
sucess: function (data) {
document.getElementById("contact-form").reset();
}
}).done(function (data) {
console.log(data); // will contain success or invalid
});
});
And some php
<?php
$firstname = $_POST["firstname"];
$lastname = $_POST["lastname"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$message = $_POST["message"];
$EmailTo = "marcin#rmonline.com";
$Subject = "New Message Received";
// prepare email body text
$email_content .= "Firstname: $firstname\n";
$email_content .= "Lastname: $lastname\n";
$email_content .= "Email: $email\n";
$email_content .= "Phone: $phone\n";
$email_content .= "Message: $message";
// send email
$success = mail($EmailTo, $Subject, $email_content, "From:".$email);
// redirect to success page
if ($success){
echo "success";
} else {
echo "invalid";
}
?>
And html
<form id="contact-form" method="post" action="contactform.php" role="form">
<div class="messages"></div>
<div class="form-group">
<input id="form_name" type="text" name="firstname" class="form-control" placeholder="Please enter your firstname *" required="required" data-error="Firstname is required.">
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<input id="form_lastname" type="text" name="lastname" class="form-control" placeholder="Please enter your lastname *" required="required" data-error="Lastname is required.">
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<input id="form_email" type="email" name="email" class="form-control" placeholder="Please enter your email *" required="required" data-error="Valid email is required.">
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<input id="form_phone" type="tel" name="phone" class="form-control" placeholder="Please enter your phone">
<div class="help-block with-errors"></div>
</div>
<div class="form-group">
<textarea id="form_message" name="message" class="form-control" placeholder="Message for us *" rows="4" required data-error="Please,leave us a message."></textarea>
<div class="help-block with-errors"></div>
</div>
<button type="submit" class="btn" value="Send message">Send Message</button>
<p class="text-muted"><strong>*</strong> These fields are required.</p>
</form>
I've got the feeling that I'm missing something, but don't know what yet.

Try removing the preventDefault. The form will empty once the page reloads. That or you will have to manually set the value of the form inputs to empty.
Is there a particular reason you are submitting the form using ajax and not allowing the form to post directly to the php file?

Related

PHP: How to clear form after submit and show success alert?

I've been having trouble with PHP (new to php) to clear my form fields AFTER a successful submission. I would also like to show an alert that it was successful. Any ideas? Using javascript with the onclick listener for the submit button will not work because it clears the fields before PHP has time to send the data.
Here is my html:
<div class="col-lg-7 mt-5 mt-lg-0 d-flex align-items-strech" data-aos="fade-left">
<form id="contact-form" action="contact-form-handler.php" method="post" role="form" class="php-email-form">
<div class="form-row">
<div class="form-group col-md-6">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" data-rule="minlen:4" data-msg="Please enter at least 4 characters"/>
<div class="validate"></div>
</div>
<div class="form-group col-md-6">
<label for="name">Email</label>
<input type="email" name="email" class="form-control" id="email" data-rule="email" data-msg="Please enter at valid email"/>
<div class="validate"></div>
</div>
</div>
<div class="form-group">
<label for="name">Subject</label>
<input type="text" class="form-control" name="subject" id="subject" data-rule="minlen:4" data-msg="Please enter a subject of at least 8 characters"/>
<div class="validate"></div>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message" rows="10" data-rule="required" data-msg="Please enter a message"></textarea>
<div class="validate"></div>
</div>
<div class="text-center"><button type="submit">Send Message</button></div>
</form>
</div>
Here is my PHP:
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$email_from = "Portfolio Email";
$email_body = "Name: $name.\n".
"User Email: $visitor_email.\n".
"User Message: $message.\n";
$email_to = "email#gmail.com";
$headers = "From: $email_from \r\n";
mail($email_to, $subject, $email_body);
?>
If you do not want to use ajax, please try the following
(assuming the html is known as "form.html", and the php is "contact-form-handler.php")
The php redirection to form.html will clear the input fields (a normal behavior in web-browsers), and the javascript will show the word "successful" as an alert.
Please make further adjustment(s) according to your needs.
form.html
<div class="col-lg-7 mt-5 mt-lg-0 d-flex align-items-strech" data-aos="fade-left">
<form id="contact-form" action="contact-form-handler.php" method="post" role="form" class="php-email-form">
<div class="form-row">
<div class="form-group col-md-6">
<label for="name">Name</label>
<input type="text" name="name" class="form-control" id="name" data-rule="minlen:4" data-msg="Please enter at least 4 characters"/>
<div class="validate"></div>
</div>
<div class="form-group col-md-6">
<label for="name">Email</label>
<input type="email" name="email" class="form-control" id="email" data-rule="email" data-msg="Please enter at valid email"/>
<div class="validate"></div>
</div>
</div>
<div class="form-group">
<label for="name">Subject</label>
<input type="text" class="form-control" name="subject" id="subject" data-rule="minlen:4" data-msg="Please enter a subject of at least 8 characters"/>
<div class="validate"></div>
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" name="message" rows="10" data-rule="required" data-msg="Please enter a message"></textarea>
<div class="validate"></div>
</div>
<div class="text-center"><button type="submit">Send Message</button></div>
</form>
</div>
<script>
function get(name){
if(name=(new RegExp('[?&]'+encodeURIComponent(name)+'=([^&]*)')).exec(location.search))
return decodeURIComponent(name[1]);
}
</script>
<script>
if (get("result")=="success")
{
alert("Successful");
}
</script>
contact-form-handler.php
<?php
$name = $_POST['name'];
$visitor_email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$email_from = "Portfolio Email";
$email_body = "Name: $name.\n".
"User Email: $visitor_email.\n".
"User Message: $message.\n";
$email_to = "email#gmail.com";
$headers = "From: $email_from \r\n";
mail($email_to, $subject, $email_body);
?>
<script>
window.location.href="form.html?result=success"
</script>

contact form using php in angularjs

html :
View/contactform.html
<form method="post" role="form" name="myemailform" action="sendingemail.php">
<div class="form-group">
<input type="text" name="name" class="form-control" id="name" placeholder="Your Name" data-rule="minlen:4" data-msg="Please enter at least 4 chars" />
<div class="validation"></div>
</div>
<div class="form-group">
<input type="email" class="form-control" name="email" id="email" placeholder="Your Email" data-rule="email" data-msg="Please enter a valid email" />
<div class="validation"></div>
</div>
<div class="form-group">
<input type="text" class="form-control" name="subject" id="subject" placeholder="Subject" data-rule="minlen:4" data-msg="Please enter at least 8 chars of subject" />
<div class="validation"></div>
</div>
<div class="form-group">
<textarea class="form-control" name="message" rows="5" cols="10" data-rule="required" data-msg="Please write something for us" placeholder="Message"></textarea>
<div class="validation"></div>
</div>
<div class="text-center"><input type="submit" class="btn applyBtn" name="submit"/></div>
</form>
PHP:
view/sendingemail.php
<?php
$name = $_POST['name'];
$email_from = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
$emails = "From: $email_from \r\n";
$emails .= "Name: $name \r\n";
$emails .= "Message: $message \r\n";
mail('sunitha_anilkumar#yahoo.com',$subject,$emails);
echo 'Mail sent!';
header('Location: thank-you.html');
?>
app.js
.when('/contactus', {
templateUrl: 'view/contactform.html',
controllerAs:'vm'
})
I wanted a contact form in my angular project and I given the form in view/contactform.html and sendingemail.php for submitting the form details. But when I submit the form, i am just seeing the sendingemail.php code , no form submission is done.Need assistance.
This means your webserver does not recognise your php file to be run as a php file. You might want to check your webserver's documentation to install and/or enable the PHP module.

PHP form won't pass value to email anyone?

For some odd reason, the business input value won't submit to the email... it's always blank.. can someone tell me what's wrong? Thank you!
<?php
// Check for empty fields
if(isset(empty($_POST['submit']) ||
empty($_POST['email']) ||
empty($_POST['phone']) ||
empty($_POST['business']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$business = strip_tags(htmlspecialchars($_POST['business']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$to = 'somekoreanguy#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nBusiness: $business\n\nMessage:\n$message";
$headers = "From: noreply#yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
And here's my HTML:
<form name="sentMessage" id="contactForm" novalidate method="post">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Name *" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Business Name *" id="business" required data-validation-required-message="Please enter your business name.">
<p class="help-block text-danger"></p>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" placeholder="Your Message *" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div id="success"></div>
<button type="submit" name="submit" class="btn btn-xl">Send Message</button>
</div>
</div>
</form>
For some reason, when I put that "empty($_POST['business']) ||" in there, nothing comes in to the email. When I remove "type="text" from my business html section the email goes through but nothing as a value. it'll just say Business: and blank....
you did not define method="post"
<form name="sentMessage" id="contactForm" novalidate>
so it will be like
<form name="sentMessage" id="contactForm" novalidate method="post">
give name for
<button type="submit" name ="submit" class="btn btn-xl">Send Message</button>
So your code will be
if(isset($_POST['submit']){
//set your code here
}
It's always blank, because no name="" in your input form, name="" to reference form data after a form is submitted. Add name="" to your input like this..
<?php
// Check for empty fields
if(isset($_POST['submit'])) {
if(trim($_POST['email'])== '' || trim($_POST['phone'])== '' || trim($_POST['business'])== '' || trim($_POST['message'])== '' || !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
}else{
$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$business = strip_tags(htmlspecialchars($_POST['business']));
$message = strip_tags(htmlspecialchars($_POST['message']));
// Create the email and send the message
$to = 'somekoreanguy#gmail.com'; // Add your email address inbetween the '' replacing yourname#yourdomain.com - This is where the form will send a message to.
$email_subject = "Website Contact Form: $name";
$email_body = "You have received a new message from your website contact form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nBusiness: $business\n\nMessage:\n$message";
$headers = "From: noreply#yourdomain.com\n"; // This is the email address the generated message will be from. We recommend using something like noreply#yourdomain.com.
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
}
}
?>
<html>
<head>
</head>
<body>
<form name="contactForm" novalidate method="POST">
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Name *" name="name" id="name" data-validation-required-message="Please enter your name." required>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Your Email *" name="email" id="email" data-validation-required-message="Please enter your email address." required>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Your Phone *" name="phone" id="phone" data-validation-required-message="Please enter your phone number." required>
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="text" class="form-control" placeholder="Your Business Name *" name="business" id="business" data-validation-required-message="Please enter your business name." required>
<p class="help-block text-danger"></p>
</div>
<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" placeholder="Your Message *" name="message" id="message" data-validation-required-message="Please enter a message." required></textarea>
<p class="help-block text-danger"></p>
</div>
</div>
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<div name="success"></div>
<button type="submit" name ="submit" class="btn btn-xl">Send Message</button>
</form>
</body>
</html>

PHP Form only showing name email and message in email, not the rest [duplicate]

This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
In my emails , I am only recieving the customer's name email and the message contents. Just wanted to see what you guys think - no clue why this is happening.
Here's my code.
<form name="sentMessage" id="contactForm" novalidate>
<div class="control-group form-group">
<div class="controls">
<label>Full Name:</label>
<input type="text" class="form-control" id="name" required data-validation-required-message="Please enter your name.">
<p class="help-block"></p>
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Phone Number:</label>
<input type="text" class="form-control" name="phone" required data-validation-required-message="Please enter your phone number.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Email Address:</label>
<input type="email" class="form-control" name="email" required data-validation-required-message="Please enter your email address.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Best way for us to reach back to you:</label>
<input type="text" class="form-control" name="reach" required data-validation-required-message="Please enter the best way we can reach you.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Type of event: (ex. sports/wedding/business) </label>
<input type="text" class="form-control" name="event" required data-validation-required-message="Please enter your type of event.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Date or Weekend Desired (ex. 4/16-4/25) </label>
<input type="text" class="form-control" name="date" required data-validation-required-message="Please enter your desired date." >
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Number of Attendees:</label>
<input type="text" class="form-control" name="attendees" required data-validation-required-message="Please enter your number of attendees.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Overnight Guests</label>
<input type="text" class="form-control" name="overnight" required data-validation-required-message="Please enter your number of overnight guests.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Meals Included? (Yes/No/Maybe)</label>
<input type="text" class="form-control" name="meals" required data-validation-required-message="Please enter whether or not you'd like meals included.">
</div>
</div>
<div class="control-group form-group">
<div class="controls">
<label>Additional Information:</label>
<textarea rows="10" cols="100" class="form-control" name="message" required data-validation-required-message="Please enter your message" maxlength="3999" style="resize:none"></textarea>
</div>
</div>
<div id="success"></div>
<!-- For success/fail messages -->
<button type="submit" class="btn btn-primary">Send Message</button>
</form>
That was the HTML here's the PHP.
<?php
// check if fields passed are empty
if(empty($_POST['name']) ||
empty($_POST['phone']) ||
empty($_POST['email']) ||
empty($_POST['message']) ||
!filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
{
echo "No arguments Provided!";
return false;
}
$name = $_POST['name'];
$phone = $_POST['phone'];
$email_address = $_POST['email'];
$phone =$_Post['phone'];
$reach = $_Post['reach'];
$event = $_Post['event'];
$date = $_Post['date'];
$attendees = $_Post['attendees'];
$overnight = $_Post['overnight'];
$meals = $_Post['meals'];
$message = $_POST['message'];
$to = 'me(notmyactualemail)#gmail.com';
$email_subject = "Wahnee Events Inquiry";
$email_body = "Hooray! You have an event inquiry.\n\n"."Here are the details:\n\nName: $name \n\nPhone: $phone \n\nEmail: $email_address \n\nPhone: $phone \n\nReach: $reach \n\nEvent: $event \n\nDate: $date \n\nAttendees: $attendees \n\nOvernight: $overnight \n\nMeals: $meals \n\nInfo: $message";
$headers = "From: inquiry#wahneeevents.com\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
return true;
?>
Here's the email I get ( I put working next to anything working)
Hooray! You have an event inquiry.
Here are the details:
Name: WORKING
Phone:
Email: WORKING
Phone:
Reach:
Event:
Date:
Attendees:
Overnight:
Meals:
Info: WORKING
Thanks so much! :)
Add all your message body variables in one variable like:
$mesage = "Name: " . $_POST['name'];
$mesage .= $_POST['phone'];
$mesage .= $_POST['email'];
$mesage .=$_POST['phone'];
$mesage .= $_POST['reach'];
$mesage .= $_POST['event'];
$mesage .= $_POST['date'];
$mesage .= $_POST['attendees'];
$mesage .= $_POST['overnight'];
$mesage .= $_POST['meals'];
$mesage .= $_POST['message'];
and just pass $message in:
$email_body = $message;

PHP Mailer script not receiving POST variables

when a contact me script is run on my website, the script doesn't seem to be grabbing the POST variables from the HTML. The HTML is as follows:
<section id="contact">
<div class="container">
<div class="row">
<div class="col-lg-12 text-center">
<h2 class="section-heading">Contact Us</h2>
<h3 class="section-subheading text-muted">Fill out a form below to make an appointment, and our top masseur will be with you as soon as possible!</h3>
</div>
</div>
<!-- To Do: Change pictures, add FA's, finish "about",and remove portfolio-->
<div class="row">
<div class="col-lg-12">
<form name="sentMessage" id="contactForm" formaction="mail/contact_me.php" method="POST" novalidate>
<div class="row">
<div id="center><div class="col-md-6">
<center><div class="form-group">
<input type="text" class="form-control" placeholder="Your Name *" id="name" name="name" required data-validation-required-message="Please enter your name.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="email" class="form-control" placeholder="Your Email *" id="email" required data-validation-required-message="Please enter your email address.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Your Phone *" id="phone" required data-validation-required-message="Please enter your phone number.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Massage Type *" id="type" required data-validation-required-message="Please enter your type of massage.">
<p class="help-block text-danger"></p>
</div>
<div class="form-group">
<input type="tel" class="form-control" placeholder="Date of desired appointment *" id="message" required data-validation-required-message="Please enter the date">
<p class="help-block text-danger"></p>
</div>
</div>
<!--<div class="col-md-6">
<div class="form-group">
<textarea class="form-control" placeholder="Please include date, and type of massage *" id="message" required data-validation-required-message="Please enter a message."></textarea>
<p class="help-block text-danger"></p>
</div>
</div> -->
<div class="clearfix"></div>
<div class="col-lg-12 text-center">
<button type="submit" class="btn btn-xl" formaction="mail/contact_me.php">Set Appointment!</button>
</div>
</div>
</form>
</div>
</div>
</div>
</section>
The PHP Script (contact_me.php) is:
<?php
if(isset($_POST['submit'])){
$to = "*****"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['name'];
$subject = "New Appointment!";
$subject2 = "Copy of your Appointment credentials.";
$message = $first_name . "'s Appointment. Phone: " . $phone . " Email: " . $_POST['email'] . " Type of Massage:" . "\n\n" . $_POST['type'] . " On:" . $_POST['message'];
$message2 = "Here is a copy of your message " . $first_name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to,$subject,$message,$headers);
mail($from,$subject2,$message2,$headers2); // sends a copy of the message to the sender
echo "Mail Sent. Thank you " . $first_name . ", we will contact you shortly.";
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
I tried removing the isset (Or rather changing it to !isset), and the email sent. However, the credentials and the fields that were provided at the website were completely missing, and only the given text in the script was sent.
The attribute formaction is not valid. Use action instead.
In your case:
<form name="sentMessage" id="contactForm" action="mail/contact_me.php" method="POST" novalidate>
You're also missing a name attribute, as #anant kumar singh stated.

Categories