0

So I have this HTML code for my contact form:

<section id="contact-form">
            <form action="form.php" method="post" enctype="multipart/form-data">
          <div class="container">
            <div class="row">
              <div class="col-md-6 col-sm-12">
                <div class="block">
                  <form>
                    <div class="form-group">
                      <input type="text" class="form-control" placeholder="Ime" name="name">
                    </div>
                    <div class="form-group">
                      <input type="text" class="form-control" placeholder="Email Adresa" name="email">
                    </div>
                    <div class="form-group">
                      <input type="text" class="form-control" name="subject" placeholder="Predmet">
                    </div>
                  </form>
                </div>
              </div>
              <div class="col-md-6 col-sm-12">
                <div class="block">
                  <form>
                    <div class="form-group-2">
                      <textarea class="form-control" rows="3" placeholder="Poruka" name="message"></textarea>
                    </div>
                    <button class="btn btn-default" type="submit">Pošalji</button>
                  </form>
                </div>
              </div>
            </div>
            </div>
                </form>
              </section>

And my PHP script looks like this:

<?php
    $name = $_POST['name'];
    $email = $_POST['email'];
    $message = $_POST['message'];
    $from = 'From: Kontakt s web-stranice'; 
    $to = 'myEmailAddress'; 
    $subject = 'Kontakt s web-stranice ';

    $body = "From: $name\n E-Mail: $email\n Message:\n $message";
?>

<?php
if ($_POST['submit']) {
    if (mail ($to, $subject, $body, $from)) { 
        echo '<p>Thank you for your email!</p>';
    } else { 
        echo '<p>Oops! An error occurred. Try sending your message again.</p>'; 
    }
}
?>

And for some reason when I click on the submit button the web page refreshes and the message isn't sent to my email.

Does anyone know why?

DaxHR
  • 683
  • 1
  • 11
  • 30

1 Answers1

0

Your HTML format is using forms within forms, so everything outside that <form> tag is not being collected. All you need to do is have the first <form> tag and wrap all your input boxes and the submit button in it.

Notice in the code below how you only need one <form>.

<section id="contact-form">
<form action="form.php" method="post" enctype="multipart/form-data">
    <div class="container">
        <div class="row">
            <div class="col-md-6 col-sm-12">
                <div class="block">

                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Ime" name="name">
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" placeholder="Email Adresa" name="email">
                    </div>
                    <div class="form-group">
                        <input type="text" class="form-control" name="subject" placeholder="Predmet">
                    </div>
                </div>
            </div>
            <div class="col-md-6 col-sm-12">
                <div class="block">
                    <div class="form-group-2">
                        <textarea class="form-control" rows="3" placeholder="Poruka" name="message"></textarea>
                    </div>
                    <button class="btn btn-default" type="submit">Pošalji</button>
                </div>
            </div>
        </div>
    </div>
</form>
</section>

Your PHP code is correct from the looks of things, but that messed up form tag was the big problem.

MinistryOfChaps
  • 1,458
  • 18
  • 31