JQuery / PHP mail sending an easy way?

Good to make a long story short:

JavaScript:

jQuery("submit").click(function() { var address = jQuery("#mail").val(); var title = jQuery("#title").val(); var name = jQuery("#name").val(); var mail = jQuery("#email").val(); var message = jQuery("#msg").val(); alert(address); alert(title); alert(name); alert(mail); alert(message); jQuery.post("sendmail.php", {address: address, title: title, name: name, mail: mail , message: message}, function(data){ jQuery("#email").html("<div id='sent'><p>Thank you for the message.</p><p>We will reply as soon as possible.</p></div>"); alert('sent'); }); return false; }); 

It works smoothly (shows each value in the warning), but never shows the "sent" div. And never sends the actual mail.

sendmail.php is in the right place, and its code is:

 <?php // getting variables from form $emailTo = trim($_REQUEST['address']); $subject = trim($_REQUEST['title']);; $name = trim($_REQUEST['name']); $emailFrom = trim($_REQUEST['mail']); $message = $_REQUEST['message']; // prepare email body text $Body = "You have a message from: "; $Body .= $name; $Body .= "\n"; $Body .= "\n"; $Body .= $message; // send prepared message $sent = mail($emailTo, $subject, $Body); //callback for jQuery AJAX if ($sent){ echo 'sent'; } else{} print_r($_REQUEST); die(); ?> 

Updated jQuery code, also does not work:

 jQuery("#contact-form-send").click(function() { var address = jQuery("#contact-form-mail-address").val(); var title = jQuery("#contact-form-mail-title").val(); var name = jQuery("#contact-form-name").val(); var mail = jQuery("#contact-form-email").val(); var message = jQuery("#contact-form-message").val(); var data = "&address=" + address + "&title=" + title + "&name=" + name + "&mail=" + mail + "&message=" + message; jQuery.ajax({ type: "POST", url: "sendmail.php", data: data, success: function(){ jQuery(".email-us").html("<div id='email-sent'><p>Thank you for the message.</p><p>We will reply as soon as possible.</p></div>"); } }); return false; }); 
+4
source share
1 answer

You must use Ajax. It is much simpler. The following is a simple tutorial on sending mail using PHP, Ajax and jQuery:

Send mail using PHP, Ajax and jQuery

it will look something like this:

 var data = "address=" + address + "&title=" + title + "&name=" + name + "&mail=" + mail + "&message=" + message; //or however u want to format your data $.ajax({ type: "POST", url: "sendmail.php", data: data, success: function(phpReturnResult){ alert('Success: ' + phpReturnResult); jQuery(".email-us").html("<div id='email-sent'><p>Thank you for the message.</p><p>We will reply as soon as possible. PHP  return result: " + phpReturnResult + "</p></div>"); }, error: function(errormessage) { //you would not show the real error to the user - this is just to see if everything is working alert('Sendmail failed possibly PHP : ' + errormessage); } }); 

Also in your PHP file, you seem to have used the mail function twice.

+5
source

Source: https://habr.com/ru/post/1340453/


All Articles