I just made my contact form to function, sort of. It has a few issues:
When the form is sent, it kicks you off the page, directs you to a blank page with the message: "Thank you etc". I would love to stay on the page, and just either get a box pop-up with the success message or just have it on the page. Either is fine :)
The error message doesn't show, it just puts you on a blank page.
The errMessages like "please fill in a valid e-mail" don't show. Probably because I removed the labels, but I would like to show that message IN the box rather than under it.
It's overriding my CSS! But that happens sometimes on my computer, one time I see lines other times I see boxes. So that issue might be somewhere else. However with the 'working form' it changes my last box.
This is what I meant with changing CSS on my boxes
PHP code:
<?php
if (isset($_POST["submit"])) {
$name = $_POST['name'];
$subject = $_POST['subject'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$from = 'Demo Contact Form';
$to = 'denise#hetfulfilmentbedrijf.nl';
$subject = 'Message from Contact Demo';
$body = "From: $name\n Subject: $subject\n Number: $phone\n E-Mail: $email\n Message:\n $message";
//Check if name has been entered
if(!$_POST['name']) {
$errName = 'Vul alsjeblieft je naam in';
}
//Check if subject has been entered
if(!$_POST['subject']) {
$errName = 'Vul alsjeblieft een onderwerp in';
}
//Check if number has been entered
if(!$_POST['phone']) {
$errName = 'Vul alsjeblieft je nummer in';
}
//Check if e-mail is entered and valid
if(!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Vul alsjeblieft je e-mailadres in';
}
//Check if message has been entered
if(!$_POST['email']) {
$errMessage = 'Laat alsjeblieft een bericht achter';
}
// If no errors, send email
if (!$errName && !$errSubject && !$errPhone && !$errEmail && !$errMessage) {
if (mail ($to, $subject, $body, $from)) {
echo $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>';
}
}
}
?>
HTML:
<form class="form-horizontal" role="form" method="post" action="partials/contactform.php">
<div class="form-group offset-top-45">
<textarea rows="11" cols="100" class="form-control" id="message" name="message" placeholder="Laat hier je bericht voor ons achter:"></textarea><?php echo htmlspecialchars($_POST['message']);?></textarea>
<span class="help-block" style="display: none;">Laat alsjeblieft een bericht achter</span>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="col-md-6">
<div class="row offset-top-10">
<div class="pull-right">
<img height="60" width="100" src="/images/stamp.png" alt="stamp">
</div>
</div>
<div class="row offset-top-10" style="padding-right:20px; padding-left:10px">
<form role="form" id="feedbackForm">
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Hoe heet je?" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<span class="help-block" style="display: none;">Vul alsjeblieft je naam in</span>
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Wat is het onderwerp?" value="<?php echo htmlspecialchars($_POST['subject']); ?>">
<span class="help-block" style="display: none;">Vul alsjeblieft een onderwerpin</span>
<?php echo "<p class='text-danger'>$errSubject</p>";?>
</div>
<div class="form-group">
<input type="text" class="form-control" id="phone" name="phone" placeholder="Op welk nummer kunnen wij jou bereiken?" value="<?php echo htmlspecialchars($_POST['phone']); ?>">
<span class="help-block" style="display: none;">Vul alsjeblieft je nummer in</span>
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" name="email" placeholder="Wat is je e-mailadres?">
<span class="help-block" style="display: none;">Vul alsjeblieft je e-mail in</span>
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
<div class="form-group">
<div class="col-md-12 text-right">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary btn-s">
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</div>
</div>
</form>
Help is much appreciated!
Full script it should work without any problems
<?php
$result = "";
$errors = ""; //use to count errors
//Error message variables
$errName = "";
$errSubject = "";
$errEmail = "";
$errMessage = "";
//message variables
$name = "";
$subject = "";
$phone = "";
$email = "";
$message = "";
$to = "denise#hetfulfilmentbedrijf.nl";
if (isset($_POST['submit'])) {
//check if name has been entered
if (empty($_POST['name'])) {
$errName = "Vul alsjeblieft je naam in";
$errors++;
} else {
$name = UserInput($_POST['name']);
}
////Check if subject has been entered
if (empty($_POST['subject'])) {
$errSubject = "Vul alsjeblieft een onderwerp in";
$errors++;
} else {
$Subject = UserInput($_POST['subject']);
}
//check if email entered
if (empty($_POST['email'])) {
$errEmail = "Laat alsjeblieft een bericht achter";
$errors++;
} else {
$email = UserInput($_POST['email']);
// check if email is valid
if (!preg_match("/([\w\-]+\#[\w\-]+\.[\w\-]+)/", $email)) {
$errEmail = "Vul alsjeblieft je e-mailadres in";
$errors++;
}
}
if (empty($_POST['phone'])) {
$errPhone = "Vul alsjeblieft je nummer in";
$errors++;
} else {
$phone = UserInput($_POST['phone']);
// check if email is numbers
if (!is_numeric($phone)) {
$errPhone = "enter numbers only";
$errors++;
}
}
//check message
if (empty($_POST['message'])) {
$errMessage = "Laat alsjeblieft een bericht achter";
} else {
$message = UserInput($_POST['message']);
}
if ($errors > 0) {
// we have errors do not send email
$result = "<div class=\"alert alert-danger\">Sorry there was an error sending your message. Please fix " . $errors . " errors on the form </div>";
} else {
//no errors set email headers and send email
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: <' . $email . '>' . "\r\n";
$body = "<p> New email from $name";
$body .= "<p> Phone : $phone</p>";
$body .= "<p> Email : $email<p>";
$body .= "<p>Message : $message</p>";
if (mail($email, $subject, $msg, $header)) {
$result = "<div class=\"alert alert-success\">Thank You! I will be in touch</div>";
$_POST = array(); //clear the form aftter sendig
} else {
$result = "<div class=\"alert alert-danger\">Sorry there was an error sending your message. Please try again later</div>";
}
}
}
//sanitise use input
function UserInput($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form class="form-horizontal" role="form" method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<div class="form-group">
<div class="col-md-12 text-right">
<input id="submit" name="submit" type="submit" value="Send" class="btn btn-primary btn-s">
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<?php echo $result; ?>
</div>
</div>
</div>
</div>
<div class="form-group offset-top-45">
<textarea rows="11" cols="100" class="form-control" id="message" name="message" placeholder="Laat hier je bericht voor ons achter:"></textarea><?php if(!empty($_POST['message'])){echo $_POST['message'];}?></textarea>
<?php echo "<p class=\"text-danger\">".$errMessage."</p>";?>
</div>
</div>
<div class="col-md-6">
<div class="row offset-top-10">
<div class="pull-right">
<img height="60" width="100" src="/images/stamp.png" alt="stamp">
</div>
</div>
<div class="row offset-top-10" style="padding-right:20px; padding-left:10px">
<form role="form" id="feedbackForm">
<div class="form-group">
<input type="text" class="form-control" id="name" name="name" placeholder="Hoe heet je?" <?php if(!empty($_POST['name'])){echo "value=\"".$_POST['name']."\"";}?>>
<?php echo "<p class=\"text-danger\">".$errName."</p>";?>
</div>
<div class="form-group">
<input type="text" class="form-control" id="subject" name="subject" placeholder="Wat is het onderwerp?" <?php if(!empty($_POST['Subject'])){echo "value=\"".$_POST['Subject']."\"";}?>>
<?php echo "<p class=\"text-danger\">".$errSubject."</p>";?>
</div>
<div class="form-group">
<input type="text" class="form-control" id="phone" name="phone" placeholder="Op welk nummer kunnen wij jou bereiken?" <?php if(!empty($_POST['phone'])){echo "value=\"".$_POST['phone']."\"";}?>>
<?php echo "<p class=\"text-danger\">".$errPhone."</p>";?>
</div>
<div class="form-group">
<input type="email" class="form-control" id="email" name="email" placeholder="Wat is je e-mailadres?">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
</form>
Let me know if you have any question or need any help.
Related
The below HTML and PHP code executes as expected in Google Chrome - enter the requested data into the form and press send, then a message is sent to an email address and the page is refreshed.
However, the same expected behaviour does not occur in Safari - once the 'send' button is clicked the user is brought to a blank white page (the email.php file).
Can anyone tell me why it does not do as expected in Safari?
HTML Code:
<form method="POST" id="contactForm" name="contactForm" class="contactForm" novalidate="novalidate" action="email.php" onsubmit="this.submit(); this.reset(); return false;">
<div class="row">
<!-- Name Input -->
<div class="col-md-1 col-sm-1">
<div class="form-group">
<label class="contact-text">Name:</label>
</div>
</div>
<div class="col-md-11 col-sm-11 contact-col">
<div class="form-group">
<input type="text" class="form-control" name="name" id="name">
</div>
</div>
<!-- Email Input -->
<div class="col-md-1 col-sm-1">
<div class="form-group">
<label class="contact-text">Email:</label>
</div>
</div>
<div class="col-md-11 col-sm-11 contact-col">
<div class="form-group">
<input type="email" class="form-control" name="email" id="email">
</div>
</div>
<!-- Message Input -->
<div class="col-md-1 col-sm-1">
<div class="form-group">
<label class="contact-text">Message:</label>
</div>
</div>
<div class="col-md-11 col-sm-11 contact-col">
<div class="form-group">
<textarea class="form-control" name="message" id="message" cols="30" rows="6"></textarea>
</div>
</div>
</div>
</div>
</div>
<!-- Submit Button -->
<div class="col-md-8 col-sm-8 col-8 text-end">
<div class="form-group">
<button type="submit" class="btn btn-primary">Send</button>
<div class="submitting"></div>
</div>
</div>
</div>
</form>
PHP Code:
$errors = [];
$errorMessage = '';
if (!empty($_POST)) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (empty($name)) {
$errors[] = 'Name is empty';
}
if (empty($email)) {
$errors[] = 'Email is empty';
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors[] = 'Email is invalid';
}
if (empty($message)) {
$errors[] = 'Message is empty';
}
if (empty($errors)) {
$toEmail = 'cillinlyons#outlook.com';
$emailSubject = 'New email from your contant form';
$headers = ['From' => $email, 'Reply-To' => $email, 'Content-type' => 'text/html; charset=iso-8859-1'];
$bodyParagraphs = ["Name: {$name}", "Email: {$email}", "Message:", $message];
$body = join('<br>', $bodyParagraphs);
if (mail($toEmail, $emailSubject, $body, $headers)) {
header('Location: https://cillinlyons.me');
} else {
$errorMessage = 'Oops, something went wrong. Please try again later';
}
} else {
$allErrors = join('<br/>', $errors);
$errorMessage = "<p style='color: red;'>{$allErrors}</p>";
}
}
?>
you need a name for your button to set it with php
<button type="submit" class="btn btn-primary" name="submit">Send</button>
and use array_push() to have all errors in a array ,
i used isset() for check the submit is clicked or not.
<?php
$errors = [];
$errorMessage = '';
if(isset($_POST['submit'])) {
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
if (empty($name)) {
array_push($errors, 'Name is empty');
}
if (empty($email)) {
array_push($errors, 'Email is empty');
} else if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
array_push($errors, 'Email is invalid');
}
if (empty($message)) {
array_push($errors, 'Message is empty');
}
if (empty($errors)) {
$toEmail = 'cillinlyons#outlook.com';
$emailSubject = 'New email from your contant form';
$headers = ['From' => $email, 'Reply-To' => $email, 'Content-type' => 'text/html; charset=iso-8859-1'];
$bodyParagraphs = ["Name: {$name}", "Email: {$email}", "Message:", $message];
$body = join('<br>', $bodyParagraphs);
if (mail($toEmail, $emailSubject, $body, $headers)) {
header('Location: https://cillinlyons.me');
} else {
$errorMessage = 'Oops, something went wrong. Please try again later';
}
} else {
foreach ($errors as $error)
echo "<p style='color: red;'>{$error}</p>";
}}
i tested this on safari and its working
I tried this in my code and I entered the right secret key just didn't want to put it out there but everytime I submit my form even with it checked it shows up with the error that it isn't filled out, without the captacha stuff in the form it works just fine. Can someone please help me fix this issue I would like it to make sure you filled it out before it sends it to my email!!
<h2 class="text-center" id="whatwedo">Contact form</h2><hr class="titlehr"></div></div><br>
<?php
if (isset($_POST["submit"])) {
$fullname = $_POST['fullname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$subject = $_POST['subject'];
$description = $_POST['description'];
$captcha = $_POST['g-recaptcha-response'];
$from = $fullname;
$to = 'mcgarrywebdesign#gmail.com';
$subject = 'Contact Form';
$body = "From: $fullname\n E-Mail: $email\n Phone: $phone\n Subject: $subject\n description: $description";
// Check if name has been entered
if (!$_POST['fullname']) {
$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['phone']) {
$errPhone = 'Please enter your phone number';
}
if (!$_POST['subject']) {
$errSubject = 'Please enter the subject';
}
if (!$_POST['description']) {
$errDescription = 'Please enter the description';
}
if(!$captcha){
$errcaptcha = "Please check the the captcha form";
}
// If there are no errors, send the email
if (!$errName && !$errEmail && !$errPhone && !$errSubject && !$errDescription && !$errcaptcha) {
$secretKey = "secret key";
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
if(intval($responseKeys["success"]) !== 1) {
$result = "You are spammer ! Get the #$%K out";
} else {
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>';
}
}
}}
?>
<?php echo "<p class='text-danger'>$result</p>";?>
<div class="row">
<div class="col-md-12 col-lg-6"> <form action="contact.php" method="post">
<div class="form-group">
<label for="exampleInputEmail1">Full Name</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Full Name" name="fullname">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="form-group">
<label for="exampleInputPassword1">Email Address</label>
<input type="email" class="form-control" id="exampleInputPassword1" placeholder="Enter Email" name="email">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
<div class="form-group">
<label for="exampleInputEmail1">Phone Number</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Phone Number" name="phone">
<?php echo "<p class='text-danger'>$errPhone</p>";?>
</div>
</div>
<div class="col-md-12 col-lg-6">
<div class="form-group">
<label for="exampleInputEmail1">Subject</label>
<input type="text" class="form-control" id="exampleInputEmail1" aria-describedby="emailHelp" placeholder="Subject" name="subject">
<?php echo "<p class='text-danger'>$errSubject</p>";?>
</div>
<div class="form-group">
<label for="exampleTextarea">Description</label>
<textarea class="form-control" id="exampleTextarea" rows="3" name="description" placeholder="Description"></textarea>
<?php echo "<p class='text-danger'>$errDescription</p>";?>
</div>
<br>
</div></div>
<div class="g-recaptcha text-center mx-auto d-block" data-sitekey="6LdhyXcUAAAAANrj8qTSLKcrbjVX6ij07Dqw0awe"></div>
<?php echo "<p class='text-danger'>$errcaptcha</p>";?>
<div class="row"><div class="col-lg-12"><button type="submit" name="submit" value="send" class="btn btn-primary mx-auto d-block" style="width: 190px !important;height: 60px !important;font-size: 25px;">Submit</button></div></div></form>
</div>
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?
Hey StackOverflow Community, i have a problem with my Contact Form.
I used this PHP Script:
<?php
if ($_POST["submit"]) {
$name = $_POST['name'];
$email = $_POST['phone'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Kontaktformular';
$to = 'email#mail.com';
$subject = 'Buchungsanfrage - $name';
$body ="From: $name\n: $phone\n E-Mail: $email\n Message:\n $message";
// Check if name has been entered
if (!$_POST['name']) {
$errName = 'Bitte geben Sie den Namen ein.';
}
// Check if email has been entered and is valid
if (!$_POST['email'] || !filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)) {
$errEmail = 'Bitte geben Sie eine gültige E-Mail Adresse ein.';
}
//Check if message has been entered
if (!$_POST['message']) {
$errMessage = 'Bitte hinterlassen Sie mir eine Nachricht.';
}
//Check if simple anti-bot test is correct
if ($human !== 5) {
$errHuman = 'Ihre Anti-Span Antwort war falsch.';
}
// 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">Danke! Ich werde mich schnellstmöglich bei Ihnen melden.</div>';
} else {
$result='<div class="alert alert-danger">Entschuldigung, beim versenden Ihrer Nachricht ist etwas schief gelaufen. Bitte versuchen Sie es nochmal.</div>';
}
}
}
?>
And this HTML for my Form:
<div class="block block-primary-head no-pad">
<h3><i class="fa fa-pencil"></i> Buchungsanfrage</h3>
<div class="block-content">
<form role="form" method="post" action="fotografie.php">
<div class="form-group">
<label>Name*</label>
<input type="text" class="form-control" id="name" name="name" placeholder="Vor- und Nachname" value="<?php echo htmlspecialchars($_POST['name']); ?>">
<?php echo "<p class='text-danger'>$errName</p>";?>
</div>
<div class="form-group">
<label>Telefonnummer</label>
<input type="text" class="form-control" id="phone" name="phone" placeholder="Ihre Telefonnummer" value="<?php echo htmlspecialchars($_POST['phone']); ?>">
</div>
<div class="form-group">
<label>E-Mail Adresse</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Ihre E-Mail Adresse" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
<div class="form-group">
<label>Nachricht</label>
<textarea class="form-control" id="message" name="message"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
<div class="form-group">
<label>2 + 3 = ?</label>
<input type="text" class="form-control" id="human" name="human" placeholder="Ihre Antwort">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</div>
</div>
<div class="form-group">
<?php echo $result; ?>
</div>
<button type="submit" id="submit" name="submit" value="send" class="btn btn-primary">Absenden</button>
</form>
</div>
But when i go on my side now, i got the following error:
Notice: Undefined index: submit in ..\htdocs\projects\bootstrap-themes\001\contact\form-fotografie.php on line 2
Line 2 is following code:
if ($_POST["submit"]) {
What could be the problem?
Thanks for your help :-)
It looks like you are trying to check if the form is submitted. You need to use the isset function for this as below
Line 2:
if ($_POST["submit"]) {
Should read:
if (isset($_POST["submit"])) {
EDIT:
You should always check if the variable is set before using it so to correct your html you need to change the following:
<div class="form-group">
<label>E-Mail Adresse</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Ihre E-Mail Adresse" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</div>
To:
<div class="form-group">
<label>E-Mail Adresse</label>
<input type="email" class="form-control" id="email" name="email" placeholder="Ihre E-Mail Adresse" value="<?php if(isset($_POST['email'])) { echo htmlspecialchars($_POST['email']); } ?>">
<?php if(isset($errEmail) && ($errEmail) != "") { echo "<p class='text-danger'>$errEmail</p>"; } ?>
</div>
You need to make this change for all of the form-groups with the relevant variable names, let me know how you get on
I am trying to get a mail script to work (well, it's working except for one part). I added a dropdown menu, but for some reason it's value is empty when reading the mail that was sent.
The dropdown menu that I got:
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Type werkzaamheden</label>
<select type="select" id="select" name="select">
<option value="kameronderhoud">kameronderhoud</option>
<option value="glas-bewassing">glas-bewassing</option>
<option value="specialistisch reinigen">specialistisch reinigen</option>
<option value="vloer-onderhoud">vloer-onderhoud</option>
<option value="woningontruiming">woningontruiming</option>
<option value="gevel-onderhoud">gevel-onderhoud</option>
<option value="overige">overige</option>
</select>
</div>
</div>
</div>
These are my POSTS:
$tel = $_POST['phone'];
$name = $_POST['name'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$select = $_POST['select'];
$comments = $_POST['comments'];
$phone = $_POST['phone'];
All of them work except select.
Edit: here is the full form
$e_body = "Er is contact opgenomen door $name." . PHP_EOL . PHP_EOL;
$e_content = "<br><br>
Gegevens:<br>
Naam: $name <br>
E-mail: $email <br>
Tel: $phone <br>
Type: $select <br>
$commentsb<br> " . PHP_EOL . PHP_EOL;
$e_reply = " $email2";
Everything displays correctly except the select part. Anybody know what could be causing that?
If it's needed, here is a working part of the form (HTML):
<form method="post" id="contactform" name="contactform" class="contact-form" action="mail/contact.php" type="contact">
<div class="col-md-12">
<h2 class="short">Vraag een <strong>Offerte</strong> aan</h2>
<div class="row">
<div class="form-group">
<div class="col-md-6">
<label>Uw naam*</label>
<input type="text" id="name" name="name" class="form-control input-lg" placeholder="">
</div>
<div class="col-md-6">
<label>Uw mail*</label>
<input type="email" id="email" name="email" class="form-control input-lg" placeholder="">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Type werkzaamheden</label>
<select type="select" id="select" name="select">
<option value="kameronderhoud">kameronderhoud</option>
<option value="glas-bewassing">glas-bewassing</option>
<option value="specialistisch reinigen">specialistisch reinigen</option>
<option value="vloer-onderhoud">vloer-onderhoud</option>
<option value="woningontruiming">woningontruiming</option>
<option value="gevel-onderhoud">gevel-onderhoud</option>
<option value="overige">overige</option>
</select>
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Uw telefoonnummer</label>
<input type="text" id="phone" name="phone" class="form-control input-lg" placeholder="">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Onderwerp</label>
<input type="text" id="email2" name="email2" class="form-control input-lg" placeholder="">
</div>
</div>
</div>
<div class="row">
<div class="form-group">
<div class="col-md-12">
<label>Bericht</label>
<textarea cols="6" rows="7" id="comments" name="comments" class="form-control input-lg" placeholder=""></textarea>
</div>
</div>
</div>
<div class="col-md-12">
<input id="submit" name="submit" type="submit" class="btn btn-primary btn-lg pull-right" value="Verstuur">
</div>
</div>
</form>
PHP script:
<?php
if(!$_POST) exit;
// Email address verification, do not edit.
function isEmail($email) {
return(preg_match("/^[-_.[:alnum:]]+#((([[:alnum:]]|[[:alnum:]][[:alnum:]-]*[[:alnum:]])\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|me|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)$|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i",$email));
}
if (!defined("PHP_EOL")) define("PHP_EOL", "\r\n");
$tel = $_POST['phone'];
$name = $_POST['name'];
$email = $_POST['email'];
$email2 = $_POST['email2'];
$select = $_POST['select'];
$comments = $_POST['comments'];
$phone = $_POST['phone'];
if ($_POST['form_type'] == 'contact'){
}else{
if(trim($name) == '') {
echo '<div style="color:red;border:solid 1px red;" class="alert alert-error">Vul uw naam in.</div>';
exit();
} else if(trim($email) == '') {
echo '<div style="color:red;border:solid 1px red;" class="alert alert-error">Vul uw email adres in.</div>';
exit();
} else if(!isEmail($email)) {
echo '<div style="color:red;border:solid 1px red;" class="alert alert-error">Vul een geldige emailadres in.</div>';
exit();
} else if(trim($phone) == '') {
echo '<div style="color:red;border:solid 1px red;" class="alert alert-error">Vul uw telefoonnummer in.</div>';
exit();
}
if(get_magic_quotes_gpc()) {
$comments = stripslashes($comments);
}
}
// Configuration option.
// Enter the email address that you want to emails to be sent to.
// Example $address = "joe.doe#yourdomain.com";
//$address = "example#themeforest.net";
$address = "*email*";
// Configuration option.
// i.e. The standard subject will appear as, "You've been contacted by John Doe."
// Example, $e_subject = '$name . ' has contacted you via Your Website.';
if ($_POST['form_type'] == 'contact'){
$e_subject = 'Terugbelafspraak';
}else{
$e_subject = 'Contact Form';
}
// Configuration option.
// You can change this if you feel that you need to.
// Developers, you may wish to add more fields to the form, in which case you must be sure to add them here.
if ($_POST['form_type'] == 'contact'){
$e_body = "Er is een terugbelverzoek gemaakt." . PHP_EOL . PHP_EOL;
$e_content = "" . PHP_EOL . PHP_EOL;
$e_reply = "Nummer: $tel";
}else{
if($comments == ''){
$commentsb = '';
}else{
$commentsb = 'Bericht:'.$comments.'';
}
$e_body = "Er is contact opgenomen door $name." . PHP_EOL . PHP_EOL;
$e_content = "<br><br>
Gegevens:<br>
Naam: $name <br>
E-mail: $email <br>
Tel: $phone <br>
Type: $select <br>
$commentsb<br> " . PHP_EOL . PHP_EOL;
$e_reply = " $email2";
}
$msg = wordwrap( $e_body . $e_content . $e_reply, 70 );
$headers = "From: *email*" . PHP_EOL;
$headers .= "Reply-To: $email" . PHP_EOL;
$headers .= "MIME-Version: 1.0" . PHP_EOL;
$headers .= "Content-type: text/html; charset=utf-8" . PHP_EOL;
$headers .= "Content-Transfer-Encoding: quoted-printable" . PHP_EOL;
if(mail($address, $e_subject, $msg, $headers)) {
// Email has sent successfully, echo a success page.
if ($_POST['form_type'] == 'contact'){
echo "<div class='alert alert-success'>";
echo "<h3 style='text-transform:none;'>Aanvraag is verzonden.</h3><br>";
echo "<p>Er zal zo spoedig mogelijk contact opgenomen worden door ons.</p>";
echo "</div>";
}
if ($_POST['form_type'] != 'contact'){
echo "<div class='alert alert-success'>";
echo "<h3 style='text-transform:none;'>Email is verzonden.</h3><br>";
echo "<p>Bedankt <strong>$name</strong>, uw mail is ontvangen door sfsdf.</p>";
echo "</div>";
}
} else {
echo 'ERROR!';
}