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();
Related
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
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
I have successfully set up a php script for my contact form on my website but I have recently found out that my server provider does not accept php. Instead I have use SMTP.
Can anyone help me as this is new to me. I've attempted using other scripts but I cannot implement it.
This is my php code:
<?php
/* Set e-mail recipient */
$myemail = "mail#louisreed.co.uk";
/* Check all form inputs using check_input function */
$name = check_input($_POST['name'], "Your Name");
$email = check_input($_POST['email'], "Your E-mail Address");
$subject = check_input($_POST['subject'], "Message Subject");
$message = check_input($_POST['message'], "Your Message");
/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email))
{
show_error("Invalid e-mail address");
}
/* Let's prepare the message for the e-mail */
$subject = "Someone has sent you a message";
$message = "
Someone has sent you a message using your contact form:
Name: $name
Email: $email
Subject: $subject
Message:
$message
";
/* Send the message using mail() function */
mail($myemail, $subject, $message);
/* Redirect visitor to the thank you page */
header('Location: http://louisreed.co.uk');
exit();
/* Functions we used */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}
function show_error($myError)
{
?>
<html>
<body>
<p>Please correct the following error:</p>
<strong><?php echo $myError; ?></strong>
<p>Hit the back button and try again</p>
</body>
</html>
<?php
exit();
}
?>
My HTML form:
<div class="row">
<div class="col-lg-8 col-lg-offset-2">
<form role="form" action="http://www.louisreed.co.uk/includes/mailer.php" method="post">
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="name">Name</label>
<input class="form-control" type="text" id="name" name="name" placeholder="Name">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="email">Email Address</label>
<input class="form-control" type="email" id="email" name="email" placeholder="Email Address">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="subject">Subject</label>
<textarea placeholder="Subject" class="form-control" id="subject" name="subject" rows="1"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="message">Message</label>
<textarea placeholder="Message" class="form-control" id="message" name="message" rows="5"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-lg btn-primary">Send</button>
</div>
</div>
</form>
</div>
</div>
Any suggestions welcome!
Thanks! :)
Louis
Oke I checked a few thinks. I guess you could atleast try the following. I know your host doesn't support php which find hard to believe tough. So maybe something else is wrong. What is the host you are using?
<?php
if($_POST) {
//Strip user input
function sanitize($value) {
//Escape the user input and then strip all html tags
//mysql_* is not recommended but I guess you don't store data in a database
$value = mysql_real_escape_string(strip_tags($value));
//Return the sanitized user input
return $value;
}
//To display all errors
function errors($error) {
echo '<ul class="error">';
foreach($error as $fail) {
echo '<li>'.$fail.'</li>';
}
echo '</ul>';
}
//All the input fields
$name = sanitize($_POST['name']);
$email = sanitize($_POST['email']);
$subject = sanitize($_POST['subject']);
$message = sanitize($_POST['message']);
//Check if there are no empty fields
if(!empty($name), !empty($email) && !empty($subject) && !empty($message)) {
if(strlen($name) <= 3) {
$error[] = 'Name '.$name.' is too short';
}
if(strlen($email) <= 3) {
$error[] = 'Email '.$email.' is too short';
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$error[] = 'Email '.$email.' is not a valid email';
}
if(strlen($subject) <= 3) {
$error[] = 'Subject '.$subject.' is too short';
}
if(strlen($message) <= 8) {
$error[] = 'Message is too short';
}
//If there are no errors
if(empty($error)) {
$to = "mail#louisreed.co.uk"; //The email you want to send it to
//Subject already set
$headers = "FROM: My site"; //Sets the headers
//The message for the email
$message = "Someone has sent you a message using your contact form:\r\n\r\nName: ".$name."\r\nEmail: ".$email."\r\nSubject: ".$subject."\r\n\r\nMessage: ".$message;
$mail = mail($to, $subject, $message, $headers);
//If the mail has been send
if($mail) {
header('Location: http://louisreed.co.uk/index.php?success');
exit();
}
}
} else {
$error[] = 'There are empty fields';
}
//If there are errors show them
if(!empty($error)) {
echo errors($error);
}
}
//If issset success (from the header()) display a message
if(isset($_GET['success'])) {
echo '<p>Thank you for your message</p>';
}
?>
<form role="form" action="" method="post">
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="name">Name</label>
<input class="form-control" type="text" id="name" name="name" placeholder="Name">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="email">Email Address</label>
<input class="form-control" type="email" id="email" name="email" placeholder="Email Address">
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="subject">Subject</label>
<textarea placeholder="Subject" class="form-control" id="subject" name="subject" rows="1"></textarea>
</div>
</div>
<div class="row">
<div class="form-group col-xs-12 floating-label-form-group">
<label for="message">Message</label>
<textarea placeholder="Message" class="form-control" id="message" name="message" rows="5"></textarea>
</div>
</div>
<br>
<div class="row">
<div class="form-group col-xs-12">
<button type="submit" class="btn btn-lg btn-primary">Send</button>
</div>
</div>
</form>
So try the exact code I gave you above just copy and past it over the form code you have now. You can leave the rest of your code as is. After that save the complete file as index.php. After that remove the index.html file from your FTP. If you have done that you can upload the index.php file now check what you get. If there are still some errors please let me know