将表单数据从php传递给ajax
问题描述:
我正在尝试将表单数据传递给php,并将其传递给ajax,以便为我的发送实际加载。将表单数据从php传递给ajax
form.html
<form method="POST" name="myemailform" action="form-to-email.php">
<div class="row">
<div class="col-md-6">
<h3 class="section-title">Our Address</h3>
<ul class="contact-info">
<li><i class="icon-location-pin"></i><span style="color:#fdfdfd;">Brisbane QLD, Australia</span></li>
<li><i class="icon-phone2"></i><span style="color:#fdfdfd;">Call Us at </span><a href="tel:0431397033"><span style="color:#FFA500;"><strong>0431397033</strong></span></a><span style="color:#fdfdfd;"> or </span><a href="tel:0405254333"><span style="color:#FFA500;"><strong>0405254333</strong></span></a></li>
<li><i class="icon-mail"></i><span style="color:#fdfdfd;">Email Us Here </span><a href="mailto:[email protected]"><span style="color:#FFA500;"><strong>Click Here</strong></span></a></li>
</ul>
</div>
<div class="col-md-6">
<div class="row" style="padding-top: 20px; background-color: rgba(0, 0, 0, 0.5);">
<div class="col-md-6">
<div class="form-group">
<input type="text" name="name" id="name" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<input type="text" name="emailaddress" id="emailaddress" class="form-control" placeholder="Email">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input type="text" name="subject" id="subject" class="form-control" placeholder="Subject">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<textarea name="comment" id="comment" class="form-control" id="" cols="30" rows="7" placeholder="Message"></textarea>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<input type="submit" id="submit" name="submit" value="Send Message" class="btn btn-primary btn-lg">
</div>
</div>
</div>
</div>
</div>
</form>
这里是我发送电子邮件功能
send.php
<?php
$errors = '';
$myEmail = "[email protected]";
// "[email protected]";
$name = $_POST['name'];
$emailaddress = $_POST['emailaddress'];
$subject = $_POST['subject'];
$comment = $_POST['comment'];
// validate first
if(empty($emailaddress) || empty($name)){
echo "name and email are mandatory";
exit;
}
if(!preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$emailaddress)){
$errors .="\n Error: Invalid Email Address";
}
$to = $myEmail;
$email_subject = $subject ;
$email_body = "You have received a new message.\n" .
"Here are the details: \n Name: $name \n " .
"Email: $emailaddress\n Message: \n $comment";
$headers[] ="MIME-Version: 1.0";
$headers[] ="Content-type:text/html; charset=ISO-8859-1";
$headers[] ="Content-Transfer-Encoding: base64";
$headers = "From: ". $emailaddress. "\n";
$headers .="Reply-To: [email protected]";
$result = mail($to,$email_subject,$email_body,$headers);
if($result){
echo "check";
} else {
echo "wrong";
}
?>
的PHP和AJAX我为我的HTML按钮有函数phpsendmail
function phpsendmail(){
$('#message').html('<i class="fa fa-spinner fa-spin" aria-hidden="true"></i>');
var name = $('#name').val();
var email = $('#emailaddress').val();
var subject = $('#subject').val();
var comment = $('#comment').val();
var datum = {
name: name,
email:email,
subject:subject,
comment:comment
}
$.ajax({
type: 'GET',
url: 'form-to-email.php',
data: datum,
success: function(response) {
if (response=='check') {
$('#message').html("<i class='fa fa-check' aria-hidden='true'></i>");
}else{
$('#message').html("<i class='fa fa-times' aria-hidden='true'></i>");
}
}
});
}
答
在你的ajax中你使用“get”来发送数据。
但在你的PHP您使用“邮报”得到的数据:
$.ajax({
type: 'post',
url: 'form-to-email.php',
data: datum,
success: function(response) {
if (response=='check') {
$('#message').html("<i class='fa fa-check' aria-hidden='true'></i>");
}else{
$('#message').html("<i class='fa fa-times' aria-hidden='true'></i>");
}
}
});
所以改变这种状况,我认为它会工作。
答
只是一个建议,我不知道还有阿贾克斯,但如果你想使用纯PHP那就试试这个一进动作网页:使用一个ID
<?php if(isset($_POST['nameofbutton'])){
echo $_POST['nameoffields'];
.
.
.
}?>
答
可以简化JS形式和使用下面的js代码:
$(document).ready(function(){
$('#signupForm').on('submit',function(e){
e.preventDefault();
$('#message').html('<i class="fa fa-spinner fa-spin" aria-hidden="true"></i>');
var data_arr=jQuery(this).serializeArray();
jQuery.ajax({
type:"POST",
url:"send.php",
data:data_arr,
success:function(response){
//alert(data);
//console.log(JSON.parse(data));
if (response=='check') {
$('#message').html("<i class='fa fa-check' aria-hidden='true'></i>");
}else{
$('#message').html("<i class='fa fa-times' aria-hidden='true'></i>");
}
}
});
});
});
答
你一定要做出4发生在你的代码/文件 -
1,从您的表单的动作和将onsubmit而不是即的onsubmit =“返回phpsendmail();”
2更改AJAX类型发表最多的建议也纠正你的一个AJAX网址到send.php在form.html
3,确保你已经包括jQuery的文件
4取代$ emailaddress = $ _POST ['emailaddress'] to $ emailaddress = $ _POST ['email'] in send.php
仍然不能正常工作 – NoobProgrammer
哦你的PHP更改$ emailaddress = $ _POST ['emailaddress'];到$ emailaddress = $ _POST ['email']; – Max
仍然我的回声反馈不是阿贾克斯 – NoobProgrammer