php错误从文本区域获取值

时间:2018-01-04 03:35:09

标签: php html html5 textarea

嘿伙计我在使用这种形式从我的textarea中捕获值时遇到问题,我可以读取名称和电子邮件的第一个值,但是当php代码尝试读取textarea的消息时,它会返回错误。有人可以帮我弄清楚我的代码有什么问题吗?顺便说一句,我仍然在这里写作,因为我需要填补这个空间,但这句话与这个问题并不真实相关。谢谢!

HTML:

<form action="form_process.php" method="post">
      <div class="row">
        <div class="col-lg-6 text-center col-md-8 ml-auto mr-auto">
          <div class="input-group input-lg">
            <span class="input-group-addon">
              <i class="now-ui-icons users_circle-08"></i>
            </span>
            <input type="text" class="form-control" placeholder="First Name..." name="Name">
          </div>
          <div class="input-group input-lg">
            <span class="input-group-addon">
              <i class="now-ui-icons ui-1_email-85"></i>
            </span>
            <input type="text" class="form-control" placeholder="Email..." name="Email">
          </div>
          <form class="" action="form_process.php" method="post">
            <div class="textarea-container">
              <p>
                <textarea class="form-control" name="name" rows="4" cols="80" placeholder="Type a message..." name="MessageForMe"></textarea>
              </p>
            </div>
          </form>

          <div class="send-button">
            <input class="btn btn-primary btn-round btn-block btn-lg" type="submit" value="Submit" />
          </div>
        </div>
      </div>
    </form>

PHP:

<!DOCTYPE html>
<html>
<body>

<?php

$con = mysqli_connect("localhost","benji3pr","Benji3pr","Contact Form");

//if we dont connect
if(mysqli_connect_errno())
{
    echo "Failed to connect" . mysqli_connect_error();
}

//if we connect
if(mysqli_ping($con))
{
    echo "Connection Ok!!!";
}
else
{
    echo "Error: " . mysqli_error($con);
}

$name = $_POST['Name'];

$email = $_POST['Email'];

$message = $_POST['MessageForMe'];

echo (' ' . $name . ' ' . $email . ' ' . $message);

$sql = "INSERT INTO Contact Form (Name, Email, Message) VALUES ('$name', 
'$email', '$message')";

if (!mysqli_query($sql)){
  die('Error: ' . mysqli_error());
}



mysqli_close($con);

?>

</body>
</html>

3 个答案:

答案 0 :(得分:1)

<form action="form_process.php" method="post">
    <div class="row">
        <div class="col-lg-6 text-center col-md-8 ml-auto mr-auto">
            <div class="input-group input-lg">
                <span class="input-group-addon">
                    <i class="now-ui-icons users_circle-08"></i>
                </span>
                <input type="text" class="form-control" placeholder="First Name..." name="Name">
            </div>
            <div class="input-group input-lg">
                <span class="input-group-addon">
                    <i class="now-ui-icons ui-1_email-85"></i>
                </span>
                <input type="text" class="form-control" placeholder="Email..." name="Email">
            </div>
            <div class="textarea-container">
                <p>
                    <textarea class="form-control" rows="4" cols="80" placeholder="Type a message..." name="MessageForMe"></textarea>
                </p>
            </div>
            <div class="send-button">
                <input class="btn btn-primary btn-round btn-block btn-lg" type="submit" value="Submit" />
            </div>
        </div>
    </div>
</form>

您指定了name属性两次。您在同一个textarea标记中有name="name"name="MessageForMe"。这可能是您的代码无效的原因。另外,我注意到你用嵌套的<form>包裹了你,并使用相同的动作和方法,我认为这是不必要的。

答案 1 :(得分:0)

从textarea中删除name="name"属性:

<textarea class="form-control" name="name" rows="4" cols="80" placeholder="Type a message..." name="MessageForMe"></textarea>

到:

<textarea class="form-control" rows="4" cols="80" placeholder="Type a message..." name="MessageForMe"></textarea>

答案 2 :(得分:0)

您在textArea字段中使用name属性twice。删除name="name"并将其更改为以下内容。它现在应该工作。

<textarea class="form-control" rows="4" cols="80" placeholder="Type a message..." name="MessageForMe"></textarea>