我在通過Ajax發送此表單時遇到了一些麻煩。我開始讓價值發送通過,但一旦添加單選按鈕var messagetype
就停下來。由於添加變量,頁面不再通過任何值,而是像以前那樣通過它們。將表單值通過AJAX傳遞給php
我認爲它與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='[email protected]';
$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;
}
?>
我是新來這一點,並遵循一些教程來實現它,我現在有點卡住,以爲什麼它不起作用。
任何幫助將不勝感激。
我選擇了這個答案,因爲我是一個新手,它工作得很好。現在正在工作,謝謝。我現在遇到的問題是通過textarea字段,我已經將代碼從'input#eventdesc'更改爲'textarea#eventdesc',但仍然沒有通過變量傳遞? – ThePagan