我在(单页业务/投资组合)网站的底部有一个联系表格。
我的DOCTYPE上方的脚本看起来像这样。
<?php
//If the form is submitted
if(isset($_POST['submit'])) {
//Check to make sure that the name field is not empty
if(trim($_POST['contactname']) == '') {
$hasError = true;
} else {
$name = trim($_POST['contactname']);
}
//Check to make sure that the subject field is not empty
if(trim($_POST['subject']) == '') {
$hasError = true;
} else {
$subject = trim($_POST['subject']);
}
//Check to make sure sure that a valid email address is submitted
if(trim($_POST['email']) == '') {
$hasError = true;
} else if (!eregi("^[A-Z0-9._%-]+@[A-Z0-9._%-]+\.[A-Z]{2,4}$", trim($_POST['email']))) {
$hasError = true;
} else {
$email = trim($_POST['email']);
}
//Check to make sure comments were entered
if(trim($_POST['comment']) == '') {
$hasError = true;
} else {
if(function_exists('stripslashes')) {
$comments = stripslashes(trim($_POST['message']));
} else {
$comments = trim($_POST['message']);
}
}
//If there is no error, send the email
if(!isset($hasError)) {
$emailTo = 'myemail@gmail.com'; //Put your own email address here
$body = "Name: $name \n\nEmail: $email \n\nSubject: $subject \n\nComments:\n $comments";
$headers = 'From: My Site <'.$emailTo.'>' . "\r\n" . 'Reply-To: ' . $email;
mail($emailTo, $subject, $body, $headers);
$emailSent = true;
}
}
?>
JQuery验证确实有效。
<script type="text/javascript">
$(document).ready(function(){
$("form").validate();
});
</script>
联系表格:
<div class="grid_8">
<?php if(isset($hasError)) { //If errors are found ?>
<p class="error">Please check if you've filled all the fields with valid information. Thank you.</p>
<?php } ?>
<?php if(isset($emailSent) && $emailSent == true) { //If email is sent ?>
<p><strong>Email Successfully Sent!</strong></p>
<p>Thank you <strong><?php echo $name;?></strong> for using my contact form! Your email was successfully sent and I will be in touch with you soon.</p>
<?php } ?>
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div>
<label for="name">Your Name:</label>
<div>
<input type="text" name="contactname" class="required" />
</div>
</div>
<div>
<label for="email">Your Email:</label>
<div>
<input type="text" name="email" class="required email" />
</div>
</div>
<div>
<label for="subject">Subject:</label>
<div>
<input type="text" name="subject" class="required" />
</div>
</div>
<div>
<label for="comments">Comments:</label>
<div>
<textarea name="comment" name="comment" class="required"></textarea>
</div>
</div>
<div>
<input id="button" type="submit" value="SEND" />
</div>
</form>
</div>
当您提交表单时,不会发送电子邮件,也不会发送任何来自php的验证。我做错了什么?
答案 0 :(得分:2)
如果您的表单中没有isset($_POST['submit'])
字段,我不明白为什么name="submit"
应该返回true。
我更喜欢使用数组来获取所有必需的数据:
<!-- ... -->
<input type="text" name="mail[contactname]" class="required" />
<!-- ... -->
<input type="text" name="mail[email]" class="required email" />
<!-- ... -->
<input type="text" name="mail[subject]" class="required" />
<!-- ... -->
<textarea name="comment" name="mail[comment]" class="required"></textarea>
<!-- ... -->
然后要求那个数组
if (isset($_POST['mail']))
// ...
答案 1 :(得分:2)
一个明显的问题,虽然我有许多不那么重要的问题。只需将您检查$_POST['submit']
放在表格中name="submit"
即可。
所以改变:
<input id="button" type="submit" value="SEND" />
要:
<input id="button" type="submit" name="submit" value="SEND" />
或改变:
if(isset($_POST['submit']))
要:
if(count($_POST))
// OR
if(!empty($_POST))
任何一个都可以解决您的问题