-1

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 = "myEmail@gmail.com"; // 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>
  • 1
    Is your webserver configured to use `mail()`? Afaik this does not work out of the box. – Sirko Apr 20 '17 at 06:48
  • Unclear question, can you show this is fiddle? – Sushan Apr 20 '17 at 06:48
  • not really clear though... please use `error_reporting(E_ALL); ini_set('display_errors', 1);` on top of your page and let us know if PHP returns any error – OldPadawan Apr 20 '17 at 06:49
  • did you check your spam folder and to ensure that the post is running the PHP code write echo ""; and check your console – Usman Shahid Apr 20 '17 at 06:51
  • Code looks ok (seems like an example from a tutorial). Maybe you just have to change the $to email with yours so that you can see if it works. – alpadev Apr 20 '17 at 06:51
  • I don't think OP is complaining about email not being sent. In the php script, there are no echoes or headers. The page will reload. – Rotimi Apr 20 '17 at 06:58
  • I was using wrong email... this code works. still thanks for answers – Lukas Abromas Apr 20 '17 at 08:40

2 Answers2

0

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 = "myEmail@gmail.com"; // 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
kojow7
  • 10,308
  • 17
  • 80
  • 135
0

You could keep the form action attribute as blank:

<form id="contact-form" method="POST" action="">

And debug your PHP code:

<?php 
    if (isset($_POST['submit'])) {
        $to = "myEmail@gmail.com"; // this is your Email address
        $headers = "From:" . $from;
        $headers2 = "From:" . $to;
        $send_email = mail($to, $subject, $message, $headers);
        echo "Debugging mail send code.... <br/>"; // Add this line
        var_dump($send_email); // Add this line. Check if it's true or false
    }
?>
Indrasis Datta
  • 8,692
  • 2
  • 14
  • 32