0
我不是很熟悉jQuery,但遇到此代碼通過從一個站點使用jQuery發送電子郵件。來自JavaScript的消息ajax
即時通訊至少在HTML/PHP中,您將擁有一個POST/GET和一個URL來指向一切。
有在最後一行:
$阿賈克斯({ 類型: 'POST', 網址:options.url,
,但我不知道如何處理與在服務器端。任何想法或指針?
感謝&新年快樂。
(功能($){
//define the new for the plugin ans how to call it
$.fn.contactable = function(options) {
//set default options
var defaults = {
name: 'Name',
email: 'Email',
message : 'Message',
subject : 'A contactable message',
submit : 'SEND',
recievedMsg : 'Thank you for your message',
notRecievedMsg : 'Sorry but your message could not be sent, try again later',
disclaimer: 'Please feel free to contact us',
hideOnSubmit: false
};
//call in the default otions
var options = $.extend(defaults, options);
//act upon the element that is passed into the design
return this.each(function() {
//construct the form
var this_id_prefix = '#'+this.id+' ';
$(this).html('<div id="contactable_inner"></div><form id="contactForm" method="" action=""><div id="loading"></div><div id="callback"></div><div class="holder"><p><label for="name">'+options.name+'<span class="red"> * </span></label><br /><input id="name" class="contact" name="name"/></p><p><label for="email">'+options.email+' <span class="red"> * </span></label><br /><input id="email" class="contact" name="email" /></p><p><label for="message">'+options.message+' <span class="red"> * </span></label><br /><textarea id="message" name="message" class="message" rows="4" cols="30" ></textarea></p><p><input class="submit" type="submit" value="'+options.submit+'"/></p><p class="disclaimer">'+options.disclaimer+'</p></div></form>');
//show/hide function
$(this_id_prefix+'div#contactable_inner').toggle(function() {
$(this_id_prefix+'#overlay').css({display: 'block'});
$(this).animate({"marginLeft": "-=5px"}, "fast");
$(this_id_prefix+'#contactForm').animate({"marginLeft": "-=0px"}, "fast");
$(this).animate({"marginLeft": "+=387px"}, "slow");
$(this_id_prefix+'#contactForm').animate({"marginLeft": "+=390px"}, "slow");
},
function() {
$(this_id_prefix+'#contactForm').animate({"marginLeft": "-=390px"}, "slow");
$(this).animate({"marginLeft": "-=387px"}, "slow").animate({"marginLeft": "+=5px"}, "fast");
$(this_id_prefix+'#overlay').css({display: 'none'});
});
//validate the form
$(this_id_prefix+"#contactForm").validate({
//set the rules for the fild names
rules: {
name: {
required: true,
minlength: 2
},
email: {
required: true,
email: true
},
message: {
required: true
}
},
//set messages to appear inline
messages: {
name: "",
email: "",
message: ""
},
submitHandler: function() {
$(this_id_prefix+'.holder').hide();
$(this_id_prefix+'#loading').show();
$.ajax({
type: 'POST',
url: options.url,
data: {subject:options.subject, name:$(this_id_prefix+'#name').val(), email:$(this_id_prefix+'#email').val(), message:$(this_id_prefix+'#message').val()},
success: function(data){
$(this_id_prefix+'#loading').css({display:'none'});
if(data == 'success') {
$(this_id_prefix+'#callback').show().append(options.recievedMsg);
if(options.hideOnSubmit == true) {
//hide the tab after successful submition if requested
$(this_id_prefix+'#contactForm').animate({dummy:1}, 2000).animate({"marginLeft": "-=450px"}, "slow");
$(this_id_prefix+'div#contactable_inner').animate({dummy:1}, 2000).animate({"marginLeft": "-=447px"}, "slow").animate({"marginLeft": "+=5px"}, "fast");
$(this_id_prefix+'#overlay').css({display: 'none'});
}
} else {
$(this_id_prefix+'#callback').show().append(options.notRecievedMsg);
setTimeout(function(){
$(this_id_prefix+'.holder').show();
$(this_id_prefix+'#callback').hide().html('');
},2000);
}
},
error:function(){
$(this_id_prefix+'#loading').css({display:'none'});
$(this_id_prefix+'#callback').show().append(options.notRecievedMsg);
}
});
}
});
});
};
})(jQuery);
什麼PHP頁面是它叫「選項.url「我創建了一個名爲message.php的頁面來處理它。還有, echo json_encode(array(「data」=>「」。$ value));一個正確的回聲,以便它可以判斷它失敗的成功? – DevilCode
'options'是您在初始化插件時傳入的對象。 '$('form')。contactable({url:'message.php'})'例如。是不是在文檔中概述?...您應該永遠不必觸摸插件內的代碼,除非修改插件的功能 – charlietfl
好吧,所以將options.url改爲'www.site.com/message.php'我沒有想法什麼時候或什麼在選擇通過。我知道我需要設置回電,以便知道它是否成功。上述評論是否正確? – DevilCode