【问题标题】:How to get the value of the form name that requested execution of php file如何获取请求执行php文件的表单名称的值
【发布时间】:2016-09-02 18:22:14
【问题描述】:

我有一个请求执行 php 文件的表单

表格代码如下:

  <form name="First Form" method="post" action="email_script.php">
  <input type="text" name="name" placeholder="Ваше Имя">
  <input type="text" name="telephone" placeholder="Ваш Телефон">
  <input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
  </form>

这里是php代码:

  <?php
  // the message
  $message .= $_POST["name"];
  $message .= $_POST["telephone"];

  // use wordwrap() if lines are longer than 70 characters
  $message = wordwrap($message,70);

  // send email
  mail("nsaann@gmail.com","Заказ",$message);
  ?>

如何将表单名称添加到我通过电子邮件发送的消息中? 如何使姓名、电话、表单名称的值位于电子邮件正文的不同行中?
(现在我收到的电子邮件是这样的:
姓名+1234567890
但我更希望它是这样的:
名称
+1234567890)
我问的原因是因为我想创建具有多个表单的多个 html 文件,所有这些文件都请求执行相同的 email_script.php,并且我想知道每个单独的页面在收集订单时的执行情况。谢谢

所以我编辑了代码,这里是:

<form name="First_Name" method="post" action="email_script.php">
<input type="hidden" name="First Name">
<input type="text" name="name" placeholder="Ваше Имя">
<input type="text" name="telephone" placeholder="Ваш Телефон">
<input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
</form>

php代码:

<?php
if(isset($_POST["name"]) and isset($_POST["telephone"])){
    $message = $_POST["name"]."<br>"; //use <br> and set the email headers to: Content-type: text/html
    $message .= $_POST["telephone"]."<br>";
    $message .= key($_POST); //form name
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    mail("nsaann@gmail.com", "Заказ", $message, $headers);
}
?>

这是我得到的结果:http://postimg.org/image/4gj5gh22p/ 不幸的是,我仍然得到“名字”而不是“名字”

【问题讨论】:

  • 要获取表单名称,请使用$formName = key($_POST);。您能否澄清一下:“我怎样才能使姓名、电话、表格名称的值出现在电子邮件正文的不同行中?”您能举个例子说明什么吗?你需要,
  • 请阅读澄清帖子
  • 你所拥有的和你需要的都是一样的。
  • 请阅读澄清后添加的html
  • 我添加了行 $formName = key($_POST);和 $message .= $formName;我收到的结果电子邮件是这样的:Nazar+380953648591name 虽然它应该是 Nazar+380953648591First Name

标签: php html contact-form


【解决方案1】:

获取表单的name 是不可能的,因为它不是作为 POST 数据的一部分发送的。

this post 的替代方案建议在表单内添加一个与表单同名的隐藏输入元素:

<form name="First Form" method="post" action="email_script.php">
 <input type="hidden" name="First Form">
 <input type="text" name="name" placeholder="Ваше Имя">
 <input type="text" name="telephone" placeholder="Ваш Телефон">
 <input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
</form>

然后您可以使用$_POST['First Form']; 来获取名称。

【讨论】:

  • “获取表单名称是不可能的,因为它不是作为 POST 数据的一部分发送的。” print_r($_POST); 输出:@ 987654328@
  • @PedroLobito "表单的名称不会作为 POST 数据的一部分发送到服务器。" - stackoverflow.com/a/846025/5798798 "不,表单的名称属性永远不会设置为作为 POST 数据的一部分发送到服务器。" - stackoverflow.com/a/4003336/5798798
  • 我认为这里可能存在某种沟通中断。我不认为他在寻找表单元素的名称,而只是在名为“name”的元素的输入
【解决方案2】:
<form name="First Form" method="post" action="emailscript.php">
 <input type="hidden" name="form" value="First Form">
 <input type="text" name="name" placeholder="Ваше Имя">
 <input type="text" name="telephone" placeholder="Ваш Телефон">
 <input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
</form>

并使用它使用

$message .= $POST['form']

【讨论】:

    【解决方案3】:

    所以我终于有了解决方案(我的兄弟帮我解决了这个问题)。

    这是我的 html 代码:

    <form name="First_Name" method="post" action="email_script.php">
    <input type="hidden" name="form_name" value="First Name">
    <input type="text" name="name" placeholder="Ваше Имя">
    <input type="text" name="telephone" placeholder="Ваш Телефон">
    <input type="submit" value="ОСТАВИТЬ ЗАЯВКУ">
    </form>
    

    这是我的 php 代码:

    <?php
    if(isset($_POST["name"]) and isset($_POST["telephone"])){
    $message = $_POST["name"]."<br>"; //use <br> and set the email headers to: Content-type: text/html
    $message .= $_POST["telephone"]."<br>";
    $message .= $_POST["form_name"]."<br>";
    //$message .= key($_POST); //form name
    $headers  = 'MIME-Version: 1.0' . "\r\n";
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
    mail("nsaann@gmail.com", "Заказ Парня", $message, $headers);
    }
    ?>
    

    它通过隐藏输入工作,生成的电子邮件如下所示:


    纳扎尔
    +380953648591
    名字

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      • 2016-08-27
      • 2021-01-11
      • 2013-10-21
      • 1970-01-01
      • 1970-01-01
      • 2019-07-02
      相关资源
      最近更新 更多