在表单中添加一个字段,php不再工作



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

这个代码可以很好地作为联系人表单,但我想添加一个电话号码字段到它

这就是我所做的:


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.">rn"
."Reply-To: ".$this->email."rn"
."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.">rn"
."Reply-To: ".$this->email."rn"
."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']);

也没有运气

PHP修复程序

尝试

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

或者可能更好:

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

您可以阅读数组和字符串的串联,了解它是如何工作的。

HTML修复程序

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

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

最新更新