如何获取请求执行php文件的表单名称的值


How to get the value of the form name that requested execution of php file

我有一个表单,请求执行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);
  ?>

如何将表单名称添加到我的电子邮件中?如何使姓名、电话、表单名称的值位于电子邮件正文的不同行中?
(Rigt现在我收到的电子邮件是:
姓名+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/不幸的是,我仍然得到了"name"而不是"First_name"

无法获取表单的name,因为它不是作为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'];来获取名称。

<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']

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

这是我的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
名字