php基本发送输入字段值

时间:2017-06-20 05:38:42

标签: php

我是php的新手,并且一直在摆弄根据输入字段内的内容发送电子邮件。我可以收到要发送的电子邮件,但无法获取输入字段中的数据以显示在电子邮件中。任何建议。

我尝试将变量名称绑定到id $ comments = comments等 但我只是收到空白的电子邮件。任何指针

<?php
//if "email" variable is filled out, send email
  if (isset($_REQUEST['email']))  {
  
  //Email information
  $admin_email = "info@email.com";
  $email = $_REQUEST['email'];
  $name = $_REQUEST['name'];
  $comment = $_REQUEST['comment'];

  
 $name = $_GET['name'];
  //send email
  mail($admin_email, "$name", $comment, "From:" . $email);
  
  //Email response
  echo "Thank you for contacting us!";
  }
mail("info@email.com","$name",$comment);
?>
<div id="contact" class="container-fluid bg-grey">
  <h2 class="text-center">CONTACT</h2>
  <div class="row">
    <div class="col-sm-5">
      <p>Contact me and I'll get back to you within 24 hours.</p>
      <p><span class="glyphicon glyphicon-map-marker"></span> </p>
      <p><span class="glyphicon glyphicon-phone"></span> </p>
      <p><span class="glyphicon glyphicon-envelope"></span> </p>
    </div>
    <div class="col-sm-7 slideanim">
      <div class="row">
        <div class="col-sm-6 form-group">
          <input class="form-control" id="name" name="name" placeholder="Name" type="text" required>
        </div>
        <div class="col-sm-6 form-group">
          <input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
        </div>
      </div>
      <textarea class="form-control" id="comments" name="comments" placeholder="Comment" rows="5"></textarea><br>
      <div class="row">
        <div class="col-sm-12 form-group">
          <button class="btn btn-default pull-right" value="Run Script" id="submit" type="submit">Send</button>
        </div>
      </div>
    </div>
    </div>
  </div>

4 个答案:

答案 0 :(得分:2)

您的textarea html具有名称属性评论

 <textarea class="form-control" id="comments" name="comments" placeholder="Comment" rows="5"></textarea><br>

但你用 $ _ REQUEST [&#39;评论&#39;] 来获取该信息,而不是那里。

replace $_REQUEST['comment'] with $_REQUEST['comments']

修改

用下面的代码替换你的html,看到输入提交按钮后输入的信息是否填充。

<?php
 print_r($_REQUEST);
?>
<div id="contact" class="container-fluid bg-grey">
  <h2 class="text-center">CONTACT</h2>
  <div class="row">
    <div class="col-sm-5">
      <p>Contact me and I'll get back to you within 24 hours.</p>
      <p><span class="glyphicon glyphicon-map-marker"></span> </p>
      <p><span class="glyphicon glyphicon-phone"></span> </p>
      <p><span class="glyphicon glyphicon-envelope"></span> </p>
    </div>
    <form  action="" method="get">
    <div class="col-sm-7 slideanim">
      <div class="row">
        <div class="col-sm-6 form-group">
          <input class="form-control" id="name" name="name" placeholder="Name" type="text" required>
        </div>
        <div class="col-sm-6 form-group">
          <input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
        </div>
      </div>
      <textarea class="form-control" id="comments" name="comments" placeholder="Comment" rows="5"></textarea><br>
      <div class="row">
        <div class="col-sm-12 form-group">
          <button class="btn btn-default pull-right" value="Run Script" id="submit" type="submit">Send</button>
        </div>
      </div>
    </div>
    </div>
  </div>

答案 1 :(得分:0)

 <textarea class="form-control" id="comments" name="comments" placeholder="Comment" rows="5"></textarea><br>

这里是name = comments 所以你的表格应该是

<form action="" method="post" id="form"/>
    <div id="contact" class="container-fluid bg-grey">
      <h2 class="text-center">CONTACT</h2>
      <div class="row">
        <div class="col-sm-5">
          <p>Contact me and I'll get back to you within 24 hours.</p>
          <p><span class="glyphicon glyphicon-map-marker"></span> </p>
          <p><span class="glyphicon glyphicon-phone"></span> </p>
          <p><span class="glyphicon glyphicon-envelope"></span> </p>
        </div>
        <div class="col-sm-7 slideanim">
          <div class="row">
            <div class="col-sm-6 form-group">
              <input class="form-control" id="name" name="name" placeholder="Name" type="text" required>
            </div>
            <div class="col-sm-6 form-group">
              <input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
            </div>
          </div>
          <textarea class="form-control" id="comments" name="comments" placeholder="Comment" rows="5"></textarea><br>
          <div class="row">
            <div class="col-sm-12 form-group">
              <button class="btn btn-default pull-right" value="Run Script" id="submit" name="btn_submit" type="submit">Send</button>
            </div>
          </div>
        </div>
        </div>
      </div>
</form>


 <?php

//您的电子邮件是必需的,因此用户始终填写该字段。如果用户单击按钮

 if (isset($_POST['btn_submit']))  {

      //Email information
      $admin_email = "info@email.com";
      $email = $_POST['email'];
      $name = $_POST['name'];


        $comment = $_POST['comments'];//replace here comments


     $name =$_POST['name'];
      //send email
      mail($admin_email, $name, $comment, "From:" . $email);

      //Email response
      echo "Thank you for contacting us!";
      }
    mail("info@email.com",$name,$comment);
    ?>

答案 2 :(得分:0)

我认为您的HTML代码应该使用表单标记

进行换行

喜欢

<form name="form_name" >
   // HTML code goes here
</form>

答案 3 :(得分:0)

您必须在提交后使用表单获取数据。

方法可以是getpost,但是在这里您使用的是请求,因此您必须使用get in form标记的方法属性。

<form method="get">

使用以下内容更改您的代码。

<?php
//if "email" variable is filled out, send email
  if (isset($_REQUEST['email']))  {

  //Email information
  $admin_email = "info@email.com";
  $email = $_REQUEST['email'];
  $name = $_REQUEST['name'];
  $comment = $_REQUEST['comment'];


 $name = $_REQUEST['name'];
  //send email
  mail($admin_email, "$name", $comment, "From:" . $email);

  //Email response
  echo "Thank you for contacting us!";
  }
mail("info@email.com","$name",$comment);
?>


<div id="contact" class="container-fluid bg-grey">
  <h2 class="text-center">CONTACT</h2>
  <div class="row">
    <div class="col-sm-5">
      <p>Contact me and I'll get back to you within 24 hours.</p>
      <p><span class="glyphicon glyphicon-map-marker"></span> </p>
      <p><span class="glyphicon glyphicon-phone"></span> </p>
      <p><span class="glyphicon glyphicon-envelope"></span> </p>
    </div>
    <form method="get">
    <div class="col-sm-7 slideanim">
      <div class="row">
        <div class="col-sm-6 form-group">
          <input class="form-control" id="name" name="name" placeholder="Name" type="text" required>
        </div>
        <div class="col-sm-6 form-group">
          <input class="form-control" id="email" name="email" placeholder="Email" type="email" required>
        </div>
      </div>
      <textarea class="form-control" id="comments" name="comments" placeholder="Comment" rows="5"></textarea><br>
      <div class="row">
        <div class="col-sm-12 form-group">
          <button class="btn btn-default pull-right" value="Run Script" id="submit" type="submit">Send</button>
        </div>
      </div>
    </div>
    </div>
   </form>
  </div>