Nazar Serdyuk Nazar Serdyuk - 6 months ago 12
PHP Question

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

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"

Answer

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.

Comments