我在通过Ajax发送此表单时遇到了一些麻烦。我开始获取要发送的值,但是一旦添加单选按钮var messagetype
就停止了。由于添加变量,页面不再传递任何值,而是传递它们之前的位置。
我认为它与var dataString
有关,并且值没有正确传递,并且因为添加var messagetype
后它开始出错,所以必须从这里开始。
我已经使用var messagetype
的代码添加了一个类并尝试了它,编写了一些方法,但仍无济于事。
这是我的代码:
js
$('#formsend').click(function(){
var name = $("input#name").val();
var email = $("input#email").val();
var phone = $("input#phone").val();
//Where I think it's going wrong:
var messagetype = $("input[name='messagetype']:checked").val();
var trackurl = $("input#trackurl").val();
var trackdesc = $("input#trackdesc").val();
var eventdate = $("input#eventdate").val();
var eventdesc = $("input#eventdesc").val();
var adrsone = $("input#adrsone").val();
var adrstwo = $("input#adrstwo").val();
var adrsthree = $("input#adrsthree").val();
var pcode = $("input#pcode").val();
var detail = $("input#subject").val();
var note = $("input#note").val();
var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone +'&messagetype=' + messagetype + '&trackurl=' + trackurl + '&trackdesc=' + trackdesc + '&eventdate=' + eventdate + '&eventdesc=' + eventdesc + '&adrsone=' + adrsone + '&adrstwo=' + adrstwo + '&adrsthree=' + adrsthree + '&pcode=' + pcode + '&detail=' + detail + '¬e=' + note;
$.ajax({
type: "POST",
url: "processmail.php",
data: dataString,
success: function() {
$('#form').html("<div id='message'></div>");
$('#message').html("<h2>Message Submitted.</h2>")
.append("<p>Thank you for contacting me, I will be in touch soon.</p>")
.hide()
.fadeIn(1500);
}
});
return false;
}); //end form ajax
processmail.php
<?php
$name=sanitiseString($_POST['name']);
$email=sanitiseString($_POST['email']);
$phone=sanitiseString($_POST['phone']);
$messagetype=sanitiseString($_POST['messagetype']);
$trackurl=sanitiseString($_POST['trackurl']);
$trackdesc=sanitiseString($_POST['trackdesc']);
$eventdate=sanitiseString($_POST['eventdate']);
$eventdesc=sanitiseString($_POST['eventdesc']);
$adrsone=sanitiseString($_POST['adrsone']);
$adrstwo=sanitiseString($_POST['adrstwo']);
$adrsthree=sanitiseString($_POST['adrsthree']);
$pcode=sanitiseString($_POST['pcode']);
$detail=sanitiseString($_POST['detail']);
$note=sanitiseString($_POST['note']);
$to='emailme@myemail.com';
$subject='Message from Form: '.$messagetype;
$headers = 'MIME-Version: 1.0' . "\r\n";
$headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";
$headers .='From: '.$name;
$headers .='Reply-To: '.$email;
if($messagetype == 'track'){
$message='<table>
<tr><td>Name: </td><td>'.$name.'</td></tr>
<tr><td>Email: </td><td>'.$email.'</td></tr>
<tr><td>Track Description: </td><td>'.$trackdesc.'</td></tr>
<tr><td>Track Link: </td><td>'.$trackurl.'</td></tr>
<table>';
}
elseif($messagetype == 'event'){
$message='<table>
<tr><td>Name: </td><td>'.$name.'</td></tr>
<tr><td>Email: </td><td>'.$email.'</td></tr>
<tr><td>Event Description: </td><td>'.$eventdesc.'</td></tr>
<tr><td>Event Date: </td><td>'.$eventdate.'</td></tr>
<tr><td>Location: </td><td>'.$adrsone.'</td></tr>
<tr><td> </td><td>'.$adrstwo.'</td></tr>
<tr><td> </td><td>'.$adrsthree.'</td></tr>
<tr><td> </td><td>'.$pcode.'</td></tr>
<table>';
}
elseif($messagetype == 'message'){
$message='<table>
<tr><td>Name: </td><td>'.$name.'</td></tr>
<tr><td>Email: </td><td>'.$email.'</td></tr>
<tr><td>Subject: </td><td>'.$detail.'</td></tr>
<tr><td>Message: </td><td>'.$note.'</td></tr>
<table>';
}
mail($to, $subject, $message, $headers);
function sanitiseString($var)
{
$var = stripslashes($var);
$var = htmlentities($var);
$var = strip_tags($var);
return $var;
}
?>
我是新手,并且已经按照一些教程来实现它,所以现在有点卡住它为什么不起作用。
非常感谢任何帮助。
答案 0 :(得分:3)
使用serialize() or SerializeArray
var dataString = $('#FORMID').serialize();
并将dataString
传递到$.ajax
data
的数据变量
<强>&安培;在php部分中,您可以获得表单中存在的所有字段值
使用GET or POST
方法
messagetype
在您的表单中设置隐藏字段或将其附加到dataString
,以便您在php文件中获取messagetype
答案 1 :(得分:2)
尝试将对象用于数据参数。
$.ajax({
type: "POST",
url: "processmail.php",
data: {'name': name,
'email': email},
success: function() {
$('#form').html("<div id='message'></div>");
$('#message').html("<h2>Message Submitted.</h2>")
.append("<p>Thank you for contacting me, I will be in touch soon.</p>")
.hide()
.fadeIn(1500);
}
});