Notice: Undefined index: submit in (Path) - php

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

Related

What is wrong with my php mail contact form?

I'm using a template for a contact form for my website. The problem is, when I test it on my server I cannot send a message. It will only give me my error message and not send. Did something happen when I translated the original send text to German?
I've tried debugging the vars. I've also tried to echo some string inputs.
This is the html code:
<form id="contact-form" action="mail.php" method="post">
<div class="row">
<div class="col-md-6 form-group">
<label class="sr-only">Name</label>
<input type="text" class="form-control input-lg" name="name" placeholder="Name" >
<p class="help-block text-danger"></p>
</div>
<div class="col-md-6 form-group">
<label class="sr-only">Email</label>
<input type="email" class="form-control input-lg" name="email" placeholder="Email" >
<p class="help-block text-danger"></p>
</div>
<div class="col-md-12 form-group">
<label class="sr-only">Betreff</label>
<input type="text" class="form-control input-lg" name="subject" placeholder="Betreff" >
<p class="help-block text-danger"></p>
</div>
<div class="col-md-12 form-group">
<textarea class="form-control input-lg" rows="7" name="message" placeholder="Nachricht"></textarea>
<p class="help-block text-danger"></p>
</div>
<div class="col-md-12 text-center">
<button type="submit" class="btn btn-lg btn-round btn-dark">Senden</button>
</div>
</div>
</form>
PHP:
<?php
// POST GET.
if ($_SERVER["REQUEST_METHOD"] == "POST") {
// Form Felder GET.
$name = strip_tags(trim($_POST["name"]));
$name = str_replace(array("\r","\n"),array(" "," "),$name);
$email = filter_var(trim($_POST["email"]), FILTER_SANITIZE_EMAIL);
$subject = trim($_POST["betreff"]);
$message = trim($_POST["nachricht"]);
// Check ob Daten an den mailer.
if ( empty($name) OR empty($subject) OR empty($message) OR !filter_var($email, FILTER_VALIDATE_EMAIL)) {
// 400 (bad request) und raus.
http_response_code(400);
echo "Bitte füllen Sie alle Felder aus.";
exit;
}
// Empfaenger.
$recipient = "info#test.de";
// Betreff.
$subject = "Neue Anfrage von $name";
// Inhalt.
$email_content = "Name: $name\n";
$email_content .= "Email: $email\n\n";
$email_content .= "Betreff: $subject\n\n";
$email_content .= "Nachricht:\n$message\n";
// Header.
$email_headers = "Von: $name <$email>";
// Senden.
if (mail($recipient, $subject, $email_content, $email_headers)) {
// 200 (okay).
http_response_code(200);
echo "Vielen Dank! Deine Nachricht wurde versendet.";
} else {
// 500 (internal server error).
http_response_code(500);
echo "Oops! Hier ist ein Fehler passiert, deine Nachricht konnte nicht gesendet werden.";
}
} else {
// 403 (forbidden).
http_response_code(403);
echo "Es gibt Probleme mit deiner Anfrage, bitte versuche es ernuet.";
}
?>
Your input name for subject is in English in your form, but in your php file is in German
Choosing the same lang for all your code can to avoid confussions
Change name attributes to german to make it work
<input type="text" class="form-control input-lg" name="betreff" placeholder="Betreff" >
<textarea class="form-control input-lg" rows="7" name="nachricht" placeholder="Nachricht">
</textarea>

How to make recaptcha work with verification

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>

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?

Contact form (Bootstrap, html & php)

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.

Contact form php inside html

I am trying to include a contact form with validation inside my html file with php. I would like the answer to be in the same page without redirecting to another one. I have added the php snippet before the <!DOCTYPE html> but on the top of my page it displays the error message
Also inside the contact forms i am getting tags <?php echo htmlspecialchars($_POST['name']); ?>
and such. I am new to php so i would need your help
Here's the code:
<?php
if (isset($_POST["submit"]))
{
$name = $_POST['name'];
$email = $_POST['email'];
$message = $_POST['message'];
$human = intval($_POST['human']);
$from = 'Demo Contact Form';
$to = 'example#domain.com';
$subject = 'Message from Contact Demo ';
$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 class="col-md-6 col-md-offset-3">
<form 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-10">
<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>
<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" value="<?php echo htmlspecialchars($_POST['email']); ?>">
<?php echo "<p class='text-danger'>$errEmail</p>";?>
</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"><?php echo htmlspecialchars($_POST['message']);?></textarea>
<?php echo "<p class='text-danger'>$errMessage</p>";?>
</div>
</div>
<div class="form-group">
<label for="human" class="col-sm-2 control-label">2 + 3 = ?</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="human" name="human" placeholder="Your Answer">
<?php echo "<p class='text-danger'>$errHuman</p>";?>
</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">
<?php echo $result; ?>
</div>
</div>
</form>
in the action="#" i included the # so that it stays on the same page
forgot to mention that when i complete the form it says cannot post
If you look at "View source" you will likely see the < ?php-tags, meaning PHP is not executed.
Check the extension of your file (it has to be X.php where x is the name you want) and make sure your webserver supports PHP.
If you are developing locally. Make sure you have PHP installed.
Take a look at http://www.php.net to download and install PHP for your operating system. If you are using windows, I would advise installing WAMP. You can find it here

Categories