Poniewaz nie znam sie jeszcze zbyt dobrze na jquery a o php nie wiem wlasciwie nic - formularzow kontaktowych do stron szukam w internecie.
Dzis wygrzebalem cos dosc ciekawego - idealnie nadajacego sie do projektu nad ktorym akualnie pracuje:
Link do oryginalnego tutka
Wprowadzilem kilka zmian:
1. Dodalem element textarea - na tresc wiadomosci - oraz kod jquery sprawdzajacy czy jakas wiadomosc zostala w ogole wpisana.
3. Pozmienialem troche w stylach - usunalem pozycjonowanie absolutne, zmienilem polozenie ostrzezenia o bledzie w wypelnieniu formularza.
4. Zmianilem adresy email na ktore maja przychodzic wiadomosci z formularza- zgodnie z instrukcia zawarta w readme.txt
(The process.php file in /bin does need adjustment to send an email correctly. At a minimum, you need to change the $to, $email, $fromaddress, and $mail->From to get it to work )
I wszystko by bylo pieknie gdyby to dzialalo:]
Wyskakuje blad 405 Method Not Allowed
The requested method POST is not allowed for the URL /ajax/index.html.
Sprawdzanie formularza dziala poprawnie. Jednak w momencie w ktorym formularz jest komplenty i ma zostac przekazany do scryptu wykonawczego dzieje sie cos dla mnie nie zrozumialego.
plik html mozna podejrzec tutaj:
Link do strony na serwerze
kod JS (JQuery)
Kod
$(function() {
$('.error').hide();
$('input.text-input').css({backgroundColor:"#FFFFFF"});
$('input.text-input').focus(function(){
$(this).css({backgroundColor:"#CCC"});
});
$('input.text-input').blur(function(){
$(this).css({backgroundColor:"#FFFFFF"});
});
$(".button").click(function() {
// validate and process form
// first hide any error messages
$('.error').hide();
var name = $("input#name").val();
if (name == "") {
$("label#name_error").show();
$("input#name").focus();
return false;
}
var email = $("input#email").val();
if (email == "") {
$("label#email_error").show();
$("input#email").focus();
return false;
}
var phone = $("input#phone").val();
if (phone == "") {
$("label#phone_error").show();
$("input#phone").focus();
return false;
}
var mess = $("textarea#mess").val();
if (mess == "") {
$("label#mess_error").show();
$("textarea#mess").focus();
return false;
}
var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone +'&mess=' + mess;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
success: function() {
$('#contact_form').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='checkmark' src='images/check.png' />");
});
}
});
return false;
});
});
runOnLoad(function(){
$("input#name").select().focus();
});
$('.error').hide();
$('input.text-input').css({backgroundColor:"#FFFFFF"});
$('input.text-input').focus(function(){
$(this).css({backgroundColor:"#CCC"});
});
$('input.text-input').blur(function(){
$(this).css({backgroundColor:"#FFFFFF"});
});
$(".button").click(function() {
// validate and process form
// first hide any error messages
$('.error').hide();
var name = $("input#name").val();
if (name == "") {
$("label#name_error").show();
$("input#name").focus();
return false;
}
var email = $("input#email").val();
if (email == "") {
$("label#email_error").show();
$("input#email").focus();
return false;
}
var phone = $("input#phone").val();
if (phone == "") {
$("label#phone_error").show();
$("input#phone").focus();
return false;
}
var mess = $("textarea#mess").val();
if (mess == "") {
$("label#mess_error").show();
$("textarea#mess").focus();
return false;
}
var dataString = 'name='+ name + '&email=' + email + '&phone=' + phone +'&mess=' + mess;
//alert (dataString);return false;
$.ajax({
type: "POST",
url: "process.php",
data: dataString,
success: function() {
$('#contact_form').html("<div id='message'></div>");
$('#message').html("<h2>Contact Form Submitted!</h2>")
.append("<p>We will be in touch soon.</p>")
.hide()
.fadeIn(1500, function() {
$('#message').append("<img id='checkmark' src='images/check.png' />");
});
}
});
return false;
});
});
runOnLoad(function(){
$("input#name").select().focus();
});
PHP
<?php } else {$name = 'No name entered';} } else {$email = 'No email entered';} } else {$phone = 'No phone entered';} } else {$phone = 'No message entered';} ?> <html> <head> <style type="text/css"> </style> </head> <body> <table width="550" border="1" cellspacing="2" cellpadding="2"> <tr bgcolor="#eeffee"> <td>Name</td> <td><?=$name;?></td> </tr> <tr bgcolor="#eeeeff"> <td>Email</td> <td><?=$email;?></td> </tr> <tr bgcolor="#eeffee"> <td>Phone</td> <td><?=$phone;?></td> </tr> <tr bgcolor="#eeffee"> <td>Message</td> <td><?=$mess;?></td> </tr> </table> </body> </html> <? $to = 'xxx@yyyy'; $email = 'zzz@yyyy'; $fromaddress = "aaa@bbbb"; $fromname = "Online Contact"; require("phpmailer.php"); $mail = new PHPMailer(); $mail->From = "xxx@yyyy"; $mail->FromName = "Contact Form"; $mail->AddAddress("email_address@example.com","Name 1"); $mail->AddAddress("another_address@example.com","Name 2"); $mail->WordWrap = 50; $mail->IsHTML(true); $mail->Subject = "Demo Form: Contact form submitted"; $mail->Body = $body; $mail->AltBody = "This is the text-only body"; if(!$mail->Send()) { $recipient = 'your_email@example.com'; $subject = 'Contact form failed'; $content = $body; mail($recipient, $subject, $content, "From: mail@yourdomain.com\r\nReply-To: $email\r\nX-Mailer: DT_formmail"); exit; } ?>
Oprocz tych 3 - formularz posiada phpmailer oraz scrypt runonload.js
Probowalem odniesc sie do oryginalu - zmieniajac tylko adres maila w miejscu podanym w instrukcji - co prawda blad nie wyskakuje - pokazuje sie informacja o wyslalej wiadomosci -ale sam mail nigdy nie dociera.
W momencie kiedy zmieniam wartosc ktorej nie powinienem (zgodnie z instrukcja podana przez autora) mail dociera ale w postaci kolorowej ramki ktora tworzy plik wykonawczy php.
Link do funkcjonalnego oryginalu - ale bez pola textarea
Przesiedzialem juz nad tym dzis kilka godzin i najwyrazniej niz z mojej wiedzy juz nie wydoje:]
Jezeli macie chwilke bylbym ogromnie wdzieczny za jakies sugestie.
pozdrawiam, Piotrek