0
我有一个通过jQuery $.post
传递数据的联系表单。
JS
$(function() {
$("#contact_form").submit(function (a) {
a.preventDefault();
$.post("<?php echo home_url('/_asset/contact.php'); ?>", {
contact_name : $("#contact_name").val(),
contact_email : $("#contact_email").val(),
contact_subject : $("input:radio[name=subject]:checked").val(),
contact_textarea: $("#contact_textarea").val(),
contact_postid : $("#contact_postid").val(),
}, function (a) {
$("div#response").removeClass("hidden");
$("div#response").delay(1E3).html(a);
});
});
});
contact.php
$contact_name = $_POST['contact_name'];
$contact_email = $_POST['contact_email'];
$contact_subject = $_POST['contact_subject'];
$contact_message = $_POST["contact_textarea"];
$contact_postid = $_POST['contact_postid'];
$contact_address = $_SERVER['REMOTE_ADDR'];
if(empty($contact_name) && empty($contact_email) && empty($contact_subject) && empty($contact_message)) {
die('You must fill out all fields amigo!');
}
// Build that email boy!
if(!empty($contact_postid)) { $email_id = ' (' . $contact_postid . ')'; }
$email_to = '[email protected]';
$email_subject = 'Contact Form: ' . $contact_subject . $email_id;
$email_header = 'From: ' . $contact_name . '<' . $contact_email . '>' . "\r\n";
$email_header .= 'Reply-To:' . $contact_email . "\r\n";
$email_header .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$email_message = nl2br($contact_message);
// Try sending the email
if(!mail($email_to, $email_subject, $email_message, $email_header)){
$status = 'red';
die('Error sending email.');
} else {
$status = 'green';
die('Email sent!');
}
PHP形式
<div class="respond_form">
<form method="post" id="contact_form">
<h2>Let's get contacting!</h2>
<div id="response" class="hidden alert <?php echo $status; ?>"></div>
<div class="line">
<label for="contact_name" title="Please enter your name (required)">Your name <span class="required">*</span></label>
<input type="text" name="contact_name" id="contact_name" tabindex="1" placeholder="John Smith" required="required"/>
</div>
<div class="line">
<label for="contact_email" title="Please enter your email (required)">Your email (so we can contact you) <span class="required">*</span></label>
<input type="email" name="contact_email" id="contact_email" tabindex="2" placeholder="[email protected]" required="required"/>
</div>
<?php if(isset($_GET['subject'])) { ?>
<input hidden="hidden" name="subject" type="radio" value="<?php echo $_GET['subject']; ?>" checked="checked">
<?php if(isset($_GET['PostID'])) { echo '<input hidden="hidden" id="contact_postid" name="postid" type="input" value="' . $_GET['PostID'] . '">'; } ?>
<?php } else { ?>
<div class="line">
<label>What is the message in regards to? <span class="required">*</span></label>
<ul style="list-style:none; margin: 0; padding: 0;">
<label style="font-weight:normal;"><input style="margin-right: 10px;" name="subject" type="radio" value="Advertising"<?php if($_GET['subject'] == 'advertising') { echo ' checked="checked"'; } ?>>Advertising</label>
<label style="font-weight:normal;"><input style="margin-right: 10px;" name="subject" type="radio" value="Contribute an Article"<?php if($_GET['subject'] == 'contribute') { echo ' checked="checked"'; } ?>>Contribute an Article</label>
</ul>
</div>
<?php } ?>
<div class="line">
<label for="contact_textarea" title="Briefly explain your message (required)">Briefly explain your message <span class="required">*</span></label>
<textarea name="contact_textarea" id="contact_textarea" rows="10" tabindex="3" maxlength="500" required="required"></textarea>
</div>
<input type="submit" id="contact_send" name="contact_send" class="button glow" value="Send Message"/>
</form>
</div>
正如你所看到的,我已经试过SE在mail()
函数中设置了$status
,但这不起作用。我不完全确定整个事情发生了什么(发现它很久以前,只是建立在它周围),但我知道die()
消息发射到div#response
。
如果电子邮件成功或不成功,我希望能够为div#response
添加一个类。
OH!和incase有人想评论缺乏安全性,或检查$_POST
数据,我已经剥离它的这里:]
你能提供比“不起作用”更糟的细节吗?事情失败的地方?代码是否产生错误信息?你期望它们的变量的值是什么? – ASGM 2013-05-05 12:07:04
你期待'$ status'做什么?您在设置之后立即调用'die()',以便它永远不会用于任何任何操作 – charlietfl 2013-05-05 12:12:51
您的检查是否所有字段都已填写实际上只是测试是否至少有一个字段已被填写。你应该使用'||'而不是'&&'。 – Arjan 2013-05-05 12:17:52