简体   繁体   English

PHP的基本发送输入字段值

[英]php basic send input field values

I am brand new to php and have been fiddling with sending emails based off what is within a input field. 我是php的新手,并且一直在根据输入字段中的内容来发送电子邮件。 I can get the email to send, but cant get the data within the input field to show up in email. 我可以发送电子邮件,但是无法在输入字段中获取数据以显示在电子邮件中。 Any suggestions. 有什么建议么。

I tried tying the variable name to the id $comments = comments etc but i just get blank emails. 我尝试将变量名称绑定到id $ comments =注释等,但是我只是收到空白电子邮件。 Any pointers 任何指针

 <?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> 

Your textarea html has name attribute comments 您的textarea html具有名称属性注释

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

but you use to get that information by $_REQUEST['comment'] which is not there. 但是您通常使用$ _REQUEST ['comment']来获取该信息,而该信息并不存在。

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

Edit 编辑

Replace your html with below code and see is the inputed information is populating or not once you hit submit button. 将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>
 <textarea class="form-control" id="comments" name="comments" placeholder="Comment" rows="5"></textarea><br>

here name=comments so your form should be 这里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

//your email is required so users always fill that field.so if user click button //您的电子邮件是必填项,因此用户始终填写该字段。因此,如果用户单击按钮

 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);
    ?>

I think your html code should be wrap with form tag 我认为您的html代码应该用表单标签包装

Like 喜欢

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

You have to use form to get data after submit. 提交后,您必须使用表格来获取数据。

Method can be either get or post , but here you are using request and get so you have to use the get in form tag's method attribute like this. 方法可以是getpost ,但是这里您使用request和get,因此您必须使用get in form标记的method属性。

<form method="get">

change your code with following. 使用以下命令更改代码。

<?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM