PHP联系表单不起作用,代码在同一个文件中

时间:2014-02-08 01:16:31

标签: php contact-form

我的联系表单有问题,我收到此消息“通知:未定义的索引:联系人名称在......”。这基本上就是我正在使用的代码。

<?php
                    //Receive all the data to process
                    $contact_name = $_POST['contact_name'];
                    $contact_company = $_POST['contact_company'];
                    $contact_country = $_POST['contact_country'];
                    $contact_phone = $_POST['contact_phone'];
                    $contact_email = $_POST['contact_email'];
                    $contact_subject = $_POST['contact_subject'];
                    $contact_message = $_POST['contact_message'];
                    $contact_headers = 'Sent by: '.$contact_name.' ('.$contact_email.');'.' Phone: '.$contact_phone.'; Country: '.$contact_country.'; Company: '.$contact_company;
                    $contact_to = 'name@email.com';

                    if($contact_name != '' || $contact_country != '' || $contact_email != '' || $contact_message != ''){  
                        mail($contact_to, $contact_subject, $contact_message, $contact_headers); //calling php mail function
                        echo 'Thank you for contacting us.<br><br><a href="contact.php">Go back.</a>';
                    }else{  
                        echo 'There was an error sending the message.<br><br><a href="index.php">Go back.</a>';  
                    }
                ?>

这是HTML

<form id="main-contact-form" action="contact.php" method="post">
                    <p>
                        <label for="contact_name" class="label-s required">NAME</label>
                        <input type="text" role="form-item" name="contact_name" id="contact_name" required>
                    </p>
                    <p>
                        <label for="contact_company" class="label-s">COMPANY</label>
                        <input type="text" role="form-item" name="contact_company" id="contact_company">
                    </p>
                    <p>
                        <label for="contact_country" class="label-s required">COUNTRY</label>
                        <input type="text" role="form-item" name="contact_country" id="contact_country" required>
                    </p>
                    <p>
                        <label for="contact_phone" class="label-s">PHONE</label>
                        <input type="text" role="form-item" name="contac_phone" id="contact_phone">
                    </p>
                    <p>
                        <label for="contact_email" class="label-s required">EMAIL</label>
                        <input type="email" role="form-item" name="contact_email" id="contact_email" required>
                    </p>
                    <p>
                        <label for="contact_subject" class="label-s">SUBJECT</label>
                        <input type="text" role="form-item" name="contact_subject" id="contact_subject">
                    </p>
                    <p>
                        <label for="contact_message" class="label-s required">MESSAGE</label>
                        <textarea role="form-item" name="contact_message" id="contact_message" class="avoid-resize-h" required></textarea>
                    </p>
                    <input id="contact-submit" type="submit" role="btn" class="btn-primary btn-l expand" value="SEND">
                </form>

当我使用这个将PHP代码放在不同的文件中并更改操作时,它可以工作,但我正在尝试使用它将php代码放在同一个文件中,当我收到错误消息时。

我希望你能帮助我。谢谢。

2 个答案:

答案 0 :(得分:2)

请务必检查$_POST数据是否已设置,否则用户第一次访问时将为空,您将看到未定义的索引错误。

将与表单相关的php代码包装为:

if( !empty($_POST) ){

答案 1 :(得分:2)

当你在同一个文件中并且它是一个空数组时,你不会检查是否设置了$_POST。添加if语句以检查$_POST

<?php
       if($_POST) {
            //Receive all the data to process
            $contact_name = $_POST['contact_name'] ? : '';
            $contact_company = $_POST['contact_company'] ? : '';
            $contact_country = $_POST['contact_country'] ? : '';
            $contact_phone = $_POST['contact_phone'] ? : '';
            $contact_email = $_POST['contact_email'] ? : '';
            $contact_subject = $_POST['contact_subject'] ? : '';
            $contact_message = $_POST['contact_message'] ? : '';
            $contact_headers = 'Sent by: '.$contact_name.' ('.$contact_email.');'.' Phone: '.$contact_phone.'; Country: '.$contact_country.'; Company: '.$contact_company;
            $contact_to = 'name@email.com';

            if($contact_name != '' || $contact_country != '' || $contact_email != '' || $contact_message != ''){  
                  mail($contact_to, $contact_subject, $contact_message, $contact_headers); //calling php mail function
                  echo 'Thank you for contacting us.<br><br><a href="contact.php">Go back.</a>';
            }else{  
                  echo 'There was an error sending the message.<br><br><a href="index.php">Go back.</a>';  
            }
      }
?>

您还应确保实际拥有值,三元运算符对此非常有用。

http://us1.php.net/manual/en/function.mail.php

邮件功能中的标题选项不正确。这应该是一个数组,请参阅手册中的示例。它应该是:

$contact_headers = 'From: webmaster@example.com';

您的$ contact_headers变量中的内容可能会附加到您的邮件中。标题是供您的服务器发送邮件的。