I am trying to create a contact form in php. But the problem I am facing is that when ever I try to submit the form it shows 404 exception rather than the actual file which has the contact form. The theme is custom made. I have added my code below
<?php
if(isset($_POST)) {
if(trim($_POST["name"]) === "") {
$nameError = "Please enter your name.";
$hasError = true;
} else {
$name = trim($_POST["name"]);
}
if(trim($_POST["email"]) === "") {
$emailError = "Please enter your email address.";
$hasError = true;
} else if (!preg_match("/^[[:alnum:]][a-z0-9_.-]*#[a-z0-9.-]+\.[a-z]{2,4}$/i", trim($_POST["email"]))) {
$emailError = "You entered an invalid email address.";
$hasError = true;
} else {
$email = trim($_POST["email"]);
}
if(trim($_POST["phone"]) === "") {
$phoneError = "Please enter your phone address.";
$hasError = true;
} else {
$phone = trim($_POST["phone"]);
}
if(trim($_POST["comments"]) ==="") {
$commentError = "Please enter a message.";
$hasError = true;
} else {
if(function_exists("stripslashes")) {
$comments = stripslashes(trim($_POST["comments"]));
} else {
$comments = trim($_POST["comments"]);
}
}
if(!isset($hasError)) {
$emailTo = get_option("tz_email");
if (!isset($emailTo) || ($emailTo == "") ){
$emailTo = get_option("admin_email");
}
$subject = "[PHP Snippets] From ".$name;
$body = "Name: $name \n\nEmail: $email \n\nPhone:$phone\n\nComments: $comments";
$headers = "From: ".$name." <".$emailTo.">" . "\r\n" . "Reply-To: " . $email;
wp_mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
<?php get_header(); ?>
<div class="row" style="margin-top:150px;">
<div class="bg-container"></div>
<div class="col-md-4">
<div class="card contact-form-card">
<form action="<?php esc_url(the_permalink());?>" method="post">
<fieldset>
<legend>Give us your details</legend>
<div class="form-group label-floating">
<label class="control-label" for="user-name">Enter your full name</label>
<input class="form-control" id="user-name" name="name" type="text">
</div>
<div class="form-group label-floating">
<label class="control-label" for="user-email">Enter your email id</label>
<input class="form-control" id="user-email" name="email" type="text">
</div>
<div class="form-group label-floating">
<label class="control-label" for="user-phone">Enter your phone number</label>
<input class="form-control" id="user-phone" name="phone" type="text">
</div>
<div class="form-group label-floating">
<label class="control-label" for="commenets">Comments</label>
<textarea class="form-control" id="commenets" name="comments" type="text"></textarea>
</div>
<button type="submit" name="submit" class="btn btn-primary btn-raised col-xs-12">Submit</button>
</fieldset>
</form>
</div>
</div>
<div class="col-md-8">
<h1 class="text-center">We are currently maintaining over 1000 properties in north Bangalore</h1>
<div class="owl-carousel">
<div class="item card">
<p class="statement">"Poorna is an immaculate real estate consulting professional, and possess several unique skills up his sleeves like property consulting report writing that are rare to find in a professional framework."</p>
<p class="by">Shariq Saleem, Director, Armchair Solutions India</p>
</div>
<div class="item card">
<p class="statement">"Square Capital Assets understands your needs clearly and they are very easy to work with. Once they taken over managing my property, it was a smooth ride"</p>
<p class="by">Manoj</p>
</div>
</div>
</div>
</div>
<div class="row" style="margin-top:15px;">
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/sign_the_agreement.png";?> alt="Sign the agreement">
<h5>Sign with us</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/choose_your_tenants.png";?> alt="Choose your tenants">
<h5>We will find reliable tenants.</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/collect_security_deposit.png";?> alt="Collect Security Deposit">
<h5>Rental Agreement</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/rent_assured.png";?> alt="Rent Assured">
<h5>Collecting rent and keeping your home safe</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/repairs_under_2000.png";?> alt="Repairs Under Rs.2,000">
<h5>Regular check up and maintenance</h5>
</div>
</div>
<div class="col-sm-2">
<div class="col-xs-12 feature-tile text-center">
<img src=<?php echo get_template_directory_uri()."/img/property_tax.png";?> alt="Property Taxes?">
<h5>Managing taxes and bills</h5>
</div>
</div>
</div>
<?php get_footer(); ?>
Just change the fields name like below
<input class="form-control" id="user-name" name="Fname" type="text">
<input class="form-control" id="user-email" name="Femail" type="text">
You will not get 404 error
:)
Naming a field in the form "name" caused that problem.
I renamed it to customer name and it started working
Related
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();
NOTE: the mail function itself works fine, I have a hosted site with mail configured professionally. Mail is sent via this script, but my simple empty field handler never catches empty fields. I'm sure this is something super obvious, but I'm very new to php and the logic of my script seems sound. The code follows:
edit: I've left the form call within the php script in case someone finds issue with the bootstrap form itself. The redirect is temporary until I can get better form error handling going.
<body>
<!-- jQuery and bootstrap -->
<script src="https://code.jquery.com/jquery.js"></script>
<script src="../js/bootstrap.min.js"></script>
<script src="../js/counter.js"></script>
<div class="jumbotron-fluid" id="primary">
<?php
if (isset($_POST['submit'])) {
$from = 'Web_Form_Contact';
$to = 'example#example.com';
$subject = 'Job_Contact';
$c_type = $_POST["c_type"];
if (empty($_POST["c_name"])) {
$errName = 'Info missing: Name';
} else {
$c_name = $_POST["c_name"];
}
if (empty($_POST["c_email"] )) {
$errEmail = 'Please enter a valid email address';
} else {
$c_email = $_POST["c_email"];
}
if (empty($_POST["co_name"])) {
$errCoName = 'Info missing: Company Name';
} else {
$co_name = $_POST["co_name"];
}
if (empty($_POST["j_desc"])) {
$errjDesc = 'Info missing: Job Description';
} else {
$j_desc = $_POST["j_desc"];
}
$body = "From: $c_name\n E-Mail: $c_email\n Type: $c_type\n CoName: $co_name\n Description\n $j_desc";
if (empty($errName && $errEmail && $errCoName && $errjDesc)) {
if (mail ($to, $subject, $body, $from)) {
$result='<div class="alert alert-success">Thank You! I will be in touch\n This page will redirect in 5 seconds.</div>';
} else {
$result='<div class="alert alert-danger">Sorry there was an error sending your message. Please try again later</div>';
}
} else {
echo "$errName\n $errEmail\n $errCoName\n $errjDesc";
}
}
echo "$result";
?>
<div class="container">
<form class="form-horizontal" role="form" method="post" action="php/info.php"
<div class="row">
<div class="col-xs-10 col-sm-6 col-md-6 col-lg-offset-2 col-md-offset-2">
<<h2>Contact Me! <small></small></h2>
<form>
<div class="form-group row">
<label for="clientEmail" class="col-sm-2 form-control-label">Email</label>
<div class="col-sm-10">
<input type="email" name="c_email"class="form-control" id="clientEmail" placeholder="Email" >
</div>
</div>
<div class="form-group row">
<label for="clientName" class="col-sm-2 form-control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="c_name" id="clientName" placeholder="Your Name" >
</div>
</div>
<div class="form-group row">
<label class="col-sm-2">Contact Type</label>
<div class="col-sm-10">
<div class="radio">
<label>
<input type="radio" name="c_type" id="gridRadios1" value="recruiter" checked>
I am a recruiter, and do not work directly for the company hiring.
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="c_type" id="gridRadios2" value="employee">
I work for the company in question, and wish to discuss an open position.
</label>
</div>
<div class="radio">
<label>
<input type="radio" name="c_type" id="gridRadios3" value="other" >
Other
</label>
</div>
</div>
</div>
<div class="form-group row">
<label for="coName" class="col-sm-2 form-control-label">Company Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="co_name" id="coName" placeholder="Company Name" >
</div>
</div>
<!-- Job Description Text Box -->
<div class="form-group row">
<label for="Job Description ">Job Description: (1100 character max!)</label>
<div class="col-sm-10 col-lg-offset-2 col-md-offset-2 col-xl-offset-2 col-sm-offset-2" >
<div class="form-group">
<textarea class="form-control" rows="5" name="j_desc" id="jobDesc" placeholder="Job Description" ></textarea>
<p class="counter">1100</p>
</div>
</div>
</div>
<div class="form-group row">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" id="send" name="submit" value="Send"class="btn btn-secondary">Submit</button>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
<!--redirect script -->
<script>
function redirect(page) {
setTimeout(function () {
window.location.href = page;
}, 5000);
}
//sends to homepage
redirect("../index.html");
</script>
You need to have an empty check for each variable:
if (empty($errName) && empty($errEmail) && empty($errCoName) && empty($errjDesc)) {
You can also set an email flag at the beginning to true, and if you hit any of the errors, you can set it to false. Then when you're ready to email, check to see if that email flag is still true.
I've been working on a website with a contact form. Everything is working fine except that the attachment isn't mailed. Here are the HTML and PHP files:
(And this is the first time I ask question in this website, I apologise for this terrible formatting, thanks in advance)
<? php
require_once 'phpmailer/PHPMailerAutoload.php';
if (isset($_POST['name'])) {
$name = $_POST['name'];
}
if (isset($_POST['email'])) {
$email_address = $_POST['email'];
}
if (isset($_POST['phone'])) {
$phone = $_POST['phone'];
}
if (isset($_POST['comment'])) {
$message = $_POST['comment'];
}
$body_message = '<p>You have a new message from your website contact form.</p> </br> </br> <p> Here are the deatils:</p> </br> </br> <p>Name: </p>'.$name.
'<p>Email: </p>'.$email_address.
'<p>Phone: </p> '.$phone.
'</br> <p>Message: </p> </br>'.$message;
$mail = new PHPMailer;
$mail - > IsSMTP();
$mail - > Host = 'mail.ronaldtest.com';
$mail - > Username = 'Ronald#ronaldtest.com';
$mail - > Password = '151588';
$mail - > Port = 587;
$mail - > addAddress('ronaldng1588#gmail.com');
$mail - > setFrom('ronald#ronaldtest.com', 'Brighten Management Auto Mailer');
$mail - > Subject = 'Website Contact Form: $name';
$mail - > Body = $body_message;
if (isset($_FILES['attachmentFile']) && $_FILES['attachmentFile']['error'] == UPLOAD_ERR_OK) {
$mail - > AddAttachment($_FILES['attachmentFile']['tmp_name'],
$_FILES['attachmentFile']['name']);
}
$mail - > IsHTML(true);
if ($_POST["submit"]) {
if (!$_POST['name']) {
$error = "<br />Please enter your name";
}
if (!$_POST['email']) {
$error. = "<br />Please enter your email address";
}
if (!$_POST['phone']) {
$error. = "<br />Please enter your phone number";
}
if (!$_POST['comment']) {
$error. = "<br />Please enter a comment";
}
if ($error) {
$result = '<div class="alert alert-danger" There were error(s)
in your form: '.$error.
'</div>';
} else {
if (!$mail - > Send()) {
$result = '<div class="alert alert-danger">Sorry, there was
an error sending your message. Please try again later.</div>';
} else {
$result = '<div class="alert alert-success">Thank you, <strong>'.$_POST['name'].
'</strong>! I will be in touch!</div';;
}
}
}
?>
<section id="contact">
<? php include ( 'email.php'); ?>
<div class="container">
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<div class="feature_header text-center">
<h3 class="feature_title">Keep In <b>touch</b></h3>
<h4 class="feature_sub">Contact us for future improvements</h4>
<div class="divider"></div>
<?php if (isset($result)){ echo $result; } ?>
</div>
</div>
<div class="row">
<div class="contact_full">
<form method="post">
<div class="col-md-6 left">
<div class="left_contact">
<div class="control-group">
<div class="form-level controls">
<input type="text" name="name" class="input-block" placeholder="Your Name" value="<?php
if (isset($_POST[" name "])){
echo $_POST['name']; }?>"/>
<span class="form-icon fa fa-user"></span>
</div>
</div>
<div class="control-group">
<div class="form-level controls">
<input type="email" name="email" class="input-block" placeholder="Your Email" value="<?php
if (isset($_POST[" email "])){
echo $_POST['email']; }?>" />
<span class="form-icon fa fa-envelope-o"></span>
</div>
</div>
<div class="control-group">
<div class="form-level controls">
<input type="phone" name="phone" class="input-block" placeholder="Your Phone Number" value="<?php
if (isset($_POST[" phone "])){
echo $_POST['phone']; }?>" />
<span class="form-icon fa fa-phone"></span>
</div>
</div>
</div>
</div>
<div class="col-md-6 right">
<div class="control-group">
<div class="form-level controls">
<input type="file" name="attachmentFile" id="attachmentFile" value="<?php echo $_FILES['attachmentFile']['tmp_name'] ?>" />
<span class="fa fa-file"></span>
</div>
</div>
<div class="control-group">
<div class="form-level controls">
<textarea class="textarea-block" name="comment" rows="4" value="<?php
if (isset($_POST[" comment "])){
echo $_POST['comment']; }?>"></textarea>
<span class="form-icon fa fa-pencil"></span>
</div>
</div>
</div>
<div class="col-md-12 text-center">
<input type="submit" name="submit" class="btn btn-main featured" value="Submit Now" />
</div>
</form>
</div>
</div>
</div>
</div>
</section>
for a form to upload a file it has to have enctype="multipart/form-data" in the form tag. so in your case:
<form method="post" enctype="multipart/form-data">
Hy everybody,
this is the website I'm building using the Bootstrap togglable tab feature to handle the content I want to show (on the right side).
My problem comes when I want to submit a contact form with php because the eventual errors are correctly shown in the Contact page but the form automatically redirects to the home page since there is no proper link to the contact page itself. The question might be silly but I cannot find a way to be redirected on the same Contact page when I submit the form. Any help?
Here is the HTML code:
<div class="container-fluid">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<h3>MARCELLO GIOVENCO PHOTOGRAPHY</h3>
</div>
</div>
<!-- Menu -->
<div class="container-fluid">
<div class="row">
<div class="col-sm-3">
<div class="collapse navbar-collapse" id="myNavbar">
<ul class="nav nav-pills nav-stacked">
<li class="dropdown">
PROJECTS
<ul class="dropdown-menu" role="menu" aria-labelledby="myTabDrop1">
<li>NAPOLI</li>
<li>PORTRAITS</li>
</ul>
</li>
<li>BIO</li>
<li>CONTACT</li>
<li>BLABLABLA</li>
</ul>
</div>
</div>
<!-- Content -->
<div class="col-sm-9">
<div id="myNavbar" class="tab-content">
<div class="tab-pane fade active in" id="home">
<p>home</p>
</div>
<div class="tab-pane fade" id="napoli">
<p>napoli</p>
</div>
<div class="tab-pane fade" id="portraits">
<p>portraits</p>
</div>
<div class="tab-pane fade" id="bio">
<p>bio</p>
</div>
<div class="tab-pane fade" id="contact">
<?php include('contact.php');?>
</div>
</div>
</div>
</div>
</div>
and this is the PHP code for contact.php
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'www.marcellogiovenco.com';
$to = 'marcellomariagiovenco#gmail.com';
$subject = "$name sent you a message!";
$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';
} else {
$name = test_input($_POST["name"]);
// check if name only contains letters and whitespace
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$errName = "Only letters and white space allowed";
}
}
// 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 for the message! 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>';
}
}
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form id="myForm" 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-5">
<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 class="col-sm-5"></div>
</div>
<div class="form-group">
<label for="email" class="col-sm-2 control-label">Email</label>
<div class="col-sm-5">
<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 class="col-sm-5"></div>
</div>
<div class="form-group">
<label for="message" class="col-sm-2 control-label">Message</label>
<div class="col-sm-5">
<textarea class="form-control" rows="4" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
<div class="col-sm-5"></div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-5">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
<div class="col-sm-5"></div>
</div>
<div class="form-group">
<div class="col-sm-5 col-sm-offset-2">
<p>All fields are required.</p>
</div>
</div>
<div class="form-group">
<div class="col-sm- 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-5 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</form>
From what I understand , your problem is that when you click on the "submit " button is redirected to the index or to the first tab . As I ran until I got this script that redirects me to the tab where the submit button that I clicked is .
$(document).ready(function() {
if(location.hash) {
$('a[href=' + location.hash + ']').tab('show');
}
$(document.body).on("click", "a[data-toggle]", function(event) {
location.hash = this.getAttribute("href");
});
});
$(window).on('popstate', function() {
var anchor = location.hash || $("a[data-toggle=tab]").first().attr("href");
$('a[href=' + anchor + ']').tab('show');
});
Hope that it can help you.
Use contact.php as action for the form. Then, at the end of the if, redirect the user to a confirmation page or to the form (in case of errors) using the header() function.
You can use jQuery to submit your form with ajax.
http://api.jquery.com/jquery.ajax/
$('#myForm').on('submit', function() {
$.ajax({
url: 'some-url',
type: 'post',
dataType: 'json',
data: $('form#myForm').serialize(),
success: function(data) {
// Show success message
}
});
});
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