HTML + PHP表单没有发送到我的电子邮件

时间:2022-10-16 13:17:41

Lots of similar questions have been asked, but couldn't find an answer to the exact problem I have. I have a form that works completely fine, but I asked someone to design it nicer (which he did) and now it just doesn't work anymore. I don't know what's causing it.

已经提出了很多类似的问题,但找不到我所遇到的确切问题的答案。我有一个完全正常的表单,但我要求某人设计得更好(他做了)现在它不再起作用了。我不知道是什么导致了它。

I'm assuming there is something wrong in my HTML code for the form, since the .php page works perfectly fine (I copy/pasted it entirely) on my previous page.

我假设我的HTML代码中的表单有问题,因为.php页面在我的上一页上完全正常(我完全复制/粘贴)。

Any idea?

<form name="htmlform" method="post" action="html_form_send.php">
<div class="row has-form">
    <div class="col-xs-12 col-sm-8 col-sm-offset-2">
            <div class="form-group">
                <label for="first_name" class="col-sm-3 control-label">Prénom *</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" id="first_name">
                    </div>
            </div>

            <div class="form-group">
                <label for="last_name" class="col-sm-3 control-label">Nom *</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" id="last_name">
                    </div>
            </div>

            <div class="form-group">
                <label for="email" class="col-sm-3 control-label">Email *</label>
                    <div class="col-sm-9">
                        <input type="email" class="form-control" id="email">
                    </div>
            </div>

            <div class="form-group">
                <label for="telephone" class="col-sm-3 control-label">Téléphone</label>
                    <div class="col-sm-9">
                        <input type="text" class="form-control" id="telephone">
                    </div>
            </div>

            <div class="form-group">
                <label for="comments" class="col-sm-3 control-label">Votre critique *</label>
                    <div class="col-sm-9">
                        <textarea class="form-control" rows="6" id="comments"></textarea>
                    </div>
            </div>

        <div class="col-xs-12 col-sm-9 col-sm-offset-3 text-center">
            <button type="submit" class="btn btn-primary btn-lg btn-block">Envoyer</button>
        </div>
    </div>
</div>
</form>

2 个解决方案

#1


your inputs have no "name" attribute add a "name" attribute with the same value as the "id" attribute to each input tag as Php uses NAME as the identifier when posted.

您的输入没有“name”属性为每个输入标记添加“id”属性与“id”属性相同的值,因为Php在发布时使用NAME作为标识符。

To Clearify:

if you try and get an input value from the POST in PHP you have to use the following code:

如果您尝试从PHP中的POST获取输入值,则必须使用以下代码:

$var = $_POST['IDENTIFIER'];

Where IDENTIFIER is the value of the "name" attribute of the INPUT or TEXTAREA or any other HTML FORM element you're trying to retrieve.

其中IDENTIFIER是INPUT或TEXTAREA的“name”属性的值或您尝试检索的任何其他HTML FORM元素。

#2


all your inputs need attribute name="first_name" and etc:

您的所有输入都需要属性name =“first_name”等:

<input type="text" class="form-control" id="first_name" name="first_name">

html_form_send.php must looks like:

html_form_send.php必须如下所示:

<?php 
   if ($_SERVER['REQUEST_METHOD'] == 'POST') {
     $first_name = $_POST['first_name'];
     $last_name = $_POST['last_name'];
     $email = $_POST['email'];
     $telephone = $_POST['telephone'];
     $comments = $_POST['comments'];

     //preparing mail data
     $from = $email;
     $to = "yourmail@exemple.com";
     $subject = "Sending Form From site";
     $message = "First name: $first_name \r\nLast name:$last_name \r\nTelephone: $telephone \r\nComments: $comments";
     $headers = 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
     //sending 
     mail($to, $subject, $message, $headers);

   }
?>

#1


your inputs have no "name" attribute add a "name" attribute with the same value as the "id" attribute to each input tag as Php uses NAME as the identifier when posted.

您的输入没有“name”属性为每个输入标记添加“id”属性与“id”属性相同的值,因为Php在发布时使用NAME作为标识符。

To Clearify:

if you try and get an input value from the POST in PHP you have to use the following code:

如果您尝试从PHP中的POST获取输入值,则必须使用以下代码:

$var = $_POST['IDENTIFIER'];

Where IDENTIFIER is the value of the "name" attribute of the INPUT or TEXTAREA or any other HTML FORM element you're trying to retrieve.

其中IDENTIFIER是INPUT或TEXTAREA的“name”属性的值或您尝试检索的任何其他HTML FORM元素。

#2


all your inputs need attribute name="first_name" and etc:

您的所有输入都需要属性name =“first_name”等:

<input type="text" class="form-control" id="first_name" name="first_name">

html_form_send.php must looks like:

html_form_send.php必须如下所示:

<?php 
   if ($_SERVER['REQUEST_METHOD'] == 'POST') {
     $first_name = $_POST['first_name'];
     $last_name = $_POST['last_name'];
     $email = $_POST['email'];
     $telephone = $_POST['telephone'];
     $comments = $_POST['comments'];

     //preparing mail data
     $from = $email;
     $to = "yourmail@exemple.com";
     $subject = "Sending Form From site";
     $message = "First name: $first_name \r\nLast name:$last_name \r\nTelephone: $telephone \r\nComments: $comments";
     $headers = 'From: Birthday Reminder <birthday@example.com>' . "\r\n";
     //sending 
     mail($to, $subject, $message, $headers);

   }
?>