I was coding contact form for my webpage but something doesnt work and I can't find what's wrong. When i press submit button its just reloads webpage. If someone wouldnt mind to check my code I would appreciate it.
<?php
if(isset($_POST['submit'])){
$to = "[email protected]"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
mail($to, $subject, $message, $headers);
// You can also use header('Location: thank_you.php'); to redirect to another page.
}
?>
<section id="contact" class="parallax-section">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-md-offset-2 col-md-8 col-sm-offset-1 col-sm-10">
<div class="wow fadeInUp section-title" data-wow-delay="0.3s">
<h2>Susisiekite su mumis</h2>
<h4>mes visada pasiruoše atsakyti į jums rūpimus klausimus</h4>
</div>
<div class="contact-form wow fadeInUp" data-wow-delay="0.7s">
<form id="contact-form" method="POST" action="#">
<input name="name" type="text" class="form-control" placeholder="Vardas, Pavardė" required>
<input name="email" type="email" class="form-control" placeholder="Elektroninis paštas" required>
<textarea name="message" class="form-control" placeholder="Jūsų žinutė" rows="5" cols="30" required></textarea>
<input type="submit" class="form-control submit" name="submit" value="SIŲSTI">
</form>
</div>
</div>
</div>
</div>
</section>
Your question is a bit unclear. However, the form will be displayed again because your code is set up to always display the form. If you'd rather display a message saying that the email was sent, you should put an echo statement inside of your if block. If you do not want to display the form you can put the form inside of an else block. Something like this should work:
<?php
if(isset($_POST['submit'])){
$to = "[email protected]"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$name = $_POST['name'];
$subject = "Form submission";
$subject2 = "Copy of your form submission";
$message = $name . " wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "Here is a copy of your message " . $name . "\n\n" . $_POST['message'];
$headers = "From:" . $from;
$headers2 = "From:" . $to;
$success = mail($to, $subject, $message, $headers);
if ($success) {
echo "Your message has been sent.";
} else {
echo "An error was encountered.";
}
}
else { //begin else section
?>
<section id="contact" class="parallax-section">
<div class="overlay"></div>
<div class="container">
<div class="row">
<div class="col-md-offset-2 col-md-8 col-sm-offset-1 col-sm-10">
<div class="wow fadeInUp section-title" data-wow-delay="0.3s">
<h2>Susisiekite su mumis</h2>
<h4>mes visada pasiruoše atsakyti į jums rūpimus klausimus</h4>
</div>
<div class="contact-form wow fadeInUp" data-wow-delay="0.7s">
<form id="contact-form" method="POST">
<input name="name" type="text" class="form-control" placeholder="Vardas, Pavardė" required>
<input name="email" type="email" class="form-control" placeholder="Elektroninis paštas" required>
<textarea name="message" class="form-control" placeholder="Jūsų žinutė" rows="5" cols="30" required></textarea>
<input type="submit" class="form-control submit" name="submit" value="SIŲSTI">
</form>
</div>
</div>
</div>
</div>
</section>
<?php
} //closes else section