How to Get Contact Form Working - php

Okay so I am fairly new to web designing. How do I get the contact form on my current theme to work? This is the current html.
I need to know how to code the PHP file; is this correct?
<div class="form row-fluid clearfix">
<div class="field span5">
<label>Your name:</label>
<input type="text" value="" class="req" placeholder="Placeholder text..." />
</div>
<div class="field span5">
<label>Your email:</label>
<input type="email" value="" class="req" />
</div>
<div class="clearfix"> </div>
<div class="field full">
<label>Your comment:</label>
<textarea class="span12" cols="2" rows="7"></textarea>
</div>
<button class="extruded"><span>Submit</span></button>
</div>
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: iclear';
$to = 'sales#tangledindesign.com';
$subject = 'Hello';
?>
And how do I link the PHP file for that contact form?

Step 1
Wrap your fields with a form HTML element that has its action property set to your php processing page
Step 2
Name the form fields according to what the php file expects
Step 3
Add some validation
Step 4
Submit and test
Example
HTML
<form action="process.php" method="post">
First Name: <input type="text" name="first_name">
<input type="submit">
</form>
PHP
<?php
$first_name=$_POST["first_name"];
if($first_name=="John")
{
echo "Hi John!";
}
else
{
echo "Sorry Buddy, Don't really know you";
}
?>
Note
The reason why i did not provide you a full solution is that you mentioned you are a newbie in that programming, and it would be injustice to just solve your problem and not guide you how to do it

You need to wrap your HTML with the tag, and don't forget to get include the submit button:
<form action="process.php" method="post">
<div class="form row-fluid clearfix">
<input type="text" name="name">
<input type="text" name="email">
<input type="text" name="message">
<input type="submit" name="submit">
</div>
</form>
Then here is the php (process.php) file to get all values from your HTML form:
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: iclear';
$to = 'sales#tangledindesign.com';
$subject = 'Hello';
Hope this help.

Try this code
<?php
$toaddress ="youremail#domain.com" //change to your email address
$error ="";
if($_SERVER['REQUEST_METHOD']=="POST") {
$name=$_POST['name'] ;
$email=$_POST['email'] ;
$comment=$_POST['comment'] ;
if(!isset($name) || $name==""){
$error .="Please Enter your name <br/>";
}elseif (!isset($email) || $email==""){
$error .="Please Enter your email Address.<br/>";
}elseif(!isset($comment) || $comment==""){
$error .="Please Enter your Comments.<br/>";
}
if ($error ==""){
mail($toaddress,"Contact form",$comment)
}
}
?>
<?php echo $error ;?>
<form method='post' action='' enctype='multipart/form-data' id='news_form' name='post_form' >
<div class="form row-fluid clearfix">
<div class="field span5">
<label>Your name:</label>
<input name="name" type="text" value="" class="req" placeholder="Placeholder text..." />
</div>
<div class="field span5">
<label>Your email:</label>
<input type="email" value="" class="req" name="email" />
</div>
<div class="clearfix"> </div>
<div class="field full">
<label>Your comment:</label>
<textarea class="span12" cols="2" rows="7" name="comment"></textarea>
</div>
<input type="submit" value="submit" />
</div>
</form>

Related

Php mail not submitting and blank page displaying

I am using Ampps on Mac and trying to send an email using php from a contact form however I do not receive the mail and when the form is submitted it redirects me to the file page resulting in a blank display
my form :
<form action="email.php" method="post">
<div class="col-md-6 w3_agileits_contact_left">
<span class="input input--akira">
<input class="input__field input__field--akira" type="text" id="input-22" name="Name" placeholder="" required="" />
<label class="input__label input__label--akira" for="input-22">
<span class="input__label-content input__label-content--akira">Your name</span>
</label>
</span>
<span class="input input--akira">
<input class="input__field input__field--akira" type="email" id="input-23" name="Email" placeholder="" required="" />
<label class="input__label input__label--akira" for="input-23">
<span class="input__label-content input__label-content--akira">Your email</span>
</label>
</span>
<span class="input input--akira">
<input class="input__field input__field--akira" type="text" id="input-24" name="Subject" placeholder="" required="" />
<label class="input__label input__label--akira" for="input-24">
<span class="input__label-content input__label-content--akira">Your subject</span>
</label>
</span>
</div>
<div class="col-md-6 w3_agileits_contact_right">
<div class="w3_agileits_contact_right1">
<textarea name="Message" id="Message" placeholder="Your comment here..." required=""></textarea>
</div>
<div class="w3_agileits_contact_right2">
<input type="submit" value="Send">
</div>
<div class="clearfix"> </div>
</div>
<div class="clearfix"> </div>
</form>
my File :
$email = $_POST['Email'];
$name = $_POST['Name'];
$to = "lucvanrooyen#gmail.com";
$subject = $_POST['Subject'];
$userMessage =$_POST['Message'];
$headers = "From: $email\n";
$message = "$name has sent you the following message.\n
Message: $userMessage";
$user = "$email";
$usersubject = "Thank You";
$userheaders = "From: lucvanrooyen#gmail.com\n";
$usermessage = "Thank you for your enquiry we will be in touch.";
mail($to,$subject,$message,$headers);
mail($user,$usersubject,$usermessage,$userheaders);
The code you have shown is not enough to debug your problem.
If you are using the mail method which is built into the standard library, read this answer to find out a few troubleshooting steps you could follow.
If you are and are still finding it difficult to use, then you could consider using one of the PHP emailing solutions like -
PHPMailer
SwiftMailer
Please check your form tag. there is no opening tag for form
<form action="email.php" method="post">

How to use data from bootstrap form in swiftmailer?

Hi I'm new to php and swift mailer and I face this problem when I want to send the data from a bootstrap form. When I use a bootstrap form I get an empty mail, but when I use a simple html form I get a .txt file with the information from the form in the mail. Here's my index.php code:
<form id="main-contact-form" name="contact-form" method="post" action="contact_email.php" role="form">
<div class="row wow fadeInUp" data-wow-duration="1000ms" data-wow-delay="300ms">
<div class="col-sm-6">
<div class="form-group">
<input type="text" name="name" class="form-control" placeholder="Name" required="required">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<input type="email" name="email" class="form-control" placeholder="Email Address" required="required">
</div>
</div>
</div>
<div class="form-group">
<input type="text" name="subject" class="form-control" placeholder="Subject" required="required">
</div>
<div class="form-group">
<textarea name="message" id="message" class="form-control" rows="4" placeholder="Enter your message" required="required"></textarea>
</div>
<div class="form-group">
<button type="submit" class="btn-submit">Send</button>
</div>
</form>
and here's my contact_email.php code
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<?php
error_reporting(-1);
ini_set('display_errors', 'On');
set_error_handler("var_dump");
$name = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$email = filter_var($_POST['email'], FILTER_SANITIZE_EMAIL);
$subject = filter_var($_POST['subject'], FILTER_SANITIZE_STRING);
$mesage = filter_var($_POST['messsage'], FILTER_SANITIZE_STRING);
$data = 'Name: '.$name.'<br />
Subject: '.$subject.'<br />
Email: '.$email.'<br />
Message: '.$mesage;
require_once 'swift/lib/swift_required.php';
$transport = Swift_SmtpTransport::newInstance('ssl://smtp.gmail.com', 465)
->setUsername('myemail#gmail.com')
->setPassword('********');
$mailer = Swift_Mailer::newInstance($transport);
$message = Swift_Message::newInstance('Email From My Website')
->setFrom(array('myemail#gmail.com' => 'website'))
->setTo(array('myemail#gmail.com'))
->setBody($data, text/html);
if ($mailer->send($message)) {
echo 'Mail sent successfully.';
} else {
echo 'I am sure, your configuration are not correct. :(';
}
?>
</body>
</html>
please tell me why I'm getting an empty mail and how to fix it and why I'm getting the information in a .txt file. thank you in advance :)

how to configure email address on submit button in html contact form

I am unable to configure my email address on the submit button in this piece of code:
<div id="contact" class="spacer">
<div class="container contactform center">
<h2 class="text-center wowload fadeInUp">Get in touch with us</h2>
<div class="row wowload fadeInLeftBig">
<div class="col-sm-6 col-sm-offset-3 col-xs-12">
<form class="cmxform" id="commentForm" method="post" action="email.php">
<fieldset>
<input type="text" placeholder="Subject" id="csubject" name="subject" minlength="2" type="text" required>
<input type="text" placeholder="Email" id="cemail" type="email" name="email" required>
<textarea rows="5" placeholder="Message" id="ccomment" name="comment" required></textarea>
<input class="submit btn btn-primary" type="submit" value="Submit">
</fieldset>
</form>
</div>
</div>
I tried linking it to a php page (email.php), but it says server error. I don't know what to do. Can someone please help me?
To send an email you need the mail() function http://php.net/manual/en/function.mail.php
With your code you need to name the submit like name="submit" then inside action.php file you have to write something like this (or inside the same php where you have this code):
if(isset($_POST["submit"]))
{
//Remember to do input validations
$subject = $_POST["subject"];
$from = $_POST["email"];
$comment = $_POST["comment"];
$body = "User with email $from send the next comment: $comment";
//then here you set your email
$to = "myemail#email.com"; //change this
mail($to,$subject,$body);
}
This is only to explain some basic usage and set the variables you need, I advice you read also PHPmailer class

PHP mail is not sending email to my email address

I've been looking around everywhere and cannot seem to find how to make this work - it is simply not sending an email to my address. Here is my HTML form:
<form id="contactMe" name="contact" method="post" novalidate="novalidate">
<fieldset>
<label for="name" id="name">Name<span class="required">*</span></label>
<input type="text" name="name" id="name" size="30" value="" required="">
<label for="email" id="email">Email<span class="required">*</span></label>
<input type="text" name="email" id="email" size="30" value="" required="">
<label for="phone" id="phone">Phone</label>
<input type="text" name="phone" id="phone" size="30" value="">
<label for="Message" id="message">Message<span class="required">*</span></label>
<textarea name="message" id="message" required=""></textarea>
<label for="Answer" id="answer">Name the house pet that says “<i>woof</i>“<span class="required">*</span></label>
<input type="text" name="answer" value="" required=""></br>
<input id="submit" type="submit" name="submit" value="Send">
</fieldset>
<div id="success">
<span class="green textcenter">
<p>Your message was sent successfully! I will be in touch as soon as I can.</p>
</span>
</div> <!-- closes success div -->
<div id="error">
<span><p>Something went wrong, try refreshing and submitting the form again.</p></span>
</div> <!--close error div-->
</form>
and here is my PHP saved as mailer.php:
<?php
$to = "someone#gmail.com";
$from = $_REQUEST['email'];
$name = $_REQUEST['name'];
$headers = "From: $from";
$subject = "You have a message from your.";
$fields = array();
$fields{"name"} = "name";
$fields{"email"} = "email";
$fields{"phone"} = "phone";
$fields{"message"} = "message";
$body = "Here is what was sent:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
mail("maytee.kneitz#gmail.com",$subject,$message,"From: $from\n");
echo 'Mail sent';
?>
This is my first shot at working on a mailer / contact form, so sorry if it's a blatant problem. I just can't seem to find it. Any guidance would be appreciated.
I do have validation in my scripts (not posted here).
You don't have a form action defined. Try this:
<form id="contactMe" name="contact" method="post" action="mailer.php" novalidate="novalidate">
By default, the form will be submitted to its current location unless otherwise specified. In your case, point it to wherever your mail script is located

Hide or clear a PHP form on submit

I have a php contact form which all works well with its validation etc, but I have one niggle with it. The original code which I have tweaked redirected the page to a new one with a thank you message on submission which I was unhappy with, so I've managed to get a thank you message to display on the original page, however the input form content still stays, i'd rather it didn't. Even better I'd like to be able to hide the form completely and replace it with a thank you.
I ought to mention that when completed it will be placed on a page with other items, so it is just the form that I'm after hiding, or clearing.
This is the code in the Header
<?php
$your_email ='email#example.com';
session_start();
$errors = '';
$name = '';
$company = '';
$visitor_email = '';
$phone = '';
$user_message = '';
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$company = $_POST['company'];
$visitor_email = $_POST['email'];
$phone = $_POST['phone'];
$user_message = $_POST['message'];
///------------Do Validations-------------
if(empty($name)||empty($visitor_email))
{
$errors .= "\n Name and Email are required fields. ";
}
if(IsInjected($visitor_email))
{
$errors .= "\n Bad email value!";
}
if(empty($_SESSION['6_letters_code'] ) ||
strcasecmp($_SESSION['6_letters_code'], $_POST['6_letters_code']) != 0)
{
//Note: the captcha code is compared case insensitively.
//if you want case sensitive match, update the check above to
// strcmp()
$errors .= "\n The captcha code does not match!";
}
if(empty($errors))
{
//send the email
$to = $your_email;
$subject="New form submission";
$from = $your_email;
$ip = isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '';
$body = "A user $name submitted the contact form:\n".
"Name: $name\n".
"Company: $company\n".
"Email: $visitor_email \n".
"Phone: $phone\n".
"Message: \n ".
"$user_message\n".
"IP: $ip\n";
$headers = "From: $from \r\n";
$headers .= "Reply-To: $visitor_email \r\n";
mail($to, $subject, $body,$headers);
//header('Location: #thanks');
$myForm = '< style="visibility: hidden;">';
$thankyou = file_get_contents("thank-you.html");
}
}
// Function to validate against any email injection attempts
function IsInjected($str)
{
$injections = array('(\n+)',
'(\r+)',
'(\t+)',
'(%0A+)',
'(%0D+)',
'(%08+)',
'(%09+)'
);
$inject = join('|', $injections);
$inject = "/$inject/i";
if(preg_match($inject,$str))
{
return true;
}
else
{
return false;
}
}
?>
And this is the form itself minus a chunk of javascript validation which I didn't think was relevant to the question
<?php
if(!empty($errors)){
echo "<p class='err'>".nl2br($errors)."</p>";
}
?>
<div id="footer">
<div class="twelve-column-wrapper">
<div class="six-column-wrapper">
<div class="six-column">
<h3>Why not get in touch</h3>
</div>
<div id='contact_form_errorloc' class='err'></div>
<form method="POST" name="contact_form"
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<div class="three-column">
<p>
<label for='name'>Name: </label>
<br>
<input type="text" name="name" value='<?php echo htmlentities($name) ?>'>
</p>
</div>
<div class="three-column">
<p>
<label for='company'>Your Company: </label>
<input type="text" name="company" id="company" value='<?php echo htmlentities($company) ?>'/>
</p>
</div>
<div class="three-column">
<p>
<label for='email'>Email: </label>
<br>
<input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>'>
</p>
</div>
<div class="three-column">
<p>
<label for='phone'>Phone No. </label>
<input type="text" name="phone" id="phone" value='<?php echo htmlentities($phone) ?>'/>
</p>
</div>
<div class="six-column">
<p>
<label for='message'>Message:</label>
<br>
<textarea name="message" rows=8 cols=30><?php echo htmlentities($user_message) ?></textarea>
</p>
</div>
<div class="three-column">
<p> <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
<label for='message'>Enter the code above here :</label>
<br>
<input id="6_letters_code" name="6_letters_code" type="text">
<br>
<small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small> </p>
<input id="submit" type="submit" value="Submit" name='submit'>
</div>
<div class="six-column"> <?php echo $thankyou; ?> </div>
</form>
</div>
</div>
</div>
I have tried a few methods I've found by searching but have fallen down mainly due to my basic knowledge of PHP.
Any help would be much appreciated.
<?php
if(!empty($errors)){
echo "<p class='err'>".nl2br($errors)."</p>";
}
?>
<div id="footer">
<div class="twelve-column-wrapper">
<div class="six-column-wrapper">
<div class="six-column">
<h3>Why not get in touch</h3>
</div>
<div id='contact_form_errorloc' class='err'></div>
<?php
if(!isset($_POST['submit'])):
?>
<form method="POST" name="contact_form"
action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>">
<div class="three-column">
<p>
<label for='name'>Name: </label>
<br>
<input type="text" name="name" value='<?php echo htmlentities($name) ?>'>
</p>
</div>
<div class="three-column">
<p>
<label for='company'>Your Company: </label>
<input type="text" name="company" id="company" value='<?php echo htmlentities($company) ?>'/>
</p>
</div>
<div class="three-column">
<p>
<label for='email'>Email: </label>
<br>
<input type="text" name="email" value='<?php echo htmlentities($visitor_email) ?>'>
</p>
</div>
<div class="three-column">
<p>
<label for='phone'>Phone No. </label>
<input type="text" name="phone" id="phone" value='<?php echo htmlentities($phone) ?>'/>
</p>
</div>
<div class="six-column">
<p>
<label for='message'>Message:</label>
<br>
<textarea name="message" rows=8 cols=30><?php echo htmlentities($user_message) ?></textarea>
</p>
</div>
<div class="three-column">
<p> <img src="captcha_code_file.php?rand=<?php echo rand(); ?>" id='captchaimg' ><br>
<label for='message'>Enter the code above here :</label>
<br>
<input id="6_letters_code" name="6_letters_code" type="text">
<br>
<small>Can't read the image? click <a href='javascript: refreshCaptcha();'>here</a> to refresh</small> </p>
<input id="submit" type="submit" value="Submit" name='submit'>
</div>
</form>
<?php
endif;
?>
<div class="six-column"> <?php echo $thankyou; ?> </div>
</div>
</div>
</div>
That will hide the form. But that won't prevent you to be spammed. If you don't want to be spammed to have to track IPs and before submitting the email check that the IP didn't already send an email let's say in the last 30 seconds, for instance.
if(empty($_POST)){
//You form goes here;
}
else{
echo $thankyou;
}
Also you can have something like:
if(empty($thankyou)){
//You form goes here;
}
else{
echo $thankyou;
}
You can trigger this function onsubmit or onclick event on form
<script>
function hideform()
{
document.forms['contact_form'].style.visibility = 'hidden';
}
</script>
You could use jQuery to clear the form div on a successful submission.
$('#myform').submit(function(){
$('#formcontainer').empty;
});
Where myform and form container are the ids of your form and the div containing your form
You better create separate action file and for display thanks message use:
if(isset($_SESSION['msg'])){
echo $_SESSION['msg'];
}
else{
//display from here
}

Categories