Show message and reset form PHPMailer - javascript

I configured a phpmailer to send info from a form and works perfectly but when sends the mail show a blank page with the message: "Thank you for contacting us, one of our associates will contact you soon"
Here is the sending code:
try {
if ($_POST['name'] != '' && $_POST['email'] != '' && $_POST['message'] != '') {
$mail->Body = $messaje;
}
} catch (phpmailerException $e) { //Catch all kinds of bad addressing
throw new phpmailerAppException($e->getMessage());
}
if(!$mail->send()) {
echo 'Message could not be sent.';
echo 'Mailer Error: ' . $mail->ErrorInfo;
exit;
}
and after that I have this:
if (!$mail->send()) {
echo "Mailer Error: " . $mail->ErrorInfo;
}
/*CLIENT MESSAGE*/
echo 'Thank you for contacting us, one of our associates will contact you soon';
}else {
exit();
}
I want to show that message in the same page of the form and reset the form.
Thanks in advance
Here is my form:
<form role="form" id="request_quoute" method="post" action="<?php echo site_url(); ?>/wp-content/themes/sfra/sendMail.php">
<input class="form-control col-xs-12" id="name" name="name" placeholder="Name" type="text" required>
<input class="form-control col-xs-12" id="email" name="email" placeholder="Email" type="email" required>
<textarea class="form-control col-xs-12" rows="5" id="message" name="message" placeholder="Message" style="resize:none" required></textarea>
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="text-align: center">
<button type="submit" id="send-form" class="btn btn-primary contact_button">Send Message</button>
</div>
</form>

Related

Else statement being run even when if statement is true?

I'm trying to establish a verification system for a form, whereas one of the input elements is called verification. The form should only be submitted (as an email to someone else) if the value of verification is 108 — which works. However, when I run my code, I find that the code inside the else statement (of the embedded if-statement) also runs, and I get a popup window that asks me what 3+105 is. Why is this?
HTML
<form class="pb-5" action="" method="POST">
<div class="form-row">
<div class="form-group col-md-6">
<label for="inputName4">Name</label>
<input
type="text"
class="form-control"
id="inputName"
name="name"
value="<?php echo isset($_POST['name']) ? htmlspecialchars($_POST['name'], ENT_QUOTES) : ''; ?>"
required
/>
</div>
<div class="form-group col-md-6">
<label for="inputCompany">Company</label>
<input
type="text"
class="form-control"
id="inputCompany"
name="company"
value="<?php echo isset($_POST['company']) ? htmlspecialchars($_POST['company'], ENT_QUOTES) : ''; ?>"
required
/>
</div>
</div>
<div class="form-row">
<div class="form-group col-md-6">
<label for="subject">Subject</label>
<input
type="text"
class="form-control"
id="subject"
name="subject"
value="<?php echo isset($_POST['subject']) ? htmlspecialchars($_POST['subject'], ENT_QUOTES) : ''; ?>"
required
/>
</div>
<div class="form-group col-md-6">
<label for="inputEmail">Email</label>
<input
type="email"
class="form-control"
id="inputEmail"
name="email"
value="<?php echo isset($_POST['email']) ? htmlspecialchars($_POST['email'], ENT_QUOTES) : ''; ?>"
required
/>
</div>
</div>
<div class="form-group">
<label for="verification">What is 5+103?</label>
<input
type="text"
class="form-control"
id="verification"
name="verification"
required
></input>
</div>
<div class="form-group">
<label for="textarea">Message</label>
<textarea
class="form-control"
id="textarea"
rows="3"
name="message"
required
><?php echo isset($_POST['message']) ? htmlspecialchars($_POST['message'], ENT_QUOTES) : ''; ?>"</textarea>
</div>
<button type="submit" class="btn btn-primary my-3">
Send
</button>
</form>
PHP
$verification = $_POST["verification"];
settype($verification, "integer");
if($_SERVER["REQUEST_METHOD"] == "POST") {
if($verification == 108) {
mail($to, $subject, $message, $headers);
echo "<script> alert('Thank you for your mail!')</script>";
} elseif ($_SERVER["REQUEST_METHOD"] == "POST" && mail($to, $subject, $message, $headers) === false) {
echo "<script> alert('An error has occurred.')</script>";
} else {
echo "<script> alert('What is 3+105?')</script>";
}
} else {
return;
}
If I can clarify my issue in any way, please do not hesitate to let me know! Thanks in advance!
I suppose you have to rewrite your if ... else ...
$verification = $_POST["verification"];
settype($verification, "integer");
if($_SERVER["REQUEST_METHOD"] == "POST") {
if($verification == 108) {
//TEST IF MAIL IS CORRECTLY SEND
if( mail($to, $subject, $message, $headers) === false){
echo "<script> alert('An error has occurred.')</script>";
}
else{
echo "<script> alert('Thank you for your mail!')</script>";
}
}
else {
echo "<script> alert('What is 3+105?')</script>";
}
}

How to retain Form data After Incomplete Form Submission

This is my code,
PHP
if(isset($_POST['submit'])){
if (empty($_POST["name"])) {
$NameErr = "Name is required";
} else {
$name = Test_User_Input($_POST["name"]);
$name = mysqli_real_escape_string($connection , $name);
}
if (empty($_POST["contact"])) {
$ContactErr = "Contact is required";
} else {
$contact = Test_User_Input($_POST["contact"]);
$contact = mysqli_real_escape_string($connection , $contact);
}
if (empty($_POST["email"])) {
$EmailErr = "Email is required";
} else {
$email = Test_User_Input($_POST["email"]);
$email = mysqli_real_escape_string($connection , $email);
}
if (empty($_POST["pan"])) {
$PanErr = "PAN is required";
} else {
$pan = Test_User_Input($_POST["pan"]);
$pan = mysqli_real_escape_string($connection , $pan);
}
if (empty($_POST["dob"])) {
$DobErr = "DOB is required";
} else {
$dob = Test_User_Input($_POST["dob"]);
$dob = mysqli_real_escape_string($connection , $dob);
}
if (empty($_POST["gender"])) {
$GenderErr = "Gender is required";
} else {
$gender = Test_User_Input($_POST["gender"]);
$gender = mysqli_real_escape_string($connection , $gender);
}
if (!empty($name) && !empty($contact) && !empty($email) && !empty($pan) &&
!empty($dob) && !empty($gender){
$query = "INSERT INTO form ( name, contact, email, pan, birthday,
gender )VALUES ('$name' , '$contact' , '$email' , '$pan' , '$dob' ,
'$gender')";
}
if ($insert_query) {
echo "Form Filled";
} else {
echo "Please Fill the form";
}
}
function Test_User_Input($Data)
{
return $Data;
}
HTML
<form action="" autocomplete="off" method="post">
<div class="col-md-6 col-sm-6 col-xs-12 marB20">
<div class="form-group-kyc">
<label>Full Name:</label><span class = "error_msg"><?php echo $NameErr; ?></span>
<input type="text" name="name" id="name" placeholder="Full Name" class="form-control-kyc" value="<?php echo isset($_POST['name']) ? $_POST['name'] :' '?> ">
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12 marB20">
<div class="form-group-kyc">
<label>Contact Number:</label><span class = "error_msg"><?php echo $ContactErr; ?></span>
<input type="text" name="contact" id="contact" placeholder="Contact Number" class="form-control-kyc" value="<?php echo isset($_POST['contact']) ? $_POST['contact'] : '' ?> ">
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12 marB20">
<div class="form-group-kyc">
<label>Email Id:</label><span class = "error_msg"><?php echo $EmailErr; ?></span>
<input type="email" name="email" id="email" placeholder="Email id" class="form-control-kyc" value="<?php echo isset($_POST['email']) ? $_POST['email'] : '' ?> ">
</div>
</div>
<div class="col-md-6 col-xs-12 col-sm-6 marB20">
<div class="form-group-kyc">
<label>PAN:</label><span class = "error_msg"><?php echo $PanErr; ?></span>
<input type="text" name="pan" id="pan" placeholder="PAN Card No." class="form-control-kyc" value="<?php echo isset($_POST['pan']) ? $_POST['pan'] : '' ?> ">
</div>
</div>
<div class="col-md-6 col-xs-12 col-sm-6 marB20">
<div class="form-group-kyc">
<label>DOB:</label><span class = "error_msg"><?php echo $DateErr; ?></span>
<input type="date" name="dob" id="dob" placeholder="DOB." class="form-control-kyc" value="">
</div>
</div>
<div class="col-md-6 col-xs-12 col-sm-6 marB20">
<div class="form-group-kyc">
<label>Gender:</label> <span class = "error_msg"><?php echo $GenderErr; ?></span>
<select name ="gender" id = "gender" class="form-control-kyc">
<option value="male"> Male </option>
<option value="female"\> Female </option>
</select>
</div>
</div>
<div class="col-md-12 marT30">
<div class="form-group-kyc">
<input type="submit"
id="submit" name = "submit" class="itg-button" value="Submit">
</div>
</form>
I want to Know, when the user fails to submit the form, how can I retain the form values, so user don't have to refill it.
Also if user forgets to fill one value , How to focus on that particular Field.?
ALSO
when the form is submitted and when I refresh The page, the form is again Refilled and data is sent again.
How can I prevent that?
One option is to redirect to another page after the submission, but again when the user will press back button, the form data is sent again.
Is there anything here which can be done?
For retain the filled values of the fields.
<input type="text" name="name" id="name" placeholder="Full Name" class="form-control-kyc" value="<?php echo if(isset($_POST['$name'])? $_POST['name'] : ''; ) ?>">
To avoid the auto resubmission of the data you can check whether the submit button pressed or not, if not the form data will not be added to Database and redirect it to index.php
<?php
if(isset($_POST['your-submit-button-name'])){
//within this curly brackets add your codes
}else{
header('Location: index.php');
die();
}
?>
If you want to keep the values stored you can simply echo the post value back as shown below.
<input type="text" name="name" id="name" placeholder="Full Name" class="form-control-kyc" value="<?php echo isset($_POST['name']) ? $_POST['name'] : '' ?>">

php doesn't add a div, it changes the page completely

I am trying to make a contact form in PHP and HTML. The code works but the problem is that is completely changes the page simply ruining the process of having only a single change: the div that goes on the bottom of the form. Here is my php file:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$title = $_POST['title'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$url = $_POST['url'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Partner from Website';
$to = 'jordanwhite916#gmail.com';
$subject = 'VetConnexx Partner Inquiry';
$body = "From: $name\n E-Mail: $email\n Phone: $phone\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
if (!$_POST['title']) {
$errTitle = 'Please enter your title';
}
if (!$_POST['phone']) {
$errPhone = 'Please enter your phone';
}
// 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';
}
if (!$_POST['url'] || !filter_var($_POST['url'], FILTER_VALIDATE_URL)) {
$errURL = 'Please enter a valid website';
}
//Check if message 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 && !$errHuman && !$errTitle && !$errPhone && !$errURL) {
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 later</div>';
}
}
}
?>
<div class="panel panel-default">
<div class="panel-body">
<p>Become a VetConnexx Business Partner.</p>
<br />
<br />
<p>VetConnexx brings the mission focused discipline, integrity, and motivation of the US Armed Forces
to your customers. VetConnexx has been tested by the best and exceeds the standards expected of
Fortune 100 companies and their privately held peers.</p>
<br />
<br />
<p>We can bring the same level of service to your business. To discuss our client services, please
contact us at VetPartners#VetConnexx.com</p>
<br />
<br />
<form class="form-horizontal" role="form" method="post" action="businesspartners.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<div class="form-group">
<label for="title" class="col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="title" name="title" placeholder="Title" value="<?php echo htmlspecialchars($_POST['title']); ?>">
<?php echo "<p class='text-danger'>$errTitle</p>";?>
</div>
</div>
<div class="form-group">
<label for="phone" class="col-sm-2 control-label">Phone</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="phone" name="phone" placeholder="Phone" value="<?php echo htmlspecialchars($_POST['phone']); ?>">
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="form-group">
<label for="url" class="col-sm-2 control-label">URL</label>
<div class="col-sm-10">
<input type="url" class="form-control" id="url" name="url" placeholder="www.examplewebsite.com" value="<?php echo htmlspecialchars($_POST['url']); ?>">
<?php echo "<p class='text-danger'>$errURL</p>";?>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message" placeholder="How may we help you?"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.1/js/bootstrap.min.js"></script>
The link to the website I am trying to develop is located here: Go to Business Partners, and fill out the information in the contact form to see what may be wrong. I only want the div on the bottom to show after the Send button is click, not for a completely white page with black text and forms to come up that has the same content. Here's the link to the website:
http://www.sampsonvision.com/VetConnexxWebsite
I suggest you want to use AJAX call to your PHP file instead of a simple submission. Because after form submission, the only content appears on a page is that one which was generated by the script. Your script outputs only one div.

Wordpress PHP validation using Modal Contact form

Hi im just new to wordpress i'm just confused on how to link my php validation in wordpress.
i have a send.php in my root folder located in wordpress\wp-content\themes\myTheme. my problem is that whenever i click submit in my contact form. it redirects me to the pa Object not found which in fact the send.php is located. am i doing something wrong? or i linked it incorrectly? here is my code for my Modal Contact Form.
<div class = "modal fade" id = "contact" role = "dialog">
<div class = "modal-dialog">
<div class = "modal-content">
<div class = "modal-header">
<h4>Message Me :3 </h4>
</div>
<div class = "modal-body">
<form class="form-horizontal" name="commentform" method="post" action="send.php">
<div class="form-group">
<label class="control-label col-md-4" for="first_name">First Name</label>
<div class="col-md-6">
<input type="text" class="form-control" id="first_name" name="first_name" placeholder="First Name"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" for="last_name">Last Name</label>
<div class="col-md-6">
<input type="text" class="form-control" id="last_name" name="last_name" placeholder="Last Name"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" for="email">Email Address</label>
<div class="col-md-6 input-group">
<span class="input-group-addon">#</span>
<input type="email" class="form-control" id="email" name="email" placeholder="Email Address"/>
</div>
</div>
<div class="form-group">
<label class="control-label col-md-4" for="comment">Question or Comment</label>
<div class="col-md-6">
<textarea rows="6" class="form-control" id="comments" name="comments" placeholder="Your question or comment here"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-md-6">
<button type="submit" value="Submit" class="btn btn-primary pull-right">Send</button>
</div>
</div>
</form>
</div>
and here is my send.php code
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = "ccmanere#gmail.com";
$email_subject = "Your email subject line";
function died($error) {
// your error code can go here
echo "We are very sorry, but there were error(s) found with the form you submitted. ";
echo "These errors appear below.<br /><br />";
echo $error."<br /><br />";
echo "Please go back and fix these errors.<br /><br />";
die();
}
// validation expected data exists
if(!isset($_POST['first_name']) ||
!isset($_POST['last_name']) ||
!isset($_POST['email']) ||
!isset($_POST['comments'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$first_name = $_POST['first_name']; // required
$last_name = $_POST['last_name']; // required
$email_from = $_POST['email']; // required
$comments = $_POST['comments']; // required
$error_message = "";
$email_exp = '/^[A-Za-z0-9._%-]+#[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
if(!preg_match($email_exp,$email_from)) {
$error_message .= 'The Email Address you entered does not appear to be valid.<br />';
}
$string_exp = "/^[A-Za-z\s.'-]+$/";
if(!preg_match($string_exp,$first_name)) {
$error_message .= 'The First Name you entered does not appear to be valid.<br />';
}
if(!preg_match($string_exp,$last_name)) {
$error_message .= 'The Last Name you entered does not appear to be valid.<br />';
}
if(strlen($comments) < 2) {
$error_message .= 'The Comments you entered do not appear to be valid.<br />';
}
if(strlen($error_message) > 0) {
died($error_message);
}
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "First Name: ".clean_string($first_name)."\n";
$email_message .= "Last Name: ".clean_string($last_name)."\n";
$email_message .= "Email: ".clean_string($email_from)."\n";
$email_message .= "Comments: ".clean_string($comments)."\n";
// create email headers
$headers = 'From: '.$email_from."\r\n".
'Reply-To: '.$email_from."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message, $headers);
sleep(2);
echo "<meta http-equiv='refresh' content=\"0; url=http://tutsme-webdesign.info/index.php\">";
?>
<?php
}
?>

Ajax not posting form data

I recently asked a question about executing a Send Mail script without a page reload and worked out what my problem was with my AJAX, that's all resolved. I now have an issue with the following:
When my form posts to my AJAX the script is executed, however the post data doesn't seem to be come through to the PHP script.
The link to my question is: Contact Form same page success.
Here is my PHP:
<?php
$name = ($_POST['name']);
$email = ($_POST['email']);
$fsubject = ($_POST['subject']);
$message = ("Name: ". $name . "\nEmail Address: " . $email . "\n\nMessage: " . $_POST['message']);
// Set Mail
$to = "emailaddress#fakeone.com";
$header = 'From: contactus#fakeone.com' . "\r\n" .
'Reply-To: website#fakeone.com';
$subject = "{$fsubject}";
$body = "{$message}";
// Send Mail
if (mail($to, $subject, $body, $header))
{
echo("<p>Message successfully sent!</p>");
}
else
{
echo("<p>Message delivery failed...</p>");
}
?>
My HTML
<div id="success" style="color:red;"></div>
<form action="" id="contactform" method="post">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Your name *</label>
<input type="text" value="" data-msg-required="Please enter your name." maxlength="100" class="form-control" name="name" id="name">
</div>
<div class="col-md-6">
<label>Your email address *</label>
<input type="email" value="" data-msg-required="Please enter your email address." data-msg-email="Please enter a valid email address." maxlength="100" class="form-control" name="email" id="email">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Subject</label>
<input type="text" value="" data-msg-required="Please enter the subject." maxlength="100" class="form-control" name="subject" id="subject">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Message *</label>
<textarea maxlength="5000" data-msg-required="Please enter your message." rows="10" class="form-control" name="message" id="message"></textarea>
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" id="submit" value="Send Message" class="btn btn-info btn-lg" data-loading-text="Loading...">
</div>
</form>
My AJAX
<script>
$(document).ready(function () {
$('#submit').click(function () {
$.post("sendmail.php", $("#contactform").serialize(), function (response) {
$('#success').html(response);
});
return false;
});
});
</script>
When I fill out the forms, nothing comes back it sends an empty email.
Any idea's why the post isn't working on this would be greatly appreciated. If I am breaking any rules by posting another question please let me know!
Regards,
Dan
I think the problem is in your PHP code. You've got a variable named $headers, but in your mail function you're using $header. A typo.
Change the following lines from this -
$subject = "{$fsubject}";
$body = "{$message}";
to this -
$subject = $fsubject;
$body = $message;

Categories

Resources