HTML form with external php form processing? - php

Below I have my html and php coding. I've racked my brain trying several different options, but what I am really looking for is something simple and secure that either reloads to my original page showing sent message. As of right now I get this error message when submitting my form. Someone please point me in the right direction, any feedback is appreciated.
HTML CODING(index.html)
<html>
<head><title></title></head>
<body>
<div id="wrapper2">
<div id="newsletter" class="container">
<div class="title">
<img src="images/port-formdivide.png" alt="" width="800" height="60" />
<h2>Contact Me</h2>
<span class="byline">Please complete the entire form.</span> </div>
<div class="content">
<form action="form.php" method="POST" enctype="multipart/form-data" target="_self">
<div class="row half">
<div class="6u">
<input name="name/company" id="name/company" type="text" class="text" maxlength="30"
placeholder="Name/Company" value="" />
</div>
<div class="6u">
<input name="email" id="email" type="text" class="text" maxlength="30"
placeholder="Email" value="" />
</div>
</div>
<div class="row half">
<div class="12u">
<textarea name="message" id="message" placeholder="Message" value=""></textarea>
</div>
</div>
<div class="row">
<div class="12u">
<input name="Send Message" type="submit" class="button submit" value="Send Message" />
</div>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
PHP CODING(form.php)
<?php
// Email Variables
$to = 'inquiries#triston-thomas.com';
$subject = 'New Inquiry';
/* Data Variables */
if ($_POST['parse_var'] == "form"){
$emailTitle = 'New Message Triston';
$yourEmail = 'inquiries#triston-thomas.com':
$name/company = $_POST['name/company'];
$email = $_POST['email'];
$message = $_POST['message'];
echo ("Name/Company:" . $name/company);
echo ("<br />Email:" . $email);
echo ("<br />Message:" . $message);
function response($name/company);
{
echo ("Thank You " . $name/company);
}
header ("Location: index.html");
mail ($to, $subject, $message, "From: ".$name/company);
echo "Your Inquiry Has Been Sent";
// Set Required Fields
$required_ fields = array("$name/company","$email","$message");
// Set Error Messages
$error_message = array("$name/company" => 'Please Enter Your Name or Company Name In
The Field',
"$email" => 'Valid Email Address Is Required',
"$message" => 'Please Enter Message In The Field',);
/*Behaviors*/
if ($_POST)
{
mail($to, $subject, $message, $header);
}
if(isset($_POST['message']))
{
echo $_POST['name/company'];
echo "<br />";
echo $_POST['email'];
echo "<br />";
echo $_POST['message'];
}
if(empty($_POST['name/company']))
{
echo "Name Field is Required";
}
if(empty($_POST['email']))
{
echo "Email Field is Required";
}
if(empty($_POST['message']))
{
echo "Message Field is Required";
}
$body = <<<EOD
<br><hr><br>
Name/Company: $name/company <br />
Email: $email <br />
Message: $message <br />
EOD;
$headers = "From: $email\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail("$yourEmail", "$emailTitle", "$body", "$headers");
$sent = "Thank You, Your Inquiry Has Been Sent";
echo $result;
enter code here
}
?>

Related

Prevent Resubmissions for PHP Contact form

This is the code for the form.
<?php
if ($_POST) {
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$company = $_POST['company'];
$phone = $_POST['phone'];
$checkbox = '';
if (isset($_POST['checkbox'])) {
$checkbox = 'Yes';} else{
$checkbox = 'No' ;}
$message = $_POST['message'];
$from = 'Demo Contact Form';
$to = 'imvael#gmail.com, vnikolic1#cps.edu';
$subject = 'Message from Contact Demo ';
$body ="From: $name\n E-Mail: $email\n Company: $company\n Phone: $phone\n Opt In?: $checkbox\n Message:\n $message";
$headers = 'From: webmaster#bradfordsystems.com' . "\r\n" .
'Reply-To: ' .$email . "\r\n" .
'X-Mailer: PHP/' . phpversion();
// 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 name has been entered
if (!$_POST['phone']) {
$errName = 'Please enter your phone number';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// then after the posting of the form data
// validation
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && isset($_POST['url']) && $_POST['url'] == '') {
if (mail ($to, $subject, $body, $headers)) {
header("Location: thankyou.php"); /* Redirect browser */
exit();
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
}
header("Location: " . $_SERVER['REQUEST_URI']);
exit();
}
This is the form itself
<div class="wrapper">
<div class="row">
<div class="col _66">
<?php echo $result; ?>
<form role="form" name="contactForm" id="contactForm" method="post" action="contact.php#contactForm">
<p>
We welcome your feedback and inquiries. Please use the form below to get in touch.
</p>
<div class="row">
<div class="col">
<input type="text" id="name" name="name" placeholder="Name" value="<?php echo htmlspecialchars($_POST['name']); ?>" required>
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="col">
<input type="email" id="email" name="email" placeholder="Company Email" value="<?php echo htmlspecialchars($_POST['email']); ?>" required>
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</div>
<div class="row">
<div class="col">
<input type="text" id="company" name="company" placeholder="Company" value="<?php echo htmlspecialchars($_POST['company']); ?>" required>
</div>
<div class="col">
<input type="tel" id="phone" name="phone" <?php echo htmlspecialchars($_POST['phone']); ?> placeholder="Phone" required>
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
</div>
<div class="row">
<div class="col">
<input type="number" id="zipcode" name="zipcode" placeholder="Zip Code" value="<?php echo htmlspecialchars($_POST['zipcode']); ?>">
</div>
<div class="col">
<input id="checkBox" type="checkbox"> <span id="optInText">YES, I want a Free Workspace Evaluation!</span>
</div>
</div>
<div class="row">
<div class="col">
<p class="antispam">Leave this empty: <input type="text" name="url" /></p>
</div>
</div>
<div class="row">
<div class="col submit-col">
<p>Questions or Comments?</p>
<textarea id="message" name="message" placeholder="Enter your questions or comments here" style="height:200px"> <?php echo htmlspecialchars($_POST['message']); ?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
<button class="btn btn-dark hvr-underline-from-left" name="submit" type="submit" value="Send">Submit Request</button>
</div>
</div>
</form>
</div>
</div>
</section>
I am writing a contact form script from scratch The code is working correctly, but I am unable to prevent resubmissions if they user refreshed the page or press the submit buton more then once.
Also is it a bad practice to use self submitting contact forms?

HTML Post action sends me to php form

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!

Is there anything wrong with the code in my form?

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

PHP Undefined index error with form

I keep getting an error as follows when i submit my form. I'm not sure what it is. I'm running the site on WAMP (windows) and im writing it in dreamweaver CS6. Please note I'm fairly new to PHP. Thanks!
Here is the code:
Notice: Undefined index: message in S:\wamp\www\latech\contact.php on line 29
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<title>Anise Technologys</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
<h1 id="title-center">Contact</h1>
<?php
// Check for Header Injections
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset($_POST['contact_submit'])) {
// Assign trimmed form data to variables
// Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message']; // no need to trim message
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die(); // If true, kill the script
}
if (!$name || !$email || !$msg) {
echo '<h4 class="error">All fields required.</h4>Go back and try again';
exit;
}
// Add the recipient email to a variable
$to = "brad#brightsidestudios.ca";
// Create a subject
$subject = "$name sent a message via your contact form";
// Construct the message
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
// If the subscribe checkbox was checked
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
// Add a new line to the $message
$message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
}
$message = wordwrap($message, 72); // Keep the message neat n' tidy
// Set the mail headers into a variable
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: " . $name . " <" . $email . ">\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email!
mail($to, $subject, $message, $headers);
?>
<!-- Show success message after email has sent -->
<h5>Thanks for contacting Anise Technologys!</h5>
<p>Please allow 24 hours for a response.</p>
<p>« Go to Home Page</p>
<?php
} else {
?>
<form method="post" action="" id="contact-form" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" placeholder="Name">
</div>
<div class="pure-control-group">
<label for="email">Your Emal</label>
<input type="email" id="email" name="email" placeholder="you#website.com">
</div>
<div class="pure-control-group">
<label for="tel">Your Telephone</label>
<input type="tel" id="tel" name="tel" placeholder="(xxx)xxx-xxxx">
</div>
<div class="pure-control-group">
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
</div>
<div class="pure-control-group">
<label for="subscribe">
<input type="checkbox" id="cb" name="subscribe" value="Subscribe">
Subscribe to Newsletter</label>
</div>
<div class="pure-control-group">
<input type="submit" class="pure-button pure-button-primary" name="contact_submit" value="Send Message">
</div>
</form>
<?php
}
?>
</div>
</div>
</body>
</html><!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<?php include 'includes/header.php'?>
<link rel="stylesheet" href="http://yui.yahooapis.com/pure/0.6.0/pure-min.css">
<title>Anise Technologys</title>
</head>
<body>
<div class="wrapper">
<?php include 'includes/nav.php'?>
<div class="content">
<h1 id="title-center">Contact</h1>
<?php
// Check for Header Injections
function has_header_injection($str) {
return preg_match( "/[\r\n]/", $str );
}
if (isset($_POST['contact_submit'])) {
// Assign trimmed form data to variables
// Note that the value within the $_POST array is looking for the HTML "name" attribute, i.e. name="email"
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$tel = trim($_POST['tel']);
$msg = $_POST['message']; // no need to trim message
// Check to see if $name or $email have header injections
if (has_header_injection($name) || has_header_injection($email)) {
die(); // If true, kill the script
}
if (!$name || !$email || !$msg) {
echo '<h4 class="error">All fields required.</h4>Go back and try again';
exit;
}
// Add the recipient email to a variable
$to = "brad#brightsidestudios.ca";
// Create a subject
$subject = "$name sent a message via your contact form";
// Construct the message
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
// If the subscribe checkbox was checked
if (isset($_POST['subscribe']) && $_POST['subscribe'] == 'Subscribe' ) {
// Add a new line to the $message
$message .= "\r\n\r\nPlease add $email to the mailing list.\r\n";
}
$message = wordwrap($message, 72); // Keep the message neat n' tidy
// Set the mail headers into a variable
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
$headers .= "From: " . $name . " <" . $email . ">\r\n";
$headers .= "X-Priority: 1\r\n";
$headers .= "X-MSMail-Priority: High\r\n\r\n";
// Send the email!
mail($to, $subject, $message, $headers);
?>
<!-- Show success message after email has sent -->
<h5>Thanks for contacting Anise Technologys!</h5>
<p>Please allow 24 hours for a response.</p>
<p>« Go to Home Page</p>
<?php
} else {
?>
<form method="post" action="" id="contact-form" class="pure-form pure-form-aligned">
<div class="pure-control-group">
<label for="name">Your Name</label>
<input type="text" id="name" name="name" placeholder="Name">
</div>
<div class="pure-control-group">
<label for="email">Your Emal</label>
<input type="email" id="email" name="email" placeholder="you#website.com">
</div>
<div class="pure-control-group">
<label for="tel">Your Telephone</label>
<input type="tel" id="tel" name="tel" placeholder="(xxx)xxx-xxxx">
</div>
<div class="pure-control-group">
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
</div>
<div class="pure-control-group">
<label for="subscribe">
<input type="checkbox" id="cb" name="subscribe" value="Subscribe">
Subscribe to Newsletter</label>
</div>
<div class="pure-control-group">
<input type="submit" class="pure-button pure-button-primary" name="contact_submit" value="Send Message">
</div>
</form>
<?php
}
?>
</div>
</div>
</body>
</html>
Your name attribute is msg your calling it message in PHP.
<label for="msg">Your Message</label>
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
$msg = $_POST['message']; // no need to trim message
Also here...
$message .= "Name: $name\r\n";
$message .= "Email: $email\r\n\r\n";
$message .= "Message:\r\n$msg";
The first occurrence should be $message = because there is nothing to concatenate when it hasn't been initialized.
You are using
<textarea id="msg" name="msg" spellcheck="true" ></textarea>
Rather use
<textarea id="msg" name="message" spellcheck="true" ></textarea>

Mail is not coming properly using php?

I am using below simple code to send mail using php. when i echo the line $headers = "From: . $email\r\n"; //echo $headers; exit; which results like From: . test#email.com.
I found dot is printing before mail address , so mail is coming but not properly.If i remove dot , mail is not even coming.Whats wrong with this code ?
<?php
if (isset($_REQUEST['user_email']))
//if "email" is filled out, send email
{
//send email
$email = $_REQUEST['user_email'];
$user_name = $_REQUEST['user_name'];
$user_message = $_REQUEST['user_message'];
include 'contact_mail_form.php';
$to = "testtest#gmail.com";
$message = $contactText;
$subject = "Test Mail";
$headers = "From: . $email\r\n"; //echo $headers; exit;
$headers .= "Content-type: text/html\r\n";
mail($to,$subject,$message,$headers);
echo "Thank you for using our mail form";
}
else
//if "email" is not filled out, display the form
{
?>
<form id="contact_form" name="ContactForm" action="" method="post" onsubmit="return checkContact(this);">
<div class="wrapper">
<div class="fright">
<div class="name">Message:</div>
<textarea cols="1" rows="1" name="user_message" id="user_message" ></textarea>
<span class="clear"></span> </div>
<div class="fleft">
<div>
<div class="name">Your Name:</div>
<input type="text" class="input" name="user_name" id="user_name" />
<span class="clear"></span> </div>
<div>
<div class="name">E-mail:</div>
<input type="text" class="input" name="user_email" id="user_email" />
<span class="clear"></span> </div>
</div>
</div>
<div class="buttons"><a class="more" href="javaScript:void(0);" onClick="document.getElementById('contact_form').reset()">clear</a> <input class="more1" type="SUBMIT" class="button2" value="Send" name="" /> </div>
</form>
<?php } ?>
Try it like this your dot is acting as string not as concatenation
$headers = "From:" . $email."\r\n";
Maybe because \r\n should be surrounded with " not '
And Also
$headers .= 'Content-type: text/html'. "\r\n";

Categories