I've looked at various solutions but I just can't get my contact form to work. **The issue im having is that the email wont actually send out to me, everything works client side but I dont get the email. So I have come here to surely get the duplicate question label. Here is my code:
<form method="post" class="reply" id="contact" action="process.php">
<fieldset>
<div class="row">
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<label>Name: <span>*</span></label>
<input class="form-control" id="name" name="name" type="text" value="" required>
</div>
<div class="col-lg-6 col-md-6 col-sm-6 col-xs-12">
<label>Email: <span>*</span></label>
<input class="form-control" type="email" id="email" name="email" value="" required>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<label>Subject: <span>*</span></label>
<input class="form-control" id="subject" name="subject" type="text" value="" required>
</div>
</div>
<div class="row">
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
<label>Message: <span>*</span></label>
<textarea class="form-control" id="text" name="text" rows="3" cols="40" required></textarea>
</div>
</div>
</fieldset>
<button class="btn btn-normal btn-color submit bottom-pad" type="submit">Send</button>
<div class="success alert-success alert" style="display:none">Your message has been sent successfully.</div>
<div class="error alert-error alert" style="display:none">E-mail must be valid and message must be longer than 100 characters.</div>
<div class="clearfix">
</div>
</form>
Here is my process.php
<?php
// Email Submit
// Note: filter_var() requires PHP >= 5.2.0
if ( isset($_POST['email']) && isset($_POST['name']) && isset($_POST['subject']) && isset($_POST['text']) && filter_var($_POST['email'], FILTER_VALIDATE_EMAIL) ) {
// detect & prevent header injections
$test = "/(content-type|bcc:|cc:|to:)/i";
foreach ( $_POST as $key => $val ) {
if ( preg_match( $test, $val ) ) {
exit;
}
}
// PREPARE THE BODY OF THE MESSAGE
$message = '<html><body>';
$message .= '<table rules="all" style="border-color: #666;" cellpadding="10">';
$message .= "<tr style='background: #eee;'><td><strong>Name:</strong> </td><td>" . strip_tags($_POST['name']) . "</td></tr>";
$message .= "<tr><td><strong>Email:</strong> </td><td>" . strip_tags($_POST['email']) . "</td></tr>";
$message .= "<tr><td><strong>Message:</strong> </td><td>" . htmlentities($_POST['text']) . "</td></tr>";
$message .= "</table>";
$message .= "</body></html>";
// CHANGE THE BELOW VARIABLES TO YOUR NEEDS
$to = 'iknowichange#this.com';
$subject = $_POST['subject'];
$headers = "From: " . $_POST['email'] . "\r\n";
$headers .= "Reply-To: ". strip_tags($_POST['req-email']) . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
mail($to, $subject, $message, $headers);
}
?>
I am completely new to forms, so thanks in advance for the help. If there are any resources that you can suggest that would be great. Thanks!
It looks like you just forgot to include an action in your form element.
(Unless your binding an onsubmit event somewhere else)
Try
<form method="post" class="reply" id="contact" action="process.php">
You haven't set any action in your form element. You've to set the path of your "process.php" in form element like following:
<form action="process.php" method="post" id="contact" class="reply">
...
</form>
More about form: http://www.w3schools.com/html/html_forms.asp
Related
I am a complete novice with PHP, I just want a simple email contact form that will display an error if a required field isn't entered, and will give a thank you message if the email is sent.
When I click my submit button the email does get sent but the website redirects straight to the /send-email.php page, which is blank and useless. The required form fields also don't seem to do anything in the way of preventing an email from being sent if the forms aren't filled.
HTML form code:
<form class="contact-form" action="php/send-email.php" method="post" novalidate>
<div class="row">
<div class="column width-6 pad-1 contact-column">
<h5>Your Name*</h5>
<input type="text" name="name" class="form-box" tabindex="1" required>
</div>
<div class="column width-6 pad-1 contact-column">
<h5>Email*</h5>
<input type="text" name="email" class="form-box" tabindex="2" required>
</div>
</div>
<div class="row mt-2">
<div class="column width-6 pad-1 contact-column">
<h5>Your Website</h5>
<input type="text" name="website" class="form-box" tabindex="3">
</div>
<div class="column width-6 pad-1 contact-column">
<h5>Company</h5>
<input type="text" name="company" class="form-box" tabindex="4">
</div>
</div>
<div class="column width-12">
<input type="text" name="honeypot" class="form-honeypot">
</div>
<div class="row mt-2">
<div class="column width-12 pad-1">
<h5>Your Message*</h5>
<textarea name="message" class="form-text" tabindex="5" required></textarea>
</div>
</div>
<div class="row mt-2">
<input type="submit" value="Send Your Message!" class="btn btn-large bg-blue">
</div>
</form>
Php file
<?php
$recipient = "contact#mysite.com";
$name = filter_var($_POST["name"], FILTER_SANITIZE_STRING);
$email = filter_var($_POST["email"], FILTER_SANITIZE_EMAIL);
$website = filter_var($_POST["website"], FILTER_SANITIZE_URL);
$company = filter_var($_POST["company"], FILTER_SANITIZE_STRING);
$message = filter_var($_POST["message"], FILTER_SANITIZE_STRING);
$headers = 'From: '.$name.' <'.$email.'>' . "\r\n";
$headers .= 'Reply-To: '.$email.'' . "\r\n";
$headers .= 'X-Mailer: PHP/' . phpversion();
$subject = "New email from contact form";
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n";
$email_content .= "Website: $website\n";
$email_content .= "Company: $company\n\n";
$email_content .= "Message:\n$message\n\n\n";
if(mail($recipient, $subject, $email_content, $headers)){
echo "Thanks for the email, we'll get back to as soon as possible!";
}
?>
Any help in the right direction is appreciated!
Check for empty input. Try using if else statement. eg.
if($name == '')
{
echo "Please fill in name";
}
else if($var == '')
{
//error message
}
or you can try
if($name == '' || $email == '' || $var == '')
{
echo "Please fill in all the blank";
}
I'm quite new too to PHP, so this is just a simple checking. Hope it'll helps!
Okay, so I've followed a tutorial and just changed some of the variables and created them. I have a HTML and PHP code below, what's wrong with the PHP that's causing it not too work? Any ideas.
<?php
if(isset($_POST['email'])) {
// EDIT THE 2 LINES BELOW AS REQUIRED
$email_to = " MY EMAIL ";
$email_subject = "FORM SUBMISSION";
// validation expected data exists
if(!isset($_POST['name']) ||
!isset($_POST['address']) ||
!isset($_POST['email']) ||
!isset($_POST['telephone']) ||
!isset($_POST['message'])) {
died('We are sorry, but there appears to be a problem with the form you submitted.');
}
$name = $_POST['name']; // required
$address = $_POST['address']; // required
$email = $_POST['email']; // required
$telephone = $_POST['telephone']; // not required
$message = $_POST['message']; // required
$error_message = "";
$email_message = "Form details below.\n\n";
function clean_string($string) {
$bad = array("content-type","bcc:","to:","cc:","href");
return str_replace($bad,"",$string);
}
$email_message .= "Name: ".clean_string($name)."\n";
$email_message .= "Address: ".clean_string($address)."\n";
$email_message .= "Email: ".clean_string($email)."\n";
$email_message .= "Telephone: ".clean_string($telephone)."\n";
$email_message .= "Message: ".clean_string($message)."\n";
// create email headers
$headers = 'From: '.$email."\r\n".
'Reply-To: '.$email."\r\n" .
'X-Mailer: PHP/' . phpversion();
#mail($email_to, $email_subject, $email_message);
?>
<?php
header('Location: MY LINK IS HERE BUT NOT WORKING');
?>
<?php
}
?>
Form
<div class="row contact-form">
<div class="container">
<h3>Get In Touch</h3>
<div class="col-sm-6">
<div class="row">
<form name="email" action="php/mail.php" method="POST">
<div class="col-sm-6">
<p class="input-label">Name <span class="star-red">*</span>
</p>
<input type="text" name="name" id="name" class="form-input-wide" required>
</div>
<div class="col-sm-6">
<p class="input-label">Address</p>
<input type="text" name="address" id="address" class="form-input-wide">
</div>
</div>
<div class="row">
<form name="send-mail" action="php/mail.php" method="POST">
<div class="col-sm-6">
<p class="input-label">Telephone <span class="star-red">*</span>
</p>
<input type="tel" name="telephone" id="telephone" class="form-input-wide" required>
</div>
<div class="col-sm-6">
<p class="input-label">Email <span class="star-red">*</span>
</p>
<input type="email" name="email" id="email" class="form-input-wide" required>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<p class="input-label">Message <span class="star-red">*</span>
</p>
<textarea name="message" id="message" style="width:100% !important;" rows="6" required></textarea>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<input type="submit" class="submit-button">
</div>
</div>
<div class="row">
<div class="col-sm-6">
 
</div>
</div>
</form>
</div>
</div>
</div>
</div>
If there is anything that I need to change? I think the error messages may be breaking the PHP.
It seems there are two things wrong in the PHP script:
!isset($_POST['address`']) ||
needs to be
!isset($_POST['address']) ||
and
if(strlen($messages) < 2) {
needs to be
if(strlen($message) < 2) {
Maybe you can look into the usage of some "form validators" for some better usability. It would be a bit nicer to show the same form again but then with error messages attached to them, in stead of stopping the script with an error message.
EDIT:
For the header redirect: remove the whitespaces. The header('...') function needs to run before any browser output. Now there are whitespaces outputted to the browser. So remove "? >" and "< ? php"
?>
<?php
header('Location: MY LINK IS HERE BUT NOT WORKING');
I'm using this tutorial to create a contact form for my site and it doesn't seem to be working. After I fill in my form and press submit, nothing happens. I would like to know whats wrong with it and how I can fix it.
This is my code:
HTML:
<form action="mail/contact.php" method="post">
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Name:</label><br />
<input type="text" name="name" id="name" class="form-control" placeholder="Name"/>
</div></div>
<br />
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Email:</label><br />
<input type="text" name="email" id="email" class="form-control" placeholder="Email"/>
</div></div>
<br />
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label>Message:</label>
<textarea name="message" rows="5" id="message" class="form-control" placeholder="Message"></textarea>
</div></div>
<br />
<input type="submit" name="submit" value="Submit" class="btn btn-success btn-lg"/>
</form>
PHP:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$to = 'name#mail.com';
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($to, $subject, $body, $headers);
// popup success
echo '<script language="javascript">';
echo 'alert("Your message has been sent!")';
echo '</script>';
?>
It's not a problem actually, you hace to change $to to $email in the email() method because the variable that has the email you want to send to is $email.
Your contact.php code will be:
<?php
// from the form
$name = trim(strip_tags($_POST['name']));
$email = trim(strip_tags($_POST['email']));
$message = htmlentities($_POST['message']);
// set here
$subject = "Contact form submitted!";
$body = <<<HTML
$message
HTML;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
// send the email
mail($email, $subject, $body, $headers);
// popup success
echo '<script language="javascript">';
echo 'alert("Your message has been sent!")';
echo '</script>';
?>
Another recommendation is to check this tutorial for a secure contact form.
Also you should check out this two php frameworks:
Symfony 2 Mail
Zend 2 Mail
I am using a simple html form as a contact, and when fields and submitted the form does not clear the fields.
this is my php
I read online in few places and I've learned that I have to use the .reset , but I am not familiar with php a lot. I am not sure where would I add the .reset and how.
<?
$name = $_REQUEST["name"];
$email = $_REQUEST["email"];
$msg = $_REQUEST["msg"];
$to = "example#example.com";
if (isset($email) && isset($name) && isset($msg)) {
$subject = "Message / Closure Film";
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
$headers .= "From: ".$name." <".$email.">\r\n"."Reply-To: ".$email."\r\n" ;
$msg = "Name: $name:<br/> Email: $email <br/> Message: $msg";
$mail = mail($to, $subject, $msg, $headers);
if($mail)
{
echo 'success';
}
else
{
echo 'failed';
}
}
?>
my html
<div id="contact">
<div class="container">
<div class="row-fluid PageHead">
<div class="span12">
<h3>CONTACT US<span> <img src="images/underline.png" alt="______"></span></h3>
</div>
</div>
<div class="row-fluid ContactUs">
<div class="span6 offset3">
<form class="form-horizontal" id="phpcontactform">
<div class="control-group">
<input class="input-block-level" type="text" placeholder="Full Name" name="name" id="name">
</div>
<div class="control-group">
<input class="input-block-level" type="email" placeholder="Email" name="email" id="email">
</div>
<div class="control-group">
<textarea class="input-block-level" rows="10" name="message" placeholder="Your Message" id="message"></textarea>
</div>
<div class="control-group">
<p>
<input class="btn btn-danger btn-large" type="submit" value="Send Message">
</p>
<span class="loading"></span> </div>
</form>
</div>
added this to the head of my html, but didnt get any result
<script type="javascript">
$('#phpcontactform').trigger("reset");
</script>
Try:
<script type="javascript">
$(document).ready(function() {
$('#phpcontactform')[0].reset();
});
</script>
I'm not sure if you're trying to do an ajax submit and keep the user on the page, or just submit the form. But the line you're looking for is $("#phpcontactform")[0].reset();, you could wrap that in a $(document).ready() if you needed to!
this is my code in HTML5
whenever i am clicking on send button ...
phpfiles open rather mailing on that mail id which I mentioned can anybody help me out please ???
<div class="col-md-6">
<div class="alert alert-success hidden" id="contactSuccess">
<strong>Success!</strong> Your message has been sent to us.</div>
<div class="alert alert-danger hidden" id="contactError">
<strong>Error!</strong> There was an error sending your message.</div>
<h2 class="short">
<strong>Contact</strong> Us</h2>
<form action="php/contact-form.php" id="contactForm" type="post">
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Your name *</label>
<input type="text" value="" data-msg-required="Please enter your name." maxlength="100" class="form-control"
name="name" id="name" /></div>
<div class="col-md-6">
<label>Your email address *</label>
<input type="email" value="" data-msg-required="Please enter your email address."
data-msg-email="Please enter a valid email address." maxlength="100" class="form-control" name="email"
id="email" /></div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Subject</label>
<input type="text" value="" data-msg-required="Please enter the subject." maxlength="100" class="form-control"
name="subject" id="subject" /></div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Message *</label>
<textarea maxlength="5000" data-msg-required="Please enter your message." rows="10" class="form-control" name="message"
id="message"></textarea></div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<input type="submit" value="Send Message" class="btn btn-primary btn-lg" data-loading-text="Loading..." />
</div>
</div>
</form>
</div>
======================================================
and dis are my codes in php file...
<?php
session_cache_limiter('nocache');
header('Expires: ' . gmdate('r', 0));
header('Content-type: application/json');
// Enter your email address below.
$to = 'info#webppulse.com';
$subject = $_POST['subject'];
if($to) {
$name = $_POST['name'];
$email = $_POST['email'];
$fields = array(
0 => array(
'text' => 'Name',
'val' => $_POST['name']
),
1 => array(
'text' => 'Email address',
'val' => $_POST['email']
),
2 => array(
'text' => 'Message',
'val' => $_POST['message']
)
);
$message = "";
foreach($fields as $field) {
$message .= $field['text'].": " . htmlspecialchars($field['val'], ENT_QUOTES) . "<br>\n";
}
$headers = '';
$headers .= 'From: ' . $name . ' <' . $email . '>' . "\r\n";
$headers .= "Reply-To: " . $email . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=UTF-8\r\n";
if (mail($to, $subject, $message, $headers)){
$arrResult = array ('response'=>'success');
} else{
$arrResult = array ('response'=>'error');
}
echo json_encode($arrResult);
} else {
$arrResult = array ('response'=>'error');
echo json_encode($arrResult);
}
?>
whenever i am clicking on send button ...
phpfiles open rather mailing on that mail id can someone help me out please ?
Change the following code
<form action="php/contact-form.php" id="contactForm" type="post">
with
<form action="php/contact-form.php" id="contactForm" method="post">
You Must have install localhost server Like xampp or wampp.
Then put files in
For Xampp : htdocx/ Put your file in htdocx folder and run.
Foe wampp: www/ put your file in www folder and run.
Turn your type="post" into a method="post". Also, you need to have your project placed into the htdocs folder of your WAMP directory and ensure that the WAMP server has been turned on.
You should always try a "Hello World" test first to ensure that everything is running correctly. Check out tutorials on the Web for this.