所以即时制作一个电子邮件表单,并使用AJAX链接到使用PHPMailer发送电子邮件的外部PHP文件。所以我在PHP的顶部有这个代码:
if(count($_POST) > 0){
$message=
'Full Name: '.$_POST['name'].'<br />
Email: '.$_POST['email'].'<br />
Message: '.$_POST['textarea'].'<br />
Current Website: '.$_POST['website'].'<br />
Business Name: '.$_POST['business'].'<br />
';
和
if(count($_POST) > 0)
如果if语句被删除,它会发送电子邮件,我确实收到了,但电子邮件只有&#34;全名:&#34;和&#34;电子邮件:&#34;等等,填充空格,没有从HTML表单输入的值。这是AJAX代码:
var ajax = {
isSubmiting: false,
send: function() {
if(ajax.isSubmiting == false) {
ajax.isSubmiting = true;
var name = $("input[name=name]").val();
var email = $("input[name=email]").val();
var textarea = $("textarea").val();
var business = $("input[name=business]").val();
var website = $("input[name=website]").val();
ajax.SetText("Sending...");
$.post("sendmail.php", {
name: name, email: email, textarea: textarea, business: business, website: website
}, function(data) {
if (data.trim() == "true") {
ajax.SetText("Sent!");
console.log(data);
$.get("sent.html", function(sentData){
$("#content").html(sentData);
});
} else {
ajax.SetText("Send mail");
console.log(data);
$.get("unsent.html", function(sentData){
$("#content").html(sentData);
});
}
ajax.isSubmiting = false;
});
}
else alert("You can only send one email at a time!");
},
SetText: function(text) {
$("input[type=button]").val(text);
}
}
这是表单HTML标记:
<form name="contact-form" method="post" id="contact-form">
<h3> We want to hear from you! Tell us about your business and/or project and what you need and we'll get back to you as soon as possible! </h3>
<div class="form-group">
<label for="name">Your Name:</label>
<input type="text" name="name" class="form-control" placeholder="Your Name" required/>
</div>
<div class="form-group">
<label for="email">Your Email:</label>
<input type="email" name="email" class="form-control" placeholder="Your Name" required/>
</div>
<div class="form-group">
<label for="web">Current Website (if available):</label>
<input type="text" name="website" class="form-control" placeholder="Current Website (if available)"/>
</div>
<div class="form-group">
<label for="web">Business Name (if available):</label>
<input type="text" name="business" class="form-control" placeholder="Business Name (if available)"/>
</div>
<div class="form-group">
<label for="comment">Message:</label>
<textarea id="textarea" rows="5" class="form-control" name="textarea" placeholder="Message"></textarea>
</div>
<div class="row" align="center" id="button-row2">
<input type="button" value="Submit" onClick="ajax.send()" class="btn">
</div>
</form>
知道为什么会这样吗?
答案 0 :(得分:0)
在您的实时环境中无法正确访问jQuery。因此,
var name = $("input[name=name]").val();
var email = $("input[name=email]").val();
var textarea = $("textarea").val();
var business = $("input[name=business]").val();
var website = $("input[name=website]").val();
都是undefined