HTML / PHP表单不起作用,“提交”按钮什么也不做?

时间:2019-03-15 08:31:25

标签: php html forms

所以我以前做过表格,但从未遇到任何问题,但是这次出了点问题。希望有人能尽快帮助我。这是代码(首先是HTML表单):

<form action="includes/mensaje.inc.php" method="post" class="contactForm">
      <div class="col-md-6 col-sm-6 col-xs-12 left">
        <div class="form-group">
          <input type="text" class="form-control form" name="name" placeholder="Your Name" data-msg="Introduzca al meno 4 caracteres" />
          <input type="email" class="form-control" name="email" placeholder="Your Email" data-msg="Por favor introduzca un Email válido" />
          <input type="text" class="form-control" name="subject" placeholder="Subject" data-msg="Mínimo 4 caracteres de mensaje" />
        </div>
      </div>

      <div class="col-md-6 col-sm-6 col-xs-12 right">
        <div class="form-group">
          <textarea class="form-control" name="message" rows="5" data-rule="required" data-msg="Por favor introduzca información aquí" placeholder="Message"></textarea>
          <div class="validation"></div>
        </div>
      </div>

      <div class="col-xs-12">
        <button name="submit" type="submit" class="btn btn-block btn-submit">
        ENVIAR <i class="fa fa-arrow-right"></i></button>
      </div>

</form>

这是PHP:

<?php
session_start();

if (isset($_POST['submit'])) {
    include_once 'dbh.inc.php';

    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $subject = mysqli_real_escape_string($conn, $_POST['subject']);
    $message = mysqli_real_escape_string($conn, $_POST['message']);

    $sql = "INSERT INTO mensajes 
        (name, email, subject, message) 
        VALUES ('$name', '$email', '$subject', '$message')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
?>

1 个答案:

答案 0 :(得分:0)

表单上没有$_POST['submit']名称。您需要从

更改if验证
  

if(isset($ _ POST ['submit'])){

  

如果(!empty($ _ POST)){


<?php
session_start();

if (isset($_POST['submit'])) {
    include_once 'dbh.inc.php';

    $name = mysqli_real_escape_string($conn, $_POST['name']);
    $email = mysqli_real_escape_string($conn, $_POST['email']);
    $subject = mysqli_real_escape_string($conn, $_POST['subject']);
    $message = mysqli_real_escape_string($conn, $_POST['message']);

    $sql = "INSERT INTO mensajes 
        (name, email, subject, message) 
        VALUES ('$name', '$email', '$subject', '$message')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    } else {
        echo "Error: " . $sql . "<br>" . $conn->error;
    }
?>