Home > Back-end >  HTML form is not sending data using POST?
HTML form is not sending data using POST?

Time:10-25

Below is the PHP code I deleted some of the PHP mailer code because stack overflow refused to allow me to submit the code saying the code was too long and that I needed more explanation for the problem at hand.

<?php             
    if(isset($_POST['submit'])) {
        $mail->addAddress($_POST['email']);
        // $mail->addReplyTo($_POST['email'], $_POST['name']);
        $mail->isHTML(true);    
        $mail->Subject = $_POST['subject'];
        $mail->Body = $_POST['message'];
        $mail->addAttachment($_POST['attachment']);
        try {
            $mail->send();
            echo 'Your message was sent successfully!';
        } catch (Exception $e) {
            echo "Your message could not be sent! PHPMailer Error: {$mail->ErrorInfo}";
        }
    } 
    else {
        echo "There is a problem with the contact.html document!";
    }
?>

Below is the HTML form. I have tried submitting the form data to be processed by the PHP code but it refuses to be submitted. I have posted the PHP code above and the HTML code below.

<form method="POST" role="form" enctype="multipart/form-data">
  <div class="row">
    <div class=" col-md-6 mb-3">
      <input type="email" class="form-control" name="email" id="email">
    </div>
    <div class=" col-md-6 mb-3">
      <input type="text" class="form-control" name="subject" id="subject">
    </div>
    <div class="mb-3">
      <textarea class="form-control" rows="5" name="message" id="message"></textarea>
    </div>
    <div class=" col-md-6 mb-3">
      <input type="file" class="form-control" name="attachment" id="attachment">
    </div>
  </div>
</form>
<button name="submit" type="submit" class="btn btn-primary">Send mail</button>

CodePudding user response:

You have to add submit button under form tag.

<form method="POST" role="form" enctype="multipart/form-data">
          <div class="row">
            <div class=" col-md-6 mb-3">
              <input type="email" class="form-control" name="email" id="email">
            </div>
            <div class=" col-md-6 mb-3">
              <input type="text" class="form-control" name="subject" id="subject">
            </div>
            <div class="mb-3">
              <textarea class="form-control" rows="5" name="message" id="message"></textarea>
            </div>
            <div class=" col-md-6 mb-3">
              <input type="file" class="form-control" name="attachment" id="attachment">
            </div>
          </div>
<button name="submit" type="submit" class="btn btn-primary">Send mail</button>
        </form>
        

Thanks

CodePudding user response:

Your submit button is not inside the form tag.. That is the issue Please put the submit button inside the form tag. before the closing tag

CodePudding user response:

You should keep the submit button inside form tags.

<form><button name="submit" type="submit" class="btn btn-primary">Send mail</button></form>
  • Related