1

I have a form that requests execution of php file

Here is the form code:

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

Here is the php code:

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

How can I add the form name to be a part of the message I email? How can I make values of name, telephone, form name to be in differrent rows of the email body?
(Rigt now what I receive as email is this:
Name+1234567890
but I rather want it to be like this:
Name
+1234567890)
The reason I ask is because I want to create multiple html files with multiple forms that all requests execution of the same email_script.php and I want to know how each individual page performs in collecting orders. Thanks

So I edited the code and here it is:

<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 code:

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

And here is what I got as a result: http://postimg.org/image/4gj5gh22p/ Unfortunately I still got "name" instead of "First_Name"

3 Answers3

1

Getting the name of a form is not possible as it's not sent as part of the POST data.

An alternative from this post suggests adding a hidden input element inside the form with the same name as the form:

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

You can then use $_POST['First Form']; to get the name.

Community
  • 1
  • 1
The Codesee
  • 3,714
  • 5
  • 38
  • 78
  • ***"Getting the name of a form is not possible as it's not sent as part of the POST data."*** `print_r($_POST);` Output: `Array ( [First_Form] => [name] => jack [telephone] => 123456 ) ` – Pedro Lobito May 07 '16 at 20:44
  • @PedroLobito _"The name of the form is not sent to the server as part of the POST data."_ - http://stackoverflow.com/a/846025/5798798 _"No, the form's name attribute is never set to sent to the server as part of the POST data."_ - http://stackoverflow.com/a/4003336/5798798 – The Codesee May 07 '16 at 20:51
  • I think there might be some sort of communication break down here. I dont think he is looking for the name of the form element, but just the input of the element named, "name" – Steven Zurek May 07 '16 at 21:18
0
<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>

and use it using

$message .= $POST['form']
0

So finally I have the solution (My brother helped me with this).

here is my html code:

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

here is my php code:

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

It works via hidden input and the resulted email looks like this:


Nazar
+380953648591
First Name