Server Error with PHP contact form - php

Not sure what is causing the error. I have tried several times but continue to get the error. Any help welcome. Thank you.
Here is my form:
<form class="form-horizontal" role="form" method="post" action="modalcontact.php">
<div class="form-group">
<div class="col-xs-8 col-xs-offset-2">
<input type="text" class="form-control" id="name" name="name" placeholder="NAME" value="">
</div>
</div>
<div class="form-group">
<div class="col-xs-8 col-xs-offset-2">
<input type="email" class="form-control" id="email" name="email" placeholder="EMAIL" value="">
</div>
</div>
<div class="form-group">
<div class="col-xs-8 col-xs-offset-2">
<textarea class="form-control" rows="4" name="message" placeholder="MESSAGE"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-xs-8 col-xs-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>
Here is the PHP under modalcontact.php file in my hosting server:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'L|D Contact Form';
$to = 'myemail#email.com';
$subject = 'Message from L|D Contact ';
$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';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
echo '<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
echo '<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>

Try this code replace the from with header:
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
$headers .= 'From: abc#gmail.com';
I tested your code and it works fine. Then what's problem on your server...

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?

How to get the sucess message in the same page after submitting the contact form?

I have created the contact form using HTML and PHP. Also, the mail is coming correctly to mail id. But after the success message, it is redirecting to the form.php page can someone please help me. It is my first time trying to build a website.
Here is my code for contact form:
<form method="post" action="form.php">
<input name="name" required="required" placeholder="Your Name">
<input name="email" type="email" required="required" placeholder="Your Email">
<div class="clearfix"> </div>
<textarea name="message" cols="20" rows="5" required="required" placeholder="Message"></textarea>
<div class="submit">
<input id="submit" name="submit" type="submit" value="Submit">
</div>
</form>
here is my form.php :
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: agriindiaexp.com';
$to = 'shridhar.kagi#gmail.com';
$subject = 'Email Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
$success = "Message successfully sent";
} else {
$success = "Message Sending Failed, try again";
}
}
?>
Please help me.
try this way . try to send mail from an ajax . Please write your code like below
javascript
<script type="text/javascript">
function sendEnquiryform(){
var name=$('#name').val();
var email=$('#email').val();
var message=$('#message').val();
$.post("send_mail.php",'name='+name+'&email='+email'&message='+message,function(result,status,xhr) {
if( status.toLowerCase()=="error".toLowerCase() )
{ alert("An Error Occurred.."); }
else {
//alert(result);
$('#sucessMessage').html(result);
}
})
.fail(function(){ alert("something went wrong. Please try again") });
}
</script>
Your html
<form method="post" name="FrmEnquiry" id="FrmEnquiry" action="" onsubmit="sendEnquiryform();">
<input name="name" id="name" required="required" placeholder="Your Name">
<input name="email" id="email" type="email" required="required" placeholder="Your Email">
<div class="clearfix"> </div>
<textarea name="message" id="message" cols="20" rows="5" required="required" placeholder="Message"></textarea>
<div class="submit">
<input id="submit" name="submit" type="submit" value="Submit">
</div>
</form>
<span id="sucessMessage"> </span>
send_mail.php
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: agriindiaexp.com';
$to = 'shridhar.kagi#gmail.com';
$subject = 'Email Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if ($_POST['submit']) {
if (mail ($to, $subject, $body, $from)) {
$success = "Message successfully sent";
} else {
$success = "Message Sending Failed, try again";
}
}
?>
this will display your message in your page.Please try this. This is working fine in my case.
You could post the form to the same page and check for a success message there, like this.
<?php
if ($_POST['submit']) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: agriindiaexp.com';
$to = 'shridhar.kagi#gmail.com';
$subject = 'Email Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (mail ($to, $subject, $body, $from)) {
$success = "Message successfully sent";
} else {
$success = "Message Sending Failed, try again";
}
}
?>
...other html....
<div id="message"><?php if(isset($success)){ echo $success; } ?></div>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<input name="name" required="required" placeholder="Your Name">
<input name="email" type="email" required="required" placeholder="Your Email">
<div class="clearfix"> </div>
<textarea name="message" cols="20" rows="5" required="required" placeholder="Message"></textarea>
<div class="submit">
<input id="submit" name="submit" type="submit" value="Submit">
</div>
</form>
...other html....
getting the error in java script.
function sendEnquiryform(){
var fname=$('#fname').val();
var email=$('#email').val();
var pd=$('#pd').val();
var pg=$('#pg').val();
var ced=$('#ced').val();
var score=$('#score').val();
var message=$('#message').val();
$.post("mail.php",'fname='+name+'&email='+email' +'&pd='+pd' +'&pg='+pg' +'&ced='+ced' +'&score='+score'&message='+message,function(result,status,xhr) }
if( status.toLowerCase()=="error".toLowerCase() )
{ alert("An Error Occurred.."); }
else {
//alert(result);
$('#sucessMessage').html(result);
}
})
.fail(function(){ alert("something went wrong. Please try again") });
}
Using the mail.php on the same page where the form is could do the magic. the following code did the magic.
<?php
$from = '';
$sendTo = 'email#email.com';
$subject = 'New message from contact form';
$fields = array('name' => 'Name', 'email' => 'Email', 'subject' => 'Subject', 'message' => 'Message');
$okMessage = 'Thank you for your message. I will write back soon.';
$errorMessage = 'There is an error while sending message. Please try again later.';
try {if (!empty($_POST)) {
$emailText = "You have a new message from your contact form\n=====\n";
foreach ($_POST as $key => $value) {
if (isset($fields[$key])) {
$emailText .= "$fields[$key]: $value\n";
}
}
$headers = array('Content-Type: text/plain; charset="UTF-8";',
'From: ' . $from,
'Reply-To: ' . $from,
'Return-Path: ' . $from,
);
mail($sendTo, $subject, $emailText, implode("\n", $headers));
$responseArray = array('type' => 'success', 'message' => $okMessage);
}
}
catch (\Exception $e) {
$responseArray = array('type' => 'danger', 'message' => $e->getMessage());
}
if (!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest') {
$encoded = json_encode($responseArray);
header('Content-Type: application/json');
echo $encoded;
} else {
echo $responseArray['message'];
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
<script>
$(function () {
$('#contact-form').on('submit', function (e) {
if (!e.isDefaultPrevented()) {
var url = "contact.php";
$.ajax({
type: "POST",
url: url,
data: $(this).serialize(),
success: function (data) {
var messageAlert = 'alert-' + data.type;
var messageText = data.message;
var alertBox = '<div class="alert ' + messageAlert + ' alert-dismissable">
<button type="button" class="close" data-dismiss="alert" aria-hidden="true">×
</button>' + messageText + '</div>';
if (messageAlert && messageText) {
$('#contact-form').find('.messages').html(alertBox);
$('#contact-form')[0].reset();
}
}
});
return false;
}
})
});
</script>
</head>
<body>
<div class="container">
<div class="row">
<div class="col-xl-8 offset-xl-2">
<h1>CONTACT FORM</h1><hr>
<p class="lead">By filling out the contact form; You may have information about us and current news.</p>
<form id="contact-form" method="post" role="form" novalidate="true">
<div class="messages"></div>
<div class="controls">
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="form_name">Full Name *</label>
<input id="form_name" type="text" name="name" class="form-control" placeholder="Please fill the name field *" required="required" data-error="You must fill the name field">
<div class="help-block with-errors alert-danger"></div>
</div>
</div>
<div class="col-lg-6"></div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="form_email">E-mail *</label>
<input id="form_email" type="email" name="email" class="form-control" placeholder="Please fill the email field *" required="required" data-error="You must fill the email field">
<div class="help-block with-errors alert-danger"></div>
</div>
</div>
<div class="col-lg-6"></div>
</div>
<div class="row">
<div class="col-lg-6">
<div class="form-group">
<label for="form_subject">Subject *</label>
<input id="form_subject" type="text" name="subject" class="form-control" placeholder="Please fill the subject field *" required="required" data-error="You must fill the subject field">
<div class="help-block with-errors alert-danger"></div>
</div>
</div>
<div class="col-lg-6"></div>
</div>
<div class="form-group">
<label for="form_message">Message *</label>
<textarea id="form_message" name="message" class="form-control" placeholder="Please fill the message field *" rows="4" required="required" data-error="You must fill the message field"></textarea>
<div class="help-block with-errors alert-danger"></div>
</div>
<input type="submit" class="btn btn-success btn-send" value="Submit">
<p class="text-muted" style="padding-top: 5px;"><strong>*</strong>This field must be fill.</p>
</div><!-- controls all end -->
</form><!-- form all end -->
</div><!-- col-xl-8 offset-xl-2 end -->
</div><!-- row all end -->
</div><!-- container all end -->
</body>
Please use this code
<?php
if ($_POST['submit']) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'From: agriindiaexp.com';
$to = 'shridhar.kagi#gmail.com';
$subject = 'Email Inquiry';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (mail ($to, $subject, $body, $from)) {
$success = "Message successfully sent";
} else {
$success = "Message Sending Failed, try again";
}
echo $success;
}
?>

Contact Form don't work

When submitting my form i get a 404 page. Something is missing, or wrong.
It's a wordpress website. I inserted the email in the options of the wordpress and nothing happen. I saw a lot of stack overflow questions but none helped me. I don't want to use plugins.
<form class="form-horizontal" role="form" method="post" action="contact.php">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" value="">
</div>
<br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-envelope"></i></span>
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="">
</div>
<br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-pencil"></i></span>
<textarea class="form-control" rows="4" name="message" placeholder="Message..."></textarea>
</div>
<br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">2 + 3 = ?</span>
<input type="text" class="form-control" id="human" name="human" placeholder="">
</div>
<br/>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-5">
<input id="submit" name="submit" type="submit" value="Enviar" class="btn btn-button">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<! Will be used to display an alert to the user>
</div>
</div>
</form>
PHP Code:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Contact Form';
$to = 'example#domain.com';
$subject = 'Contact ';
$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>';
}
}
}
?>
Create a .php file in theme (your current theme, if have child theme then in thild theme) then put below code in the file and save it.
<?php
/*
* Template Name: Contact us
*/
get_header();
if (isset($_POST["submit"]))
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Contact Form';
$to = 'example#domain.com';
$subject = 'Contact ';
$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 id="primary" class="content-area">
<main id="main" class="site-main" role="main">
<form class="form-horizontal" role="form" method="post" action="#">
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-user"></i></span>
<input type="text" class="form-control" id="name" name="name" placeholder="Name" value="">
</div><br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-envelope"></i></span>
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" value="">
</div><br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1"><i class="glyphicon glyphicon-pencil"></i></span>
<textarea class="form-control" rows="4" name="message" placeholder="Message..."></textarea>
</div><br/>
<div class="input-group">
<span class="input-group-addon" id="basic-addon1">2 + 3 = ?</span>
<input type="text" class="form-control" id="human" name="human" placeholder="">
</div><br/>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-5">
<input id="submit" name="submit" type="submit" value="Enviar" class="btn btn-button">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<!-- Will be used to display an alert to the user -->
</div>
</div>
</form>
</main>
<?php get_sidebar( 'content-bottom' ); ?>
</div>
<?php get_footer(); ?>
Now go to wp-admin and create new page (title may be contact page etc.), select page template "Contact us" from page template dropdown (right side of the page content), publish the page and view the page.
Hope it will help.
First of all thank you for your reply's.
The problem here is the php mail(). I hope i can help someone with this thread.
To reach this conclusion these were the steps:
Tried everything that i saw in stack overflow with the same problem has me;
Install Check Email Plugin;
I did not received any email so i contact my hosting service and they said they will not enable the php mail() to avoid spam.
Solution - When this happen this is the best way to work around.
In action use file location url:
action="`<?php echo get_template_directory()'contact.php';?>`"
Try this.

PHP form validation not working on submit

I have an HTML form that links to a php file. When I click submit the form sends and I get an email. However, if the form is empty and I click submit it still redirects to the homepage.
form HTML:
<form class="form-horizontal" role="form" method="post" action="contact.php">
<div class="form-group">
<label for="name" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="name" name="name" placeholder="First & Last Name">
</div>
</div>
<div class="form-group">
<label for="telephone" class="col-sm-2 control-label">Phone</label>
<div class="col-sm-10">
<input type="telephone" class="form-control" id="telephone" name="telephone" placeholder="Phone Number" >
</div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" id="email" name="email" placeholder="example#domain.com" >
</div>
</div>
<div class="form-group">
<label for="dropdown" class="col-sm-2 control-label">Select One:</label>
<div class="col-sm-10">
<select name="select" class="form-control" id="dropdown">
<option>Driver</option>
<option>Advertiser</option>
</select>
</div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-10">
<textarea class="form-control" rows="4" name="message"></textarea>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
</div>
</div>
</form>
contact.php
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$email = $_POST['email'];
$telephone= $_POST['telephone'];
$select = $_POST['select'];
$message = $_POST['message'];
$from = 'Contact Form';
$to = 'myemail#gmail.com';
$subject = 'Message from Contact Form';
$body ="From: $name\n E-Mail: $email\n Telephone: $telephone\n Select: $select\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errMessage && !$errTel) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
}
{
// To redirect form on a particular page
header("Location:http://www.mywebsite.co");
}
}
?>
Any idea on why my validation is not working?
Thank you.
you have two extra brackets in yor code, this:
{
// To redirect form on a particular page
header("Location:http://www.mywebsite.co");
}
should just be:
// To redirect form on a particular page
header("Location:http://www.mywebsite.co");
you were closing your if block and opening an empty block.
You may wanna recheck this part:
{
// To redirect form on a particular page
header("Location:http://www.mywebsite.co");
}
Also, you should also use trim function on your POST data.
contact.php
if (isset($_POST["submit"])) {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$telephone = trim($_POST['telephone']);
/* Other code */
if (!$errName && !$errEmail && !$errMessage && !$errTel) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later.</div>';
}
} else {
// To redirect form on a particular page
header("Location:http://www.mywebsite.co");
}

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

Is there anything wrong with either my php or my html for my email form? Every time I submit the form online I get a blank page. I'm not sure what else I would have to change since it seems to make sense but I don't know very much about php.
<form class="form-vertical" role="form" method="post" action="index.php">
<fieldset>
<!-- Text input-->
<div class="form-group">
<label class="control-label" for="textinput">Name</label>
<input id="textinput" name="textinput" type="text" placeholder="Enter Name" class="form-control input-md">
</div>
<!-- Text input-->
<div class="form-group">
<label class="control-label" for="Email">Email</label>
<input id="Email" name="Email" type="text" placeholder="Enter Email" class="form-control input-md">
</div>
<!-- Text input-->
<div class="form-group">
<label class="control-label" for="Phone">Phone</label>
<input id="Phone" name="Phone" type="text" placeholder="" class="form-control input-md">
</div>
</section>
</div> <!-- end col-md-4 -->
<div class="col-md-4 message-form">
<section class="wow zoomInUp" data-wow-duration="1.5s" data-wow-delay="0.2s" style="visibility: visible; -webkit-animation-delay: 0.2s; -moz-animation-delay: 0.2s; animation-delay: 0.2s;">
<!-- Textarea -->
<div class="form-group">
<label class="control-label" for="Message">Message</label>
<textarea class="form-control" rows="5" id="Message" name="Message" placeholder="Message"></textarea>
</div>
<!-- Button -->
<div class="form-group">
<label class="control-label" for="Submit"></label>
<button id="Submit" name="Submit" class="btn btn-primary pull-right">Send Message</button>
</div>
</fieldset>
</form>
PHP script
<?php
if ($_POST["Submit"]) {
$textinput = $_POST['textinput'];
$Email = $_POST['Email'];
$Phone = $_POST['Phone'];
$Message = $_POST['Message'];
$from = 'Demo Contact Form';
$to = 'max.henry.campos#gmail.com';
$subject = 'Message from Contact Demo ';
$body = "From: $textinput\n E-Mail: $Email\n Message:\n $Message";
// Check if name has been entered
if (!$_POST['textinput']) {
$errName = 'Please enter your name';
}
// Check if email has been entered and is valid
if (!$_POST['Email'] || !filter_var($_POST['Email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Please enter a valid email address';
}
//Check if message has been entered
if (!$_POST['Message']) {
$errMessage = 'Please enter your message';
}
// If there are no errors, send the email
if (!$errtextinput && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! We will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
}
}
?>
Well, you probably get the blank page because you're not actually returning anything to the browser. You're just setting $result, not outputting it.
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! We will be in touch</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
echo $result; // ?

Categories