Hi there,
I have this form which works, but the success/error messages display on a new page.
Is there a way I can have them display on the same page/under the form?
This is my HTML:
<form id="contactform" method="post" action="mailer.php">
<div class="form-group">
<label for="name">Name</label>
<input type="text" class="form-control" id="name" name="username" placeholder="Enter your name">
</div>
<div class="form-group">
<label for="phone">Phone</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="Enter your phone number">
</div>
<div class="form-group">
<label for="email">Email</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Enter your email address">
</div>
<div class="form-group">
<label for="message">Message</label>
<textarea class="form-control" id="message" name="message" rows="4" placeholder="Enter your email message"></textarea>
<div class="g-recaptcha" data-sitekey="SITEKEY"></div>
</div>
<div id="formresult"> </div>
<button type="submit" name="submit" class="btn btn-info">Submit</button>
<button type="reset" class="btn btn-danger">Reset</button>
</form>
Then I have the following in mailer.php:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {}
$name = trim($_POST["username"]);
$phone = trim($_POST["phone"]);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
if(isset($_POST['g-recaptcha-response'])){
$captcha = $_POST['g-recaptcha-response'];
}
//Validate the data
if (empty($name) OR empty($phone) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($message) OR empty($captcha)) {
http_response_code(400);
echo " Please fill all the form inputs and check the captcha to submit.";
exit;
}
//recipient email address.
$recipient = "me@gmail.com";
$subject = "New message from $name";
//email content.
$email_content = "Name: $name\n";
$email_content .= "Phone: $phone\n\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
//email headers
$email_headers = "From: $name <$email>";
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRET&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
$decoded_response = json_decode($response, true);
if($decoded_response['success'] == true){
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
http_response_code(200);
echo " Thank You! Your message has been sent.";
} else {
http_response_code(500);
echo "Whoa! message could not be sent.";
}
} else {
http_response_code(400);
echo 'You are a spammer!';
}
?>
Thank you
SamA74
October 5, 2019, 8:17pm
2
By the nature of PHP form processing there has to be a page refresh, there is no way of getting away from that.
If you want the same form page displayed again after submission with results, you will make the action
the same page and include the processing script at the beginning of the file.
Instead of echoing the result message within the processor as you have, store it as a variable which can be output in the html form page. Of course accounting for there not being a message in the initial form.
Thanks for the reply.
I have now tried using the page as its own action, so action="index.php"
, but when I view the page, it just displays a blank page with “Please fill all the form inputs and check the captcha to submit.”
Don’t use OR, use || in this case. Your not going to get the results you might think you are. It has to do with operator precedence.
<?php
$bool = FALSE || TRUE;
var_dump($bool);
$bool = FALSE OR TRUE;
var_dump($bool);
2 Likes
Thanks for the reply.
Are you referring to this line?
if (empty($name) || empty($phone) || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($message) || empty($captcha)) {
Yes, except that’s not how you posted it.
if (empty($name) OR empty($phone) OR !filter_var($email, FILTER_VALIDATE_EMAIL) OR empty($message) OR empty($captcha))
That is what I changed it to, but still it shows the blank page with just the message
SamA74
October 6, 2019, 9:19am
8
Can we see the latest version?
Sure, this is the latest version:
<?php
if ($_SERVER["REQUEST_METHOD"] == "POST") {}
$name = trim($_POST["username"]);
$phone = trim($_POST["phone"]);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$message = trim($_POST["message"]);
if(isset($_POST['g-recaptcha-response'])){
$captcha = $_POST['g-recaptcha-response'];
}
//Validate the data
if (empty($name) || empty($phone) || !filter_var($email, FILTER_VALIDATE_EMAIL) || empty($message) || empty($captcha)) {
http_response_code(400);
echo " Please fill all the form inputs and check the captcha to submit.";
exit;
}
//recipient email address.
$recipient = "me@gmail.com";
$subject = "New message from $name";
//email content.
$email_content = "Name: $name\n";
$email_content .= "Phone: $phone\n\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Message:\n$message\n";
//email headers
$email_headers = "From: $name <$email>";
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRET&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
$decoded_response = json_decode($response, true);
if($decoded_response['success'] == true){
// Send the email.
if (mail($recipient, $subject, $email_content, $email_headers)) {
http_response_code(200);
echo " Thank You! Your message has been sent.";
} else {
http_response_code(500);
echo "Whoa! message could not be sent.";
}
} else {
http_response_code(400);
echo 'You are a spammer!';
}
?>
SamA74
October 6, 2019, 10:06am
10
I think the bracket is closing a bit too soon.
Also:-
That’s what that will do.
Put the messages into a variable as I said.
system
Closed
January 5, 2020, 5:06pm
11
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.