jQuery/PHP mail send the easy way?
好吧,长话短说:
JavaScript:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | 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("<p>Thank you for the message.</p><p>We will reply as soon as possible.</p>"); alert('sent'); }); return false; }); |
工作流畅(在警报中显示每个值),但从不显示div"已发送"。而且从不发送实际的邮件。
sendmail.php在正确的位置,其代码为:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | <?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 .="\ "; $Body .="\ "; $Body .= $message; // send prepared message $sent = mail($emailTo, $subject, $Body); //callback for jQuery AJAX if ($sent){ echo 'sent'; } else{} print_r($_REQUEST); die(); ?> |
更新的jQuery代码也无法正常工作:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | 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("<p><center>[wp_ad_camp_2]</center></p><p>Thank you for the message.</p><p>We will reply as soon as possible.</p>"); } }); return false; }); |
您应该使用Ajax。它容易得多。这是有关使用PHP,Ajax,