i have this contact form and php file to submit form, but in this, when i submit form it displays script alert msg about success and failure but i need to go back where form fields are still filled with info. instead of this i want that form should show alert on same page and form field shoud be cleared. how to do it? i am very beginner in php. dont know where to update code and how.
<?php
if (isset($_POST['name']) && isset($_POST['email']) && isset($_POST['tel']) && isset($_POST['loc']) && isset($_POST['message'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$tel = $_POST['tel'];
$loc = $_POST['loc'];
$message = $_POST['message'];
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Kindly provide valid Email Address.";
} else {
$body = $name . "\n" . $email . "\n" . $tel . "\n" . $loc . "\n" . $message;
/* if (mail('[email protected]', 'Service Enquiry', $body, 'From:' . $email)) {
echo (mail('[email protected]', 'Service Enquiry', $body, 'From:' . $email))."Thanks for contacting us.";
}*/
if (mail('[email protected]', 'Service Enquiry', $body, 'From:' . $email)) {
echo "Thanks for contacting us.";
}
else {
echo "<script>alert('Sorry!, there is a problem in sending email. Please call us on +61 452 534 200')</script>";
}
}
} else {
echo 'Message could not be sent.';
}
$curlx = curl_init();
curl_setopt($curlx, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
curl_setopt($curlx, CURLOPT_HEADER, 0);
curl_setopt($curlx, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curlx, CURLOPT_POST, 1);
$post_data =
[
'secret' => '6LfRXOEhAAAAAOWyRGugvbPdYOhkztlz1aMcEwKP', //<--- your reCaptcha secret key
'response' => $_POST['g-recaptcha-response']
];
curl_setopt($curlx, CURLOPT_POSTFIELDS, $post_data);
$resp = json_decode(curl_exec($curlx));
curl_close($curlx);
if ($resp->success)
{
echo "<script>alert('Thanks For connecting us..we will get back to you soon..')</script>";
} else
{
// failed
echo "<script>alert('Please verify you are human..')</script>";
exit;
}
?>
<div class="col-md-6 padding-15">
<div class="contact-form">
<!-- https://brickboys.com.au/contact.php -->
<form action ="https://brickboys.com.au/contact.php"
method="post" id="ajax_form" class="form-horizontal">
<div class="form-group colum-row row">
<div class="col-sm-6">
<input type="text" id="name" name="name" class="form-control" placeholder="Name" required>
</div>
<div class="col-sm-6">
<input type="email" id="email" name="email" class="form-control" placeholder="Email" required>
</div>
</div>
<div class="form-group colum-row row">
<div class="col-sm-6">
<input type="tel" id="tel" name="tel" class="form-control" pattern="[0-9]{10}" placeholder="Phone 0452534200" required>
</div>
<div class="col-sm-6">
<input type="text" id="loc" name="loc" class="form-control" placeholder="Project Location">
</div>
</div>
<div class="form-group row">
<div class="col-md-12">
<textarea id="message" name="message" cols="30" rows="5" class="form-control message" placeholder="Message" required></textarea>
</div>
<div class="g-recaptcha col-sm-6 mt-2" data-sitekey="6LfRXOEhAAAAAJMek8MJn-kTKIzO-AKfg7JIp3zu"></div>
</div>
<div class="form-group row">
<div class="col-md-12">
<button id="submit" class="default-btn" type="submit">Send Message</button>
</div>
</div>
<div id="form-messages" class="alert" role="alert"></div>
</form>
</div>
</div>
</div>
</div>
</section>
<!-- begin snippet: js hide: false console: true babel: false -->
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
HTML (English "hyper text markup language" - hypertext markup language) is a special markup language that is used to create sites on the Internet.
Browsers understand html perfectly and can interpret it in an understandable way. In general, any page on the site is html-code, which the browser translates into a user-friendly form. By the way, the code of any page is available to everyone.
https://www.w3.org/html/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.