PHP中的联系表单错误

时间:2013-10-26 01:12:01

标签: php html

尝试让这个联系表单起作用,不知道发生了什么。这是HTML:

   <form id="contact-form" action="contact.php">
      <input type="text" class="form-control" placeholder="Your Name" name="name" />
      <input type="text" class="form-control" placeholder="Email" name="email" />
      <input type="text" class="form-control" placeholder="Phone Number" name="phone" />
      <input type="submit" class="btn btn-jackalope btn-centered">Request Quote</input>
    </form>

这是我正在使用的contact.php:

<?php
/*
* Contact Form Class
*/


header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');

$admin_email = 'mcollinsblog@gmail.com'; // Your Email
//$message_min_length = 0; // 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 from Your 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>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 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();

?>

以下是网址:http://jackalopemedia.com/benefits.html

非常感谢任何帮助!感谢。

1 个答案:

答案 0 :(得分:2)

如果不在method标记中写入<form>属性,默认情况下会将其作为GET请求。

您需要做的是:

<form id="contact-form" action="contact.php" method="post">
  <input type="text" class="form-control" placeholder="Your Name" name="name" />
  <input type="text" class="form-control" placeholder="Email" name="email" />
  <input type="text" class="form-control" placeholder="Phone Number" name="phone" />
  <input type="submit" class="btn btn-jackalope btn-centered">Request Quote</input>
</form>

请注意我是如何加入method="post"

检查是否有效。

<强>更新

而不是:

$response = array();
$response['status'] = $this->response_status;   
$response['html'] = $this->response_html;

echo json_encode($response);

尝试:

$response = array();
$response['status'] = $this->response_status;
$response['html'] = $this->response_html;

if ($response['status'] == '0') { // If error
  echo json_encode($response);
} else {
  echo $response['html'];
}

更新:更改:

$this->name = stripslashes($details['name']);
$this->email = trim($details['email']);
$this->phone = trim($details['phone']);
$this->subject = 'Contact from Your Website'; // Subject 
$this->message = stripslashes($details['message']);

对此:

$this->name = stripslashes($details['name']);
$this->email = trim($details['email']);
$this->phone = trim($details['phone']);
$this->subject = 'Contact from Your Website'; // Subject 
$this->message = $this->name . " " . $this->email . " " . $this->phone;

应该做的伎俩。