Reputation: 11
recently i had problem with attaching file into email, i handle that, thanks for you guys btw. Now i have next problem connected with "fixed" attching the file. Actually there is more problems than i thought. 1) in code below, in validation plugin, i thought that it's enought to call in rules somenthing like this "name : requred", but it isn't. To make it works i have to call in form class="required", why ? When i remove class="requred" validation is no longer exist.
2) Ok, so class="required", small problem, validation is working, but what happend with submitHandler ? ajax doesnt run, website refresh and i get succes message. My point is to send email with attachment without refreshig.
3) to make matters worse, the "message" that i wrote to uploaded_file rule, which should show when file is not uploaded, doesnt appear, insted the message from title attribute (in form) come into.
so everything is not that i was supposed to by. What shoud i repair/fix/change to make it work? please help me
the codes: form:
<form method="post" name="formularzaplikacyjny" enctype="multipart/form-data" action="mail-attachment.php" id="formmail">
<div id="imiediv"><label for="name">Imię i nazwisko: <em>*</em> </label><br>
<input type="text" name="name" id="name" class="required" title="Wpisz swoje imię i nazwisko" placeholder="Jan Kowalski"></div><br>
<div id="emaildiv"><label for="email">Email: <em>*</em> </label><br>
<input type="text" name="email" class="required" id="email" title="Wpisz swój adres email" placeholder="[email protected]"></div><br>
<div id="listdiv"><label for="message">List motywacyjny: <em>*</em></label><br>
<textarea name="message" rows="5" cols="48" class="required" id="message" title="Wpisz treść listu motywacyjnego" placeholder="Tutaj zpowinna znaleźć się treść Twojego listu motywacyjnego" ></textarea></div>
<div id="cvdiv"><label for="uploaded_file">Wybierz plik CV: <em>*</em></label><br>
<input type="file" name="uploaded_file" title="<h3>Wybierz plik CV do przesłania</h3>" class="required" id="uploaded_file"></div><br>
<input type="submit" value="Prześlij" name="submit" id="submitbutton">
</form>
<div id="loading-mail">
<h2>Wysyłamy maila.....</h2>
</div>
validation :
$("#formmail").validate({
rules: {
email: {
required: true,
email: true
},
name: {
required: true
},
message: {
required: true
},
uploaded_file: {
requred: true
}
}, //koniec literału obiektowego rules
messages: {
email: {
required: "<h3>Podaj adres e-mail.</h3>",
email: "<h3>To nie jest prawidłowy <br>adres e-mail.</h3>"
},
name: {
required: "<h3>Podaj swoje imię i nazwisko.</h3>"
},
message: {
required: "<h3>Wpisz treść listu motywacyjnego.</h3>"
},
uploaded_file: {
requred: "<h3>Prześlij plik CV</h3>"
}
},submitHandler: function() {
var thisForm = $('#formmail');
$('#formmail').fadeOut(function(){
//Display the "loading" message
$("#loading-mail").fadeIn(function(){
//Post the form to the send script
$.ajax({
type: 'POST',
url: thisForm.attr("action"),
data: thisForm.serialize(),
//Wait for a successful response
success: function(data){
//Hide the "loading" message
$("#loading-mail").fadeOut(function(){
//Display the "success" message
$("#success").text(data).fadeIn();
});
}
});
});
});
}
}); // koniec funkcji validate
and sending script:
<?php
require "PHPMailer/class.phpmailer.php";
$mail = new PHPMailer(true); //New instance, with exceptions enabled
$mail->CharSet = "UTF-8";
$bodys="<b>Podanie od:</b> ".$_POST['name']."<br/>"."<b>Adres e-mail: </b>".$_POST['email']."<br/>"."<b>Treść listu motywacyjnego: </b><br/>".$_POST['message'];
$mail->Body =$bodys;
$mail->IsSMTP(); // tell the class to use SMTP
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP server port
$mail->Host = "mail.xxx.linuxpl.info"; // SMTP server
$mail->Username = "username"; // SMTP server username
$mail->Password = "pass"; // SMTP server password
$mail->IsSendmail(); // tell the class to use Sendmail
$mail->AddReplyTo($_POST['email'],$_POST['name']);
$mail->From = $_POST['email']; //uzupełnij sobie
$mail->FromName = $_POST['name']; //uzupełnij sobie
$to = '[email protected]'; //na jaki mail wysłać np [email protected]
$mail->AddAddress($to);
$mail->Subject = "Nowe podanie o pracę";
$mail->AltBody = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
$mail->WordWrap = 80; // set word wrap
$mail->MsgHTML($bodys);
$plik_tmp = $_FILES['uploaded_file']['tmp_name'];
$plik_rozmiar = $_FILES['uploaded_file']['size'];
$plik_nazwa = $_FILES['uploaded_file']['name'];
if(is_uploaded_file($plik_tmp)) {
$nazwa_g=$plik_nazwa;
move_uploaded_file($plik_tmp, 'tmp_zal/'.$nazwa_g);
$mail->AddAttachment('tmp_zal/'.$nazwa_g, $nazwa_g);
}
$mail->IsHTML(true); // send as HTML
if(!$mail->Send())
{
echo "Błąd";
echo "Kod błędu: " . $mail->ErrorInfo;
}
else
{
echo 'Wiadomość została wysłana';
}
?>
Upvotes: 0
Views: 1455
Reputation: 388316
Issues 1 & 3 are because you misspelled required
as requred
in the uploaded_file
rule and message.
For issue 2 you need to return false
from submitHandler
to cancel the default form submission
jQuery(function ($) {
$("#formmail").validate({
rules: {
email: {
required: true,
email: true
},
name: {
required: true
},
message: {
required: true
},
uploaded_file: {
required: true
}
}, //koniec literału obiektowego rules
messages: {
email: {
required: "<h3>Podaj adres e-mail.</h3>",
email: "<h3>To nie jest prawidłowy <br>adres e-mail.</h3>"
},
name: {
required: "<h3>Podaj swoje imię i nazwisko.</h3>"
},
message: {
required: "<h3>Wpisz treść listu motywacyjnego.</h3>"
},
uploaded_file: {
required: "<h3>Prześlij plik CV</h3>"
}
},submitHandler: function() {
var thisForm = $('#formmail');
$('#formmail').fadeOut(function(){
//Display the "loading" message
$("#loading-mail").fadeIn(function(){
//Post the form to the send script
$.ajax({
type: 'POST',
url: thisForm.attr("action"),
data: thisForm.serialize(),
//Wait for a successful response
success: function(data){
//Hide the "loading" message
$("#loading-mail").fadeOut(function(){
//Display the "success" message
$("#success").text(data).fadeIn();
});
}
});
});
});
return false
}
});
});
Demo: Fiddle
Note: The ajax submission will not work because of the file input in the form - for some alternates see this answer
Upvotes: 1