我的AJAX联系表格有什么问题? (php mail()) [英] What's wrong with my AJAX contact form? (php mail())

查看:129
本文介绍了我的AJAX联系表格有什么问题? (php mail())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用jQuery和PHP表单创建了我的第一个Ajax联系人,但它似乎不起作用.

I`ve created my first ajax contact using jQuery and PHP form but it doesn't seem to work.

我的jQuery代码:

My jQuery code:

var dataString = 'address=' + address + '&title=' + title + '&name=' + name + '&mail=' + mail + '&message=' + message;
jQuery.ajax({
    type: "POST",
    url: "sendmail.php",
    data: dataString,
    success: function () {
        alert(dataString);
        jQuery('#ok').html("<h2>Contact Form Submitted!</h2>")
    }
});
return false;

注释-我正在使用警报来显示数据字符串,并且它恰好显示了我的字符串,此外,此脚本始终会成功并显示已提交联系表单".

Comment - I`m using alert to display the data string and it shows exactly my string, additionally this script always succeeds and displays "Contact Form Submitted".

现在sendmail.php部分(我相信这里有些问题):

Now the sendmail.php part (I believe something is wrong in here):

// getting variables from form

$emailTo = trim($_POST['address']);
$subject = trim($_POST['title']);;
$name = trim($_POST['name']);
$emailFrom = trim($_POST['mail']);
$message = $_POST['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 '';
}
else{}

有什么想法吗?我知道我不会削减$消息,但是我只发送一个单词的测试邮件.

Any ideas? I know I'm not trimming my $message, I will, but I'm sending just one-word testing mails.

所有变量都来自HTML表单,并以这种方式在jQuery脚本中注册-var address = $(#cmail-address").val(); .无论如何,与我的问题无关,因为我得到了很好的回调.

All variables come from HTML form and are registered in jQuery script this way - var address = $("#cmail-address").val(); . Anyways that has nothing to do with my problem since I'm getting good callback.

顺便说一句,我的服务器允许发送消息和其他脚本正常工作.

And by the way my server allows to send messages and other scripts work just fine.

推荐答案

您的回调函数应该与PHP文件的输出有关.您只是在JavaScript范围内提醒相同的字符串.

Your callback function should have something to do with the output from the PHP file. You are just alerting the same string in your javascript scope.

    success: function(dataOut) {  
        alert(dataOut);    
        jQuery('#ok').html("<h2>Contact Form Submitted!</h2>")
}  

当然,您需要PHP来回显某些内容.可能像已发送"或未发送"一样简单,直到以后对内容进行细化为止,也可能恰好是您要显示的消息.在尝试发送邮件之前,还应回显SOMETHING.它可以帮助您确定问题出在实际发送邮件而不是您对ajax的使用上.

Of course, you need the PHP to echo something. Could be as simple as "sent" or "not sent" until you refine stuff later, or it could be exactly the message you want to display. Also echo SOMETHING before you attempt to send the mail. It could help identify that the problem is with actually sending the mail and not your use of ajax.

正如一个评论者所提到的那样,诸如?a = 1& b = 2& c = 3"之类的字符串可能适用于POST,但名称/值对{a:"cat",b:"dog"}可能是更好的方法.像这样的字符串就是GET的样式,这可能就是为什么其他人混淆了他们答案的原因.

As one commenter mentioned - strings like "?a=1&b=2&c=3" might work for POST, but name/value pairs {a: "cat", b: "dog"} is probably the better way to go. Strings like that are GET's style, which is probably why someone else confused their answer.

我还建议使用jQuery的post函数. $ .post和$ .get更简单,可以覆盖90%的典型Ajax使用(包括您的使用!). $ .ajax在特殊情况下非常有用

I'd also recommend jQuery's post function. $.post and $.get are simpler and cover 90% of typical ajax uses (including yours!). $.ajax is pretty much for oddball special cases

$.post("sendmail.php", 
       {address: address, title: title, name: name...}, 
       function(d) {alert(d); $("#ok").html("<h2>" + d + "</h2>");});

这最后几件事是建议,它们不一定是导致您出现问题的原因,但可能会更容易找到该原因.

These last few things are recommendations, they are not likely the cause of your problem, but might make it easier to find that cause.

什么是行不通的?您的PHP已到达,但电子邮件根本没有发送?

What IS it that doesn't work? Your PHP is reached but the email doesn't send at all?

这篇关于我的AJAX联系表格有什么问题? (php mail())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆