これが私が取り組んでいるフォームです。それは私が作ったものではなく、ブートストラップで見つけて移動しました。それは機能しますが、理想的には、訪問者がそれを送信すると、訪問者を同じページに保持し、ブートストラップからの JavaScript ポップオーバーを表示して、「ありがとうございます。あなたの情報を受け取りました。ご連絡いたします!」と言います。
ただし、Javascript を表示しようとすると (今のところ基本的なアラートを使用しているだけです)、実行されているようには見えません...代わりに、新しいウィンドウ contact.php にコードを書き込むだけです。
ここに私が取り組んでいるページがあります:
http://www.jackalopemedia.com/benefits
お問い合わせフォーム:
<?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 = $this->name . " " . $this->email . " " . 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>';
echo '<script type="text/javascript">'
, 'alert(thank you);'
, '</script>';
}
}
function sendRequest(){
$this->validateFields();
if($this->response_status)
{
$this->sendEmail();
}
$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'];
}
}
}
$contact_form = new Contact_Form($_POST, $admin_email, $message_min_length);
$contact_form->sendRequest();
?>
返信ありがとうございます。