Custom Form in WordPress Page Won't Submit/Refresh - javascript

I had a normal site that I built with custom HTML/CSS/JS. I changed the site to become a WordPress site (and made my custom html/css/js into a custom WordPress theme). I had a contact form on the homepage of the site, which broke when I made the change to WordPress.
When the form was functioning, it would refresh the page and bring the user back down to the form (using the anchor id=contactphilly) and then the user would see a message confirming their email was sent.
I did a LOT of Googling and I think it has something to do with the "action" option in the form, but no matter what I try changing it to, it doesn't work. I tried the following for the action value:
<?php the_permalink(); ?>/#contactphilly
#contactphilly
<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>
$_SERVER['REQUEST_URI']
None of them worked.
Here is the code for the form:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'WebKeep.com';
$to = 'info#webkeepteam.com';
$subjectemail = 'Message from WebKeepTeam.com Contact Form ';
$body = "From: $name\n E-Mail: $email\n Subject: $subject\n Message:\n $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 message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subjectemail, $body, $from)) {
$result='<div>Thank You! We will respond shortly.</div>';
} else {
$result='<div>Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
and
<form name="contactform" method="post" action="#contactphilly">
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<fieldset class="form-group">
<input type="text" value="<?php echo $name;?>" class="form-control transparent-input" id="name" name="name" placeholder="Name">
</fieldset>
<?php echo "<p class='text-danger'>$errName</p>";?>
<fieldset class="form-group">
<input type="email" value="<?php echo $email;?>" class="form-control transparent-input" id="email" name="email" placeholder="Email">
</fieldset>
<?php echo "<p class='text-danger'>$errEmail</p>";?>
<fieldset class="form-group">
<input type="text" class="form-control transparent-input" value="<?php echo $subject;?>" id="subject" name="subject" placeholder="Subject">
</fieldset>
</div>
<div class="col-lg-6 col-md-6 col-sm-12 col-xs-12">
<fieldset class="form-group">
<textarea class="form-control transparent-input" id="message" value="<?php echo $message;?>" name="message" rows="6" placeholder="Message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
</fieldset>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-index pull-right">
</div>
<?php echo $result; ?>
</form>

I found an answer to my own question! I completely forgot about this bit of code on GitHub: https://github.com/dwyl/html-form-send-email-via-google-script-without-server I used it elsewhere on the site and it worked, and it also works beautifully for forms within pages too!

Related

mouse click not working on google recaptcha checkbox part of form

I have a issue with a form that has v2 checkbox for I'm not a robot, the mouse clicks don't seem to be working any more when trying to check the I'm not a robot checkbox, I can tab to it and press the enter key and it works but the google recaptcha checkbox is not detecting the mouse click any more for some reason. It's a new issue on me, has anyone else had this issue and how can I solve it as I don't understand this issue as was working all ok up until now
Thank you in advance, I can provide code but as am unsure what the issue is, I didn't want to post a lot of code that was not needed to post, below is the code I have
<?php
$postData = $statusMsg = '';
$status = 'error';
// If the form is submitted
if(isset($_POST['submitfooterform'])){
$postData = $_POST;
// Validate form fields
if(!empty($_POST['name']) && !empty($_POST['email']) &&
!empty($_POST['phone']) && !empty($_POST['message'])){
// Validate reCAPTCHA box
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-
recaptcha-response'])){
// Google reCAPTCHA API secret key
$secretKey = '';
// Verify the reCAPTCHA response
$verifyResponse =
file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secretKey.'&response='.$_POST['g-recaptcha-response']);
// Decode json data
$responseData = json_decode($verifyResponse);
// If reCAPTCHA response is valid
if($responseData->success){
// Posted form data
$name = !empty($_POST['name'])?$_POST['name']:'';
$email = !empty($_POST['email'])?$_POST['email']:'';
$phone = !empty($_POST['phone'])?$_POST['phone']:'';
$message = !empty($_POST['message'])?$_POST['message']:'';
// Send email notification to the site admin
$to = '';
$subject = 'New Website Enquiry';
$htmlContent = "
<h3>Contact Form Details</h3>
<br><br>
<p><b>Name: </b>".$name."</p>
<p><b>Email: </b>".$email."</p>
<p><b>Phone: </b>".$phone."</p>
<p><b>Message: </b>".$message."</p>
";
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From:'.$name.' ' . "\r\n";
// Send email
#mail($to,$subject,$htmlContent,$headers);
$status = 'success';
$statusMsg = header("Location: ../enquiry-confirmation.php");
$postData = '';
}else{
$statusMsg = "<strong>" . 'Robot verification failed, please try again.' . "</strong>";
}
}else{
$statusMsg = "<strong>" . 'Please check on the reCAPTCHA box.' . "</strong>";
}
}else{
$statusMsg = 'Please fill all the mandatory fields.';
}
}
?>
<form class="needs-validation" action="includes/footer.php" method="post" name="contact-form">
<div class="form-row">
<div class="col-md-6 mb-3">
<input type="text" class="form-control" placeholder="Name" required name="name" value="<?php echo !empty($postData['name'])?$postData['name']:''; ?>">
</div>
<div class="col-md-6 mb-3">
<input type="text" class="form-control" placeholder="Email Address" required name="email" value="<?php echo !empty($postData['email'])?$postData['email']:''; ?>">
</div>
<br><br>
<div class="col-md-12 mb-3">
<input type="text" class="form-control" placeholder="Phone Number" name="phone" value="<?php echo !empty($postData['phone'])?$postData['phone']:''; ?>">
</div>
<div class="col-md-12 Textarea-btn">
<textarea class="form-control mb-3" id="exampleFormControlTextarea1" rows="5" placeholder="Your message..." name="message" required><?php echo !empty($postData['message'])?$postData['message']:''; ?></textarea>
</div>
<div class="col-md-12 mb-3">
<div class="g-recaptcha" data-callback="recaptchaCallbackftr" data-sitekey=""></div>
</div>
<div class="col-md-12 Textarea-btn">
<button id="submitBtnftr" class="btn btn-dark btn-dark-animated" type="submit" name="submitfooterform" disabled>Send Message</button>
</div>
</div><!-- Form Row /-->
</form>

Need to process a contact form

I purchased a theme, and I need to make a processing.php for my contact form. The form that sends the data to the server for processing.
This is the form code:
<div class="form_error text-center">
<div class="name_error hide error">Please Enter your name</div>
<div class="email_error hide error">Please Enter your Email</div>
<div class="email_val_error hide error">Please Enter a Valid Email Address</div>
<div class="message_error hide error">Please Enter Your Message</div>
</div>
<div class="Sucess"></div>
<form role="form">
<div class="row">
<div class="col-md-4">
<input type="text" class="form-control" id="name" placeholder="Name">
<input type="email" class="form-control" id="email" placeholder="Email">
<input type="text" class="form-control" id="subject" placeholder="Subject">
</div>
<div class="col-md-8">
<textarea class="form-control" id="message" rows="25" cols="10" placeholder=" Message Texts..."></textarea>
<button type="button" class="btn btn-default submit-btn form_submit">SEND MESSAGE</button>
</div>
</div>
</form>
Both shubham and adi's answers are vulnerable to SMTP header injections, allowing your contact form to be used to send spam. It will happen.
Any POST or GET data passed to the additional_headers parameter needs to be sanitised to prevent abuse.
See the following:
http://php.net/manual/en/function.mail.php
https://www.phpsecure.info/v2/article/MailHeadersInject.en.php
see this example
<?php
if(isset($_POST['submit'])){
$to = "email#example.com"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $first_name . " " . $last_name . " wrote the following:" . "\n\n" . $_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.
}
?>
<!DOCTYPE html>
<head>
<title>Form submission</title>
</head>
<body>
<form action="" method="post">
First Name: <input type="text" name="first_name"><br>
Last Name: <input type="text" name="last_name"><br>
Email: <input type="text" name="email"><br>
Message:<br><textarea rows="5" name="message" cols="30"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>
<?php
if (isset($_POST['contact_name'])&&isset($_POST['contact_mail'])&&isset($_POST['message'])) {
$contact_name = $_POST['contact_name'];
$contact_mail = $_POST['contact_mail'];
$message = $_POST['message'];
if (!empty($contact_name) && !empty($contact_mail) && !empty($message)) {
$to = 'your email#gmail.com'; // this is where you want to send the email
$subject = 'Contact form Submitted';
$body = $contact_name."\n".$message;
$headers = 'From:'.$contact_mail; // email of the sender
if (#mail($to, $subject, $body, $headers)) {
echo 'Thanks for contacting us';
}
else {
echo 'Sorry, an error has been occurred';
}
}
else {
echo 'All fields are required';
}
}
?>
<form action="index.php" method="POST">
Name:<br>
<input type="text" name="contact_name"></input><br><br>
Email Address:<br>
<input type="text" name="contact_mail"></input><br><br>
Message:<br>
<textarea name="message" cols="30" rows="6"></textarea><br><br>
<input type="submit" value="Send"></input>
</form>
maybe this codes can help you

PHP contact form will just refresh the page after submission.

After searching for about 3 hours i still can't figure this one out.
I Have a html template with a contact form and im trying to make it work using a PHP script.
I changed the template to PHP and pasted the PHP form script in it. everything is working fine except the confirmation text.
After a successful submission it will just refresh the page instead of printing "Your mail has been sent successfuly ! Thank you for your feedback". i do not want a redirect, i just want it to print on the same page.
Any ideas?
I got a sample of my code.
<form action="<? echo $_SERVER['PHP_SELF']; ?>" id="contact-form" method="post" class="form afsana-form" role="form">
<div class="row">
<div class="col-sm-12 form-group">
<input class="form-control afsana-style" id="name" name="name" placeholder="name" type="text" required autofocus />
</div>
<div class="col-sm-12 form-group">
<input class="form-control afsana-style" id="email" name="email" placeholder="email" type="email" required />
</div>
<div class="col-sm-12 form-group">
<textarea class="form-control" id="message" name="message" placeholder="message" rows="5"></textarea>
</div>
<div class="col-sm-12 form-group">
<button class="btn btn-primary afsana-btn" name="submit" value="verzenden" type="submit">Verzenden <i class="ion-arrow-graph-up-right"></i></button>
</div>
</div>
</form>
<?php
if(isset($_POST["submit"])){
// Checking For Blank Fields..
if($_POST["name"]==""||$_POST["email"]==""||$_POST["message"]==""){
echo "Fill All Fields..";
}else{
// Check if the "Sender's Email" input field is filled out
$email=$_POST['email'];
// Sanitize E-mail Address
$email =filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate E-mail Address
$email= filter_var($email, FILTER_VALIDATE_EMAIL);
if (!$email){
echo "Invalid Sender's Email";
}
else{
$subject = (Contact_form);
$message = $_POST['message'];
$headers = 'From:'. $email . "\r\n"; // Sender's Email
$headers .= 'Cc:'. $email2 . "\r\n"; // Carbon copy to Sender
// Message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// Send Mail By PHP Mail Function
mail("something#domain.com", $subject, $message, $headers);
echo "Your mail has been sent successfuly ! Thank you for your feedback";
}
}
}
?>
First, you have this: $subject = (Contact_form); which should throw an error, so I assume you have error reporting turned off. When developing, you should have error reporting on so you can see errors in your code... Else you are just working blind. I don't mean by throwing tacky error_reporting(0) in every file either, I mean to set your error reporting level to E_ALL in your php.ini.
You also have: $headers .= 'Cc:'. $email2 . "\r\n";
However, $email2 is not defined anywhere, so you would get an error here too.. which is why it's important to test with error reporting on.
See if this works:
<?php
$error = '';
if(isset($_POST['submit']))
{
if ( !empty($_POST['name']) && !empty($_POST['email']) && !empty($_POST['message']) )
{
$email = $_POST['email'];
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
if ( $email = filter_var($email, FILTER_VALIDATE_EMAIL) )
{
$subject = '(Contact_form)';
$message = $_POST['message'];
$headers = 'From:'. $email . "\r\n"; // Sender's Email
$message = wordwrap($message, 70);
if ( $result = mail("something#domain.com", $subject, $message, $headers) ) {
$error = 'Success';
} else {
$error = 'There was an error sending your email!';
}
} else {
$error = 'Invalid Email Address!';
}
} else {
$error = 'Please fill all fields.';
}
}
?>
<p><?= $error ?></p>
<form action="" method="post">
<input type="text" name="name" value="" /><br />
<input type="email" name="email" value="" /><br />
<textarea name="message" rows="5"></textarea><br />
<input type="submit" name="submit" value="submit" />
</form>
Try to put in $subject just a string value like:
$subject = 'Test subject';
change also the following line to this (there is no $email2 defined):
$headers .= 'Cc:'. $email . "\r\n"; // Carbon copy to Sender
and give it a try. You can also put as first line of your code
<?php error_reporting(E_ALL); ?>
and check for errors when submiting the form.

Contact Form mailer.php returns a blank page

The contact form was working fine until I tried to add reCAPTCHA. I have managed to make reCAPTCHA appear, have the recaptchalib , and the mailer.php which is the page displaying a blank page. Any ideas what I am doing wrong?
Here is the mailer.php
<?php
if(isset($_POST['submit'])) {
// check reCAPTCHA information
require_once('recaptchalib.php');
$privatekey = "privatekey";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
// if CAPTCHA is correctly entered!
if ($resp->is_valid) {
// great success!
$myemail = "operations#socialmarketing.com";
/* Check all form inputs using check_input function */
$name = $_POST['inputName'];
$email = $_POST['inputEmail'];
$subject = $_POST['inputSubject'];
$message = $_POST['inputMessage'];
/* Let's prepare the message for the e-mail */
$subject = "Message From LGBT campaign Contact Form";
$message = "
China LGBT Contact Form
Name: $name
Email: $email
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: successPage.html#contact');
} else {
// alert the captcha is not correct
}
}?>
Here is my HTML page, which I have made a .php page
<div class="marketing">
<div class="intro" id="contact">
<h1>Contact Us</h1>
<p>If you would like to stay informed about our progress or would like to help with the campaign, please fill out this form to send us an email.</p>
<div class="panel-body">
<form name="contactform" id="contactform" action="mailer.php" class="form-horizontal" role="form" method="POST">
<div class="form-group">
<label class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="inputName" placeholder="Your Name">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Email</label>
<div class="col-lg-10">
<input type="email" class="form-control" name="inputEmail" placeholder="Your Email">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea class="form-control" rows="4" name="inputMessage" placeholder="Your message..."></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<p>Prove you are not a spambot</p>
<?php require_once('recaptchalib.php');
$publickey = "publickey";
echo recaptcha_get_html($publickey);
?>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<button type="submit" class="btn btn-primary">Send Message</button>
</div>
</div>
</form>
</div>
</div>
Any help with this would be hugely appreciated guys.
Thanks a bunch
SOLUTION
<form name="contactform" id="contactform" action="mailer.php" class="form-horizontal" role="form" method="post">
<div class="form-group">
<label class="col-lg-2 control-label">Name</label>
<div class="col-lg-10">
<input type="text" class="form-control" name="inputName" placeholder="Your Name">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Email</label>
<div class="col-lg-10">
<input type="email" class="form-control" name="inputEmail" placeholder="Your Email">
</div>
</div>
<div class="form-group">
<label class="col-lg-2 control-label">Message</label>
<div class="col-lg-10">
<textarea class="form-control" rows="4" name="inputMessage" placeholder="Your message..."></textarea>
</div>
</div>
<div class="form-group">
<div class="col-lg-10 col-lg-offset-2">
<p>Prove you are not a spambot</p>
<?php require_once('recaptchalib.php');
$publickey = "6Le0ff0SAAAAAOCeQiOcGUwQEfXERDyNJ";
echo recaptcha_get_html($publickey);
?>
</div>
</div>
<div class="form-group">
<div class="col-lg-offset-2 col-lg-10">
<input type="submit" class="btn btn-primary" value="Send Message" name="submit">
</div>
</div>
</form>
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
if(isset($_POST['submit'])) {
// check reCAPTCHA information
require_once('recaptchalib.php');
$privatekey = "6Le0ff0SAAAALTDn4IkqNSN5F0AU2Ezhvf";
$resp = recaptcha_check_answer ($privatekey,
$_SERVER["REMOTE_ADDR"],
$_POST["recaptcha_challenge_field"],
$_POST["recaptcha_response_field"]);
// if CAPTCHA is correctly entered!
if ($resp->is_valid) {
// great success!
$myemail = "kenm#socialmarketing.com";
/* Check all form inputs using check_input function */
$name = $_POST['inputName'];
$email = $_POST['inputEmail'];
$subject = "Message From LGBT campaign Contact Form";
$message = $_POST['inputMessage'];
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
/* Let's prepare the message for the e-mail */
$message = "
China LGBT Contact Form
Name: $name
Email: $email
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: successPage.html#contact');
} else {
// alert the captcha is not correct
echo "captcha did not match!";
exit;
}
}?>
Change your button to this:
<input type="submit" class="btn btn-primary" value="Send Message" name="submit">
POST is looking for a named attribute called submit.
which based on your conditional statement, and nothing will execute inside it because of it:
if(isset($_POST['submit'])) {...}
You also don't have a named form element to go with $subject = $_POST['inputSubject'];
Either add one:
Subject:<input type="text" class="form-control" name="inputSubject" placeholder="Subject">
or simply test with:
$subject = "Form submitted";
You should make sure that all fields are filled. If the subject is left empty or any other, you may not receive mail because of it, especially the Email field.
Another reason may be because you do not have proper headers, including a From:
Visit the PHP.net website on mail:
http://php.net/manual/en/function.mail.php
Example From: header from the website:
$headers = 'From: webmaster#example.com' . "\r\n" .
'Reply-To: webmaster#example.com' . "\r\n" .
'X-Mailer: PHP/' . phpversion();
and modifying mail($myemail, $subject, $message);
to mail($myemail, $subject, $message, $headers);
Quoting them:
Note:
When sending mail, the mail must contain a From header. This can be set with the additional_headers parameter, or a default can be set in php.ini.
Failing to do this will result in an error message similar to Warning: mail(): "sendmail_from" not set in php.ini or custom "From:" header missing. The From header sets also Return-Path under Windows.
Add error reporting to the top of your file(s) which will help find errors.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
// rest of your code
Sidenote: Error reporting should only be done in staging, and never production.
If you're getting a blank page, it's because something is failing and not showing an error. I'd start by making sure that error reporting is enabled in your PHP script. Add this to the top of mailer.php.
ini_set('display_errors',1);
error_reporting(E_ALL);
You also need to add some sort of message here.
} else {
// alert the captcha is not correct
echo "captcha did not match!";
exit;
}
Also as mentioned, check your error log.
You can start by changing
<button type="submit" class="btn btn-primary">Send Message</button>
to
<input type="submit" class="btn btn-primary" value="submit">
This should submit your form to mailer.php. From that point on it should work.

How to make javascript contact form submit correctly

html code:
Contact Me
<label for="Name">Name:</label>
<input type="text" name="name" id="Name" accesskey="N" tabindex="1">
<label for="Email">E-mail:</label>
<input type="text" name="email" id="Email" accesskey="E" tabindex="1">
<label for="Phone">Phone Number:</label>
<input type="text" name="number" id="Number" tabindex="1">
<label for="Comment">Comments</label>
<textarea type="text" name="comment" id="Comment" rows="27" cols="70" tabindex="1"></textarea>
<input id="mySubmit" type="submit" value="Submit">
</form>
</fieldset>
</div>
email.php
<?php
if (isset($_POST['submit'])) {
$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
if (!$email)
echo "<script type='text/javascript'>alert('Please enter a valid email address...');history.back();</script>";
else {
$to = "randomemail#gmail.com"; //change this to YOUR email address
$name = (isset($_POST['name'])) ? $_POST['name'] : "anonymous";
$number = (isset($_POST['number'])) ? $_POST['number'] : "none";
$comment = (isset($_POST['comment'])) ? $_POST['comment'] : "none";
$subject = "Message from $name via contact form";
$message = "Name: $name\nNumber: $number\nEmail: $email\nMessage: $comment";
$from = "From: " . $name . "<" . $email .">\r\n" .
"Reply-To: " . $email ."\r\n" .
"X-Mailer: PHP/" . phpversion();
if (mail($to, $subject, $message, $from))
header("Location: thanks.html");
else
echo "<script type='text/javascript'>alert('An unknown system error has occurred!');history.back();</script>";
}
}
?>
when you submit, it loads email.php, but only a white page, not the thanks.html that it should.
You forgot to name your submit field:
<input id="mySubmit" type="submit" value="Submit" name="submit">
^^^^^^^^^^^^^
Don't use form fields to detect a post. Use the 100% reliable:
if ($_SERVER['REQUEST_METHOD'] == 'POST') { ... }
instead. This will ALWAYS be true if a POST was performed. As you can see with your version, a simple typo or oversight will completely kill your logic.
First Empty your file and just put header("Location: thanks.html"); in your php tags. If it works then add the other lines progressively. you'll see the annoying line. Read about header on PHP reference website. It should be used carefully

Categories

Resources