When submitting my form i get a 404 page. Something is missing, or wrong.
It's a wordpress website. I inserted the email in the options of the wordpress and nothing happen. I saw a lot of stack overflow questions but none helped me. I don't want to use plugins.
<form class="form-horizontal" role="form" method="post" action="contact.php">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" value="">
</div>
<br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-envelope"></i></span>
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="">
</div>
<br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-pencil"></i></span>
<textarea class="form-control" rows="4" name="message" placeholder="Message..."></textarea>
</div>
<br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">2 + 3 = ?</span>
<input type="text" class="form-control" id="human" name="human" placeholder="">
</div>
<br/>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-5">
<input id="submit" name="submit" type="submit" value="Enviar" class="btn btn-button">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<! Will be used to display an alert to the user>
</div>
</div>
</form>
PHP Code:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Contact Form';
$to = 'example#domain.com';
$subject = 'Contact ';
$body = "From: $name\n E-Mail: $email\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';
}
//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) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
Create a .php file in theme (your current theme, if have child theme then in thild theme) then put below code in the file and save it.
<?php
/*
* Template Name: Contact us
*/
get_header();
if (isset($_POST["submit"]))
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Contact Form';
$to = 'example#domain.com';
$subject = 'Contact ';
$body = "From: $name\n E-Mail: $email\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';
}
//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)
{
if (mail ($to, $subject, $body, $from))
{
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
}
else
{
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<form class="form-horizontal" role="form" method="post" action="#">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" value="">
</div><br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-envelope"></i></span>
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="">
</div><br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-pencil"></i></span>
<textarea class="form-control" rows="4" name="message" placeholder="Message..."></textarea>
</div><br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">2 + 3 = ?</span>
<input type="text" class="form-control" id="human" name="human" placeholder="">
</div><br/>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-5">
<input id="submit" name="submit" type="submit" value="Enviar" class="btn btn-button">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<!-- Will be used to display an alert to the user -->
</div>
</div>
</form>
</main>
<?php get_sidebar( 'content-bottom' ); ?>
</div>
<?php get_footer(); ?>
Now go to wp-admin and create new page (title may be contact page etc.), select page template "Contact us" from page template dropdown (right side of the page content), publish the page and view the page.
Hope it will help.
First of all thank you for your reply's.
The problem here is the php mail(). I hope i can help someone with this thread.
To reach this conclusion these were the steps:
Tried everything that i saw in stack overflow with the same problem has me;
Install Check Email Plugin;
I did not received any email so i contact my hosting service and they said they will not enable the php mail() to avoid spam.
Solution - When this happen this is the best way to work around.
In action use file location url:
action="`<?php echo get_template_directory()'contact.php';?>`"
Try this.
Related
I tried this in my code and I entered the right secret key just didn't want to put it out there but everytime I submit my form even with it checked it shows up with the error that it isn't filled out, without the captacha stuff in the form it works just fine. Can someone please help me fix this issue I would like it to make sure you filled it out before it sends it to my email!!
<h2 class="text-center" id="whatwedo">Contact form</h2><hr class="titlehr"></div></div><br>
<?php
if (isset($_POST["submit"])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$description = $_POST['description'];
$captcha = $_POST['g-recaptcha-response'];
$from = $fullname;
$to = 'mcgarrywebdesign#gmail.com';
$subject = 'Contact Form';
$body = "From: $fullname\n E-Mail: $email\n Phone: $phone\n Subject: $subject\n description: $description";
// Check if name has been entered
if (!$_POST['fullname']) {
$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['phone']) {
$errPhone = 'Please enter your phone number';
}
if (!$_POST['subject']) {
$errSubject = 'Please enter the subject';
}
if (!$_POST['description']) {
$errDescription = 'Please enter the description';
}
if(!$captcha){
$errcaptcha = "Please check the the captcha form";
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errPhone && !$errSubject && !$errDescription && !$errcaptcha) {
$secretKey = "secret key";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
$result = "You are spammer ! Get the #$%K out";
} else {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! We will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}}
?>
<?php echo "<p class='text-danger'>$result</p>";?>
<div class="row">
<div class="col-md-12 col-lg-6"> <form action="contact.php" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Full Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Full Name" name="fullname">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Email Address</label>
<input type="email" class="form-control" id="exampleInputPassword1" placeholder="Enter Email" name="email">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Phone Number</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Phone Number" name="phone">
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
</div>
<div class="col-md-12 col-lg-6">
<div class="form-group">
<label for="exampleInputEmail1">Subject</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Subject" name="subject">
<?php echo "<p class='text-danger'>$errSubject</p>";?>
</div>
<div class="form-group">
<label for="exampleTextarea">Description</label>
<textarea class="form-control" id="exampleTextarea" rows="3" name="description" placeholder="Description"></textarea>
<?php echo "<p class='text-danger'>$errDescription</p>";?>
</div>
<br>
</div></div>
<div class="g-recaptcha text-center mx-auto d-block" data-sitekey="6LdhyXcUAAAAANrj8qTSLKcrbjVX6ij07Dqw0awe"></div>
<?php echo "<p class='text-danger'>$errcaptcha</p>";?>
<div class="row"><div class="col-lg-12"><button type="submit" name="submit" value="send" class="btn btn-primary mx-auto d-block" style="width: 190px !important;height: 60px !important;font-size: 25px;">Submit</button></div></div></form>
</div>
I have an HTML form that links to a php file. When I click submit the form sends and I get an email. However, if the form is empty and I click submit it still redirects to the homepage.
form HTML:
<form class="form-horizontal" role="form" method="post" action="contact.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">
</div>
</div>
<div class="form-group">
<label for="telephone" class="col-sm-2 control-label">Phone</label>
<div class="col-sm-10">
<input type="telephone" class="form-control" id="telephone" name="telephone" placeholder="Phone Number" >
</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" >
</div>
</div>
<div class="form-group">
<label for="dropdown" class="col-sm-2 control-label">Select One:</label>
<div class="col-sm-10">
<select name="select" class="form-control" id="dropdown">
<option>Driver</option>
<option>Advertiser</option>
</select>
</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"></textarea>
</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">
</div>
</div>
</form>
contact.php
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$telephone= $_POST['telephone'];
$select = $_POST['select'];
$message = $_POST['message'];
$from = 'Contact Form';
$to = 'myemail#gmail.com';
$subject = 'Message from Contact Form';
$body ="From: $name\n E-Mail: $email\n Telephone: $telephone\n Select: $select\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 && !$errTel) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
{
// To redirect form on a particular page
header("Location:http://www.mywebsite.co");
}
}
?>
Any idea on why my validation is not working?
Thank you.
you have two extra brackets in yor code, this:
{
// To redirect form on a particular page
header("Location:http://www.mywebsite.co");
}
should just be:
// To redirect form on a particular page
header("Location:http://www.mywebsite.co");
you were closing your if block and opening an empty block.
You may wanna recheck this part:
{
// To redirect form on a particular page
header("Location:http://www.mywebsite.co");
}
Also, you should also use trim function on your POST data.
contact.php
if (isset($_POST["submit"])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$telephone = trim($_POST['telephone']);
/* Other code */
if (!$errName && !$errEmail && !$errMessage && !$errTel) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
} else {
// To redirect form on a particular page
header("Location:http://www.mywebsite.co");
}
I am creating a personal website and I am using Angular JS routing for fast loading. I have a contact link on navbar and once a visitor clicks it. I load the code in contact.html inside index.html.
contact.html code contains PHP code and a HTML form. In this part of code I am using mail() for sending a email to myself once the form is filled by visitor.
When visitor clicks submit button, with incorrect information or incomplete data, the page is suppose to store the data user provided in those input fields and give errors. Contact.html works perfectly fine when I run it by itself on local server.
But when I go to index.html and then click on contact link, contact.html doesn't work as expected. Whenever submit is clicked with incorrect or incomplete data, it reloads the entire contact.html inside index.html instead of throwing errors and storing the previously filled information, as it is suppose to do.
How to make it work properly?
Here is my code
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'asfsa';
$to = 'gabanimillin#gmail.com';
$subject = 'Message from Millin Gab site ';
$body = "From: $name\n E-Mail: $email\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';
}
//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) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
<form class="form-horizontal" role="form" method="post" action="">
<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="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="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message">
<?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>
I am trying to include a contact form with validation inside my html file with php. I would like the answer to be in the same page without redirecting to another one. I have added the php snippet before the <!DOCTYPE html> but on the top of my page it displays the error message
Also inside the contact forms i am getting tags <?php echo htmlspecialchars($_POST['name']); ?>
and such. I am new to php so i would need your help
Here's the code:
<?php
if (isset($_POST["submit"]))
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'example#domain.com';
$subject = 'Message from Contact Demo ';
$body ="From: $name\n E-Mail: $email\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';
}
//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)
{
if (mail ($to, $subject, $body, $from))
{
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
}
else
{
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
?>
<div class="col-md-6 col-md-offset-3">
<form class="form-horizontal" role="form" method="post" action="#">
<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="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="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"><?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>
in the action="#" i included the # so that it stays on the same page
forgot to mention that when i complete the form it says cannot post
If you look at "View source" you will likely see the < ?php-tags, meaning PHP is not executed.
Check the extension of your file (it has to be X.php where x is the name you want) and make sure your webserver supports PHP.
If you are developing locally. Make sure you have PHP installed.
Take a look at http://www.php.net to download and install PHP for your operating system. If you are using windows, I would advise installing WAMP. You can find it here
Is there anything wrong with either my php or my html for my email form? Every time I submit the form online I get a blank page. I'm not sure what else I would have to change since it seems to make sense but I don't know very much about php.
<form class="form-vertical" role="form" method="post" action="index.php">
<fieldset>
<!-- Text input-->
<div class="form-group">
<label class="control-label" for="textinput">Name</label>
<input id="textinput" name="textinput" type="text" placeholder="Enter Name" class="form-control input-md">
</div>
<!-- Text input-->
<div class="form-group">
<label class="control-label" for="Email">Email</label>
<input id="Email" name="Email" type="text" placeholder="Enter Email" class="form-control input-md">
</div>
<!-- Text input-->
<div class="form-group">
<label class="control-label" for="Phone">Phone</label>
<input id="Phone" name="Phone" type="text" placeholder="" class="form-control input-md">
</div>
</section>
</div> <!-- end col-md-4 -->
<div class="col-md-4 message-form">
<section class="wow zoomInUp" data-wow-duration="1.5s" data-wow-delay="0.2s" style="visibility: visible; -webkit-animation-delay: 0.2s; -moz-animation-delay: 0.2s; animation-delay: 0.2s;">
<!-- Textarea -->
<div class="form-group">
<label class="control-label" for="Message">Message</label>
<textarea class="form-control" rows="5" id="Message" name="Message" placeholder="Message"></textarea>
</div>
<!-- Button -->
<div class="form-group">
<label class="control-label" for="Submit"></label>
<button id="Submit" name="Submit" class="btn btn-primary pull-right">Send Message</button>
</div>
</fieldset>
</form>
PHP script
<?php
if ($_POST["Submit"]) {
$textinput = $_POST['textinput'];
$Email = $_POST['Email'];
$Phone = $_POST['Phone'];
$Message = $_POST['Message'];
$from = 'Demo Contact Form';
$to = 'max.henry.campos#gmail.com';
$subject = 'Message from Contact Demo ';
$body = "From: $textinput\n E-Mail: $Email\n Message:\n $Message";
// Check if name has been entered
if (!$_POST['textinput']) {
$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 (!$errtextinput && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! We will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
Well, you probably get the blank page because you're not actually returning anything to the browser. You're just setting $result, not outputting it.
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! We will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
echo $result; // ?