将电话字段添加到我的表单电子邮件发送器.并输出它


Adding a telephone field to my form emailer. And outputting it

我不是PHP最伟大的,但我设法使用这个预先存在的代码来验证我的新电话字段,就像其他人一样。现在我不知道确切地把它放在哪里,以便将其添加到我的电子邮件中。我尝试了几个地方,但表单停止了工作。

<?php

header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
$admin_email = 'info@indabahairstudio.ca'; // Your Email
$message_min_length = 5; // Min Message Length

class Contact_Form{
function __construct($details, $email_admin, $message_min_length){
    $this->name = stripslashes($details['name']);
    $this->email = trim($details['email']);
    $this->telephone = stripslashes($details['telephone']);
    $this->subject = 'Appointment Request'; // Subject 
    $this->message = stripslashes($details['message']);
    $this->email_admin = $email_admin;
    $this->message_min_length = $message_min_length;
    $this->response_status = 1;
    $this->response_html = '';
}

private function validateEmail(){
    $regex = '/^(['w-]+(?:'.['w-]+)*)@((?:['w-]+'.)*'w['w-]{0,66})'.([a-z]{2,6}(?:'.[a-z]{2})?)$/i';
    if($this->email == '') { 
        return false;
    } else {
        $string = preg_replace($regex, '', $this->email);
    }
    return empty($string) ? true : false;
}

private function validateFields(){
    // Check name
    if(!$this->name)
    {
        $this->response_html .= '<p>Please enter your name</p>';
        $this->response_status = 0;
    }
    // Check email
    if(!$this->email)
    {
        $this->response_html .= '<p>Please enter an e-mail address</p>';
        $this->response_status = 0;
    }
    // Check phone number
    if(!$this->telephone)
    {
        $this->response_html .= '<p>Please enter a telephone number</p>';
        $this->response_status = 0;
    }
    // Check valid email
    if($this->email && !$this->validateEmail())
    {
        $this->response_html .= '<p>Please enter a valid e-mail address</p>';
        $this->response_status = 0;
    }
    // Check message length
    if(!$this->message || strlen($this->message) < $this->message_min_length)
    {
        $this->response_html .= '<p>Please enter your message. It should have at least '.$this->message_min_length.' characters</p>';
        $this->response_status = 0;
    }
}

private function sendEmail(){
    $mail = mail($this->email_admin, $this->subject, $this->message,
         "From: ".$this->name." <".$this->email.">'r'n"
        ."Reply-To: ".$this->email."'r'n"
    ."X-Mailer: PHP/" . phpversion());
    if($mail)
    {
        $this->response_status = 1;
        $this->response_html = '<p>Thank You!</p>';
    }
}

function sendRequest(){
    $this->validateFields();
    if($this->response_status)
    {
        $this->sendEmail();
    }
    $response = array();
    $response['status'] = $this->response_status;   
    $response['html'] = $this->response_html;
    echo json_encode($response);
}
 }

$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();
 ?>

我想通了!

我用"."连接到"$this>消息"

它放出来的时候并不漂亮,但它有效! :D