Contact form php inside html - php

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

Related

PHP form fills with ones after submission

I am stumped....
I have created a PHP form to gather some user information and email it to the site owner, after the form is submitted, the fields auto complete with a '1' I am assuming this means the field was true and submitted.
Does anyone have any suggestions on how to hide or remove the 1 after the form is submitted? I just want the form to be blank after submission. Here is the code, Sorry for the poor formatting and eye bleeding wall of code.
Thank you in advance for any help!
PHP Code
$errName = "";
$errcatBreed = "";
$errEmail = "";
$errMessage = "";
$result = "";
if (isset($_POST['submit'])) {
$name = $_POST['name'];
$catBreed = $_POST['catBreed'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'PearTreeHill Contact Form';
$to = 'test#domain.com';
$subject = "New furbaby enquiry from $name";
$body ="From: $name\n Cat Breed: $catBreed\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if cat breed has been entered
if (!$_POST['catBreed']) {
$errcatBreed = 'Please enter either Ragdoll or British ShortHair';
}
// 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';
}
// RECAPTCHA
if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response']))
{
$secret = 'Private Key';
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);
if($responseData->success)
{
$succMsg = 'Your contact request have submitted successfully.';
}
else
{
$errMsg = 'Robot verification failed, please try again.';
}
}
// If there are no errors, send the email
if (!$errName && !$errcatBreed && !$errEmail && !$errMessage) {
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.</div>';
}
}
}
HTML Code
<!-- contact form -->
<section id="contactUs" class="bg">
<div class="container">
<div class="row">
<div class="col-lg-8 mx-auto">
<div class="text-center">
<h2 class="w3-tangerine">Contact Us</h2>
</div>
<!-- start of entry form -->
<form class="form-horizontal" role="form" method="post" action="index.php">
<!-- name entry -->
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name" value="<?php echo htmlspecialchars(isset($_POST['name'])); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
</div>
<!-- cat breed selection -->
<div class="form-group">
<label for="catBreed" class="col-sm-2 control-label">Cat Breed</label>
<div class="col-sm-12">
<input type="text" class="form-control" id="catBreed" name="catBreed" placeholder="'Ragdoll' or 'British ShortHair'" value="<?php echo htmlspecialchars(isset($_POST['catBreed'])); ?>">
<?php echo "<p class='text-danger'>$errcatBreed</p>";?>
</div>
</div>
<!-- email address entry -->
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-12">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="<?php echo htmlspecialchars(isset($_POST['email'])); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<!-- Body of message -->
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-12">
<textarea class="form-control" rows="4" name="message" placeholder="Please enter any other information"><?php echo htmlspecialchars(isset($_POST['message']));?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<!-- reCAPTCHA -->
<div class="g-recaptcha" data-sitekey="Public Key" data-callback="recaptcha_callback"></div>
<!-- send button -->
<input disabled="disabled" id="submit" name="submit" type="submit" value="Send Message" class="btn btn-danger">
<label for="submit" class="col-sm-8 control-label">Please allow up to 48 hours for a response!</label>
</div>
</div>
<!-- entry alert -->
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
<?php echo htmlspecialchars(isset($_POST['email'])); ?>
Here you display the result of isset() (TRUE or FALSE).
You should do :
<?php if ( isset($_POST['email']) ) echo htmlspecialchars($_POST['email']); ?>
And this for each field :-)
change
echo htmlspecialchars(isset($_POST['name']));
to
echo htmlspecialchars ($_POST['name'])
I mean remove isset();

PHP form validation not working on submit

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");
}

What action should my Contact Form have, I am using Angular Routing to get contact.html

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>

Bootstrap PHP Form Not Sending

I am trying to create a very simple contact form for my personal website. This form worked perfect for another website I did so I'm not sure what the problem is here. I've spent the last few hours trying to figure it out and dipping into other form tutorials so its strayed from the original code. I guess the biggest difference between this and the other website is that this is in bootstrap.
I have three problems with this.
1) Is there anyway not to jump up to the top of the screen when I submit the form? I believe this has something for making the action the page itself.
2) Is there any way to not need the 'subject' variable for the mail function? I'd love to not have the subject input on my form.
3) The biggest problem is that while the form runs, I have not received any emails from the form.
The php before html on same doc:
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Contact Form';
$to = 't*************#gmail.com';
$subject = $_POST['subject'];
$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>';
}
}
}
?>
The html/form:
<form class="form-horizontal" role="form" method="post"
action="pauline.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="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="name" class="col-sm-2 control-
label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="subject"
name="subject" placeholder="Message Subject" value="<?php echo
htmlspecialchars($_POST['subject']); ?>">
<?php echo "<p class='text-danger'>$errSubject</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 have your issues resolved with example code. See code comments for exact edits
This is just a matter of setting an anchor to jump the back page
down on load
Just set the subject to a blank, and not
have any html for the subject
This was fixed on its own by cleaning up your undefined errors. Note
the issets() I used with shorthand if statements $if?$then:$else to define the variables as blank if there is nothing coming
from $_POST
I recommend you take care of your format needs on top to keep your code as organized as you can. Also use the variables you defined as opposed to calling $_POST repeatedly like your example.
<?
$name = isset($_POST['name']) ? htmlspecialchars($_POST['name']) : ""; ### shorthand if statements
$email = isset($_POST['email']) ? htmlspecialchars($_POST['email']) : "";
$message = isset($_POST['message']) ? htmlspecialchars($_POST['message']) : "";
$human = isset($_POST['human']) ? intval($_POST['human']) : -1;
$from = 'Contact Form';
$to = 't*************#gmail.com';
$subject = ""; ### send a blank subject. issue 2 solved
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
$errName = $errEmail = $errMessage = $errHuman = $result = ""; ### defining these all as blank to stop the undefined error.
if (isset($_POST["submit"])) {
// Check if name has been entered
if (!$name) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$email || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$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>';
}
}
}
?>
<!DOCTYPE html>
<html>
<head lang="en">
<meta charset="UTF-8">
<title></title>
</head>
<body>
<form class="form-horizontal" role="form" method="post"
action="pauline.php#submit"> <!-- // this solves issue 1 -->
<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 $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 $email; ?>">
<?php echo "<p class='text-danger'>$errEmail</p>"; ?>
</div>
</div>
<!--// BYE BYE SUBJECT!
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Subject</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="subject"
name="subject" placeholder="Message Subject" value="<?php # echo htmlspecialchars($_POST['subject']); ?>">
<?php # echo "<p class='text-danger'>$errSubject</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 $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>
</body>
</html>
1) Is there anyway not to jump up to the top of the screen when I submit the form? I believe this has something for making the action the page itself.
Because you're posting to the page and not providing a redirect, you'll always refresh the page, and you'll always reload the page at the top first. You could provide a Javascript function to scroll to a certain point, or redirect to an anchor tag on the page (though this would involve an unnecessary reload the way you're running it).
2) Is there any way to not need the 'subject' variable for the mail function? I'd love to not have the subject input on my form.
Sure! You can hardcode a subject into your PHP. Just replace your $subject = $_POST['subject']; with $subject = 'Message From Site'. Then remove the input from your form. You've already done this with your $from variable.
3) The biggest problem is that while the form runs, I have not received any emails from the form.
This is a more difficult issue. Emails from PHP scripts can be difficult to get through email provider spam filters. You may need to set some configuration or set specific headers in order to get emails sent to you.The mail() function accepts a set of headers as the fourth parameter, not just a "From" value - tweaking that may help. You can find TONS more info on debugging options aon the PHP docs, located here: http://php.net/manual/en/function.mail.php

Bootstrap email form not working properly. Is it my html or php?

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; // ?

Categories