向表单添加字段,php不再起作用

时间:2018-11-21 22:44:56

标签: php html forms email

需要一些帮助,这个问题已经存在了一段时间,但似乎无法解决,有点菜鸟:/

此代码可以用作联系表单,但我想在其中添加电话号码字段。

这就是我正在工作的:


HTML

        <form id="contact-form" class="contact-form" action="#">
            <p class="contact-name">
                <input id="contact_name" type="text" placeholder="Your name" value="" name="name" />
            </p>
            <p class="contact-email">
                <input id="contact_email" type="text" placeholder="Your e-Mail" value="" name="email" />
            </p>
            <p class="contact-message">
                <textarea id="contact_message" placeholder="Your message" name="message" rows="15" cols="40"></textarea>
            </p>
            <p class="contact-submit">
                <a id="contact-submit" class="submit" href="#">Send</a>
            </p>

            <div id="response">

            </div>
        </form>

contact.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 = 'name@email.org'; // 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->subject = 'Contact Website'; // 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>Name field is required</p>';
      $this->response_status = 0;
    }

    // Check email
    if(!$this->email)
    {
      $this->response_html .= '<p>e-Mail field is required</p>';
      $this->response_status = 0;
    }

    // Check valid email
    if($this->email && !$this->validateEmail())
    {
      $this->response_html .= '<p>e-Mail field is required</p>';
      $this->response_status = 0;
    }

    // Check message length
    if(!$this->message || strlen($this->message) < $this->message_min_length)
    {
      $this->response_html .= '<p>Message is required with a minimum of '.$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>Thanks, message is sent!</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();

?>

现在,我尝试了以下操作:


HTML

        <form id="contact-form" class="contact-form" action="#">
            <p class="contact-name">
                <input id="contact_name" type="text" placeholder="Your name" value="" name="name" />
            </p>
            <p class="contact-email">
                <input id="contact_email" type="text" placeholder="Your e-Mail" value="" name="email" />
            </p>
            <p class="contact-phone">
                <input id="contact_phone" type="text" placeholder="Uw telefoonnummer" value="" name="phone" />
            </p>
            <p class="contact-message">
                <textarea id="contact_message" placeholder="Your message" name="message" rows="15" cols="40"></textarea>
            </p>
            <p class="contact-submit">
                <a id="contact-submit" class="submit" href="#">Send</a>
            </p>

            <div id="response">

            </div>
        </form>

contact.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 = 'name@email.org'; // 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->phone = trim($details['phone']);
    $this->subject = 'Contact Website'; // Subject 
    $this->message = stripslashes($details['phone';'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>Name field is required</p>';
      $this->response_status = 0;
    }

    // Check email
    if(!$this->email)
    {
      $this->response_html .= '<p>e-Mail field is required</p>';
      $this->response_status = 0;
    }

    // Check phone
    if(!$this->phone)
    {
      $this->response_html .= '<p>Phone field is required</p>';
      $this->response_status = 0;
    }

    // Check valid email
    if($this->email && !$this->validateEmail())
    {
      $this->response_html .= '<p>e-Mail field is required</p>';
      $this->response_status = 0;
    }

    // Check message length
    if(!$this->message || strlen($this->message) < $this->message_min_length)
    {
      $this->response_html .= '<p>Message is required with a minimum of '.$this->message_min_length.' characters</p>';
      $this->response_status = 0;
    }
  }


  private function sendEmail(){
    $mail = mail($this->email_admin, $this->subject, $this->message, $this->phone,
       "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>Thanks, message is sent!</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->message = stripslashes($details['phone','message']);

但没有运气

此:

$this->message = stripslashes($details['phone,message']);

也没有运气

1 个答案:

答案 0 :(得分:-2)

PHP修复

尝试

$this->message = stripslashes($details['phone'] . $details['message']);

也许更好:

$this->message = stripslashes($details['message']. ' - Phone: ' . $details['phone']);

您可以阅读arraysstrings concatenation来了解其工作原理。

HTML修复

您的表单未链接到任何地方的contact.php。将您的表单标签更改为

<form id="contact-form" class="contact-form" action="contact.php" method="post">