我一直在联系表单上工作,而不是2个单独的文件我这样做。它现在工作得更好但是当你填写详细信息:“姓名,电话,电子邮件,消息”时,它会说你需要填写必填字段“即使它们都已填写......
代码:
<?php
require_once 'contact/core/init.php'; //adding init file to index
$user = new User(); //Calling class file to be used
?>
<!DOCTYPE HTML>
<html lang="en" class="no-js">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link href='http://fonts.googleapis.com/css?family=Open+Sans:400,300,700' rel='stylesheet' type='text/css'>
<link rel="stylesheet" href="contact/css/reset.css"> <!-- CSS reset -->
<link rel="stylesheet" href="contact/css/style.css"> <!-- Resource style -->
<script src="contact/js/modernizr.js"></script> <!-- Modernizr -->
<link href='http://fonts.googleapis.com/css?family=Neucha' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Antic' rel='stylesheet' type='text/css'>
<link href='http://fonts.googleapis.com/css?family=Lato:300' rel='stylesheet' type='text/css'>
<link rel="stylesheet type="text/css" href="style.css">
<link rel="apple-touch-icon" sizes="57x57" href="Favicon/apple-icon-57x57.png">
<link rel="apple-touch-icon" sizes="60x60" href="Favicon/apple-icon-60x60.png">
<link rel="apple-touch-icon" sizes="72x72" href="Favicon/apple-icon-72x72.png">
<link rel="apple-touch-icon" sizes="76x76" href="Favicon/apple-icon-76x76.png">
<link rel="apple-touch-icon" sizes="114x114" href="Favicon/apple-icon-114x114.png">
<link rel="apple-touch-icon" sizes="120x120" href="Favicon/apple-icon-120x120.png">
<link rel="apple-touch-icon" sizes="144x144" href="Favicon/apple-icon-144x144.png">
<link rel="apple-touch-icon" sizes="152x152" href="Favicon/apple-icon-152x152.png">
<link rel="apple-touch-icon" sizes="180x180" href="Favicon/apple-icon-180x180.png">
<link rel="icon" type="image/png" sizes="192x192" href="Favicon/android-icon-192x192.png">
<link rel="icon" type="image/png" sizes="32x32" href="Favicon/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="96x96" href="Favicon/favicon-96x96.png">
<link rel="icon" type="image/png" sizes="16x16" href="Favicon/favicon-16x16.png">
<link rel="manifest" href="Favicon/manifest.json">
<meta name="msapplication-TileColor" content="#ffffff">
<meta name="msapplication-TileImage" content="/ms-icon-144x144.png">
<meta name="theme-color" content="#ffffff">
<title>*******</title>
</head>
<img src="Images/Banner.jpg" />
<body>
<?php
if(Input::exists()) { //Checking if there are inputs
$validate = new Validate(); //calling Validation class
$validation = $validate->check($_POST, array(
'Name' => array( //using name in the input section you can validate your inputs here
'required' => true, //something has to be added into the name section
'min' => 2, //nothing less then 2
'max' => 255 //nothing over 255
),
'Phone' => array(
'required' => true,
'min' => 2,
'max' => 255
),
'Email' => array(
'required' => true
),
'Message' => array(
'required' => true
)
));
if($validation->passed()) { //if validation has passed
$user = new User(); //calling class file within the validation if statment
try {
if(isset($_POST['Features'])){
$user->create(array( //adding items to a database.
'Name' => Input::get('Name'), //getting information form the form to ass to database
'Phone' => Input::get('Phone'),
'Email' => Input::get('Email'), // Email is the database table name
'Budget' => Input::get('Budget'),
'Message' => Input::get('Message'),
'Features' => implode(', ', $_POST['Features']), //Imploding Features to display more then one if more then one is selected and spacing them with a comma ',' to look neat
'Message' => Input::get('Message'),
'Sent' => date('Y-m-d H:i:s'),
'IP_Address' => $user->_ip() //Getting users IP address for security reasons
));
}else{
$user->create(array( //adding items to a database.
'Name' => Input::get('Name'), //getting information form the form to ass to database
'Phone' => Input::get('Phone'),
'Email' => Input::get('Email'), // Email is the database table name
'Budget' => Input::get('Budget'),
'Project_Type' => Input::get('Project_Type'),
'Message' => Input::get('Message'),
'Sent' => date('Y-m-d H:i:s'),
'IP_Address' => $user->_ip() //Getting users IP address for security reasons
));
}
//Send email to email address
$adminEmail = '******'; //Your own email address
$subject = "Email from your Contact Form";
// Set Message
if(isset($_POST['Features'])){
$message = "Email from: " . Input::get('Name') . "<br />";
$messgae .= "Phone: " . Input::get('Phone') . "<br />";
$message .= "Email address: " . Input::get('Email') . "<br />";
$messgae .= "Budget: " . Input::get('Budget') . "<br />";
$message .= "Project Type: " . Input::get('Project_Type') . "<br />";
$message .= "Features: " . implode(', ', $_POST['Features']) . "<br />";
$message .= "Message: <br />";
$message .= Input::get('Message');
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
$message .= "Email sent: " . date('Y-m-d H:i:s') . "<br />";
$message .= Input::get('Name') . " IP Addrress: " . $user->_ip() . "<br />";
}else{
$message = "Email from: " . Input::get('Name') . "<br />";
$messgae .= "Phone: " . Input::get('Phone') . "<br />";
$message .= "Email address: " . Input::get('Email') . "<br />";
$messgae .= "Budget: " . Input::get('Budget') . "<br />";
$message .= "Project Type: " . Input::get('Project_Type') . "<br />";
$message .= "Message: <br />";
$message .= Input::get('Message');
$message .= "<br /> ----- <br /> This email was sent from your site's contact form. <br />";
$message .= "Email sent: " . date('Y-m-d H:i:s') . "<br />";
$message .= Input::get('Name') . " IP Addrress: " . $user->_ip() . "<br />";
}
// Set From: header
$from = Input::get('Name') . " <" . Input::get('Email') . ">";
// Email Headers
$headers = "From: " . $from . "\r\n";
$headers .= "Reply-To: ". Input::get('Email') . "\r\n";
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$mail = mail($adminEmail, $subject, $message, $headers);
} catch(Exception $e) {
die($e->getMessage());
}
}else{ //If validation fails display errros to screen
echo '<div class="error-message">';
foreach($validation->errors() as $error) {
echo '<p>'. str_replace('_', ' ', $error), '</p><br>'; //removes any '_' in Names
}
echo '</div>';
}
}
?>
<div class="navbar">
<ul>
<li><a href="index.html" id="dets">Home</a></li>
<li><a href="about.html" id="dets">About</a></li>
<li><a href="treatments.html" id="dets">Treatments</a></li>
<li><a href="portfolio.html" id="dets">Portfolio</a></li>
<li><a href="news.html" id="dets">Latest News</a></li>
<li><a href="contact.php" id="dets">Contact</a></li>
</ul>
</div>
<div class="backgroundframe">
<h1>Contact me</h1>
<p> Feel free to contact me via mobile (********), email (<a href="mailto:*******" target="top">******</a> ) or the form below! I will get back to you as soon as possible. <p>
<form class="cd-form floating-labels" action="" method="post"> <!-- Added Action and Methos to the form. -->
<fieldset>
<legend>Contact details</legend>
<div class="error-message">
<p>Please fill in all your details so I can be the best of help.</p>
</div>
<div class="icon">
<label class="cd-label" for="cd-name">Name</label>
<input class="user" type="text" name="cd-name" id="cd-name" required>
</div>
<div class="icon">
<label class="cd-label" for="cd-phone">Phone</label>
<input class="phone" type="text" name="cd-phone" id="cd-phone" required>
</div>
<div class="icon">
<label class="cd-label" for="cd-email">Email</label>
<input class="email" type="email" name="cd-email" id="cd-email" required>
</div>
</fieldset>
<fieldset>
<legend>Message</legend>
<div>
<h4>Subject</h4>
<p class="cd-select icon">
<select class="budget" required>
<option value="0">Select subject</option>
<option value="1">< $5000</option>
<option value="2">$5000 - $10000</option>
<option value="3">> $10000</option>
</select>
</p>
</div>
<div class="icon">
<label class="cd-label" for="cd-textarea">Message</label>
<textarea class="message" name="cd-textarea" id="cd-textarea" required></textarea>
</div>
<div>
<input type="submit" value="Send Message">
</div>
</fieldset>
</form>
<script src="contact/js/jquery-2.1.1.js"></script>
<script src="contact/js/main.js"></script> <!-- Resource jQuery -->
</body>
</html>
答案 0 :(得分:1)
当您提交表单时,它会从“姓名”,“电话”,“电子邮件”和“邮件”中查找数据。因为在你的PHP代码的顶部你需要'姓名','电话','电子邮件'和'消息'。但实际上你没有这些领域。你在你的html中将它们命名为'cd-name','cd-phone','cd-email'和'cd-textarea'。
更改php代码顶部的键名称或更改html代码中输入字段的名称。他们俩都应该一样。