将电话字段添加到联系表单

时间:2017-03-03 11:18:45

标签: php contact contact-form

我几乎没有关于php的任何知识。因此,我正在尝试通过添加电话字段来编辑我在我的网站中的联系表单。问题是,一旦我这样做,它就会停止工作而我收不到收件箱中的邮件。我可能做错了什么,但我不知道它是什么。

这是联系表格的完整代码:

--RequestVerificationToken

这是我刚刚添加的内容:

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />

<?php

    if(isset($_POST['email'])) {

        // EDIT THE 2 LINES BELOW AS REQUIRED
        $email_to = "myemail@gmail.com";
        $email_subject = "Contact Form AA";

        function died($error) {
            // your error code can go here
            echo "We are sorry, an error has occurred.";
            echo "The error is the following: <br /><br />";
            echo $error."<br /><br />";
            echo "Please fix the error.<br /><br />";
            die();
        }
        // validation expected data exists
        if(!isset($_POST['first_name']) ||
            !isset($_POST['email']) ||
            !isset($_POST['phone']) ||
            !isset($_POST['comments'])) {
            died('We are sorry, an error has occurred.');       
        }

        $first_name = $_POST['first_name']; // required
        $email_from = $_POST['email']; // required
        $phone = $_POST['phone']; // required
        $comments = $_POST['comments']; // required
        $error_message = "";
        $email_exp = '/^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/';
      if(!preg_match($email_exp,$email_from)) {
        $error_message .= 'The email is not a valid one.<br />';
      }
      if(strlen($first_name) < 2) {
        $error_message .= 'The name is not a valid one.<br />';
      }
      if(strlen($comments) < 2) {
        $error_message .= 'The message is not a valid one.<br />';
      }
      if(strlen($error_message) > 0) {
        died($error_message);
      }

      $email_message = "Contact details:\n\n";
        function clean_string($string) {
          $bad = array("content-type","bcc:","to:","cc:","href");
          return str_replace($bad,"",$string);
        }

        $email_message .= "Name: ".clean_string($first_name)."\n";
        $email_message .= "Email: ".clean_string($email_from)."\n";
        $email_message .= "Phone: ".clean_string($phone)."\n";
        $email_message .= "Menssage: ".clean_string($comments)."\n";

    // create email headers
    $headers = 'From: '.$email_from."\r\n".
    'Reply-To: '.$email_from."\r\n" .
    'X-Mailer: PHP/' . phpversion();
    @mail($email_to, $email_subject, $email_message, $phone, $headers);  
    header("Location: thankyou.html"); 
    ?>

    <?php
    }
    ?>

最后一句话!isset($_POST['phone']) || $phone = $_POST['phone']; // required $email_message .= "Phone: ".clean_string($phone)."\n";

有人能看出问题所在吗?这是我正在使用的简单形式。和以前一样,我添加的只是手机部分。

$phone

感谢您的帮助!

3 个答案:

答案 0 :(得分:1)

像比拉尔说的那样

getLastKnownLocation()

不正确,但您想要使用tel

类型
FusedLocationAPI

编辑:

您的邮件语法也错误

从邮件功能中删除 <input type="phone" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" /> </div> ,因为语法错误+您已将$ phone放入邮件中

所以你的语法应该是这样的:

 <input type="tel" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
            </div>

答案 1 :(得分:0)

<input type="phone" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
        </div>

将此行更改为

<input type="text" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />
        </div>

答案 2 :(得分:0)

您可以将手机字段更改为type = number或type = text。

<input type="text" placeholder="Phone" name="phone" id="phone" required data-validation-required-message="Please enter your phone number" />

注意:Internet Explorer 9及早期版本不支持type =“number”的元素。

我认为你不需要邮件功能的电话。

@mail($email_to, $email_subject, $email_message, $phone, $headers);

将此更改为。

@mail($email_to, $email_subject, $email_message, $headers);

这有望解决问题。