【问题标题】:how to send a body from a textarea from a .js page to a .php page如何将文本区域中的正文从 .js 页面发送到 .php 页面
【发布时间】:2013-06-26 16:38:42
【问题描述】:

我一直在查看 StackOverflow 和其他论坛网站,但我仍然不知道如何完成我正在做的事情。

我想在我正在开发的网站中发送个性化的电子邮件(可以是文本或 html)。

我正在使用 ajax 发送邮件列表、主题和正文。代码是这样的

这是.js

    function loadsentmail(){

var subject = document.getElementById("subject_title").value;
var content = tinyMCE.get('content').getContent();
var from = "somemail@mail.com";
var body = new Array();
body[0]=content;
$.ajax({
type:'POST',
url:'sendmail.php?mails='+mails+'&names='+names+'&idgroup='+idGrupo+'&subject='+subject+'&body='+body+'&from='+from+'&flag=1',
data: {},
            success:function(result){

                alert("Mail sent correctly");
                },
            error:function(){
                alert("Mail was not sent");
            }
});
}

这是.php

$to = $_GET['mails'];
$names = $_GET['names'];
$subject=$_GET['subject'];
$from = $_GET['from'];
$body = $_GET['body'];
$flag=$_GET['flag'];
$groupId=$_GET['idgroup'];
echo $flag;
$headers = "MIME-Version: 1.0\r\n"; 
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n"; 
$headers .= "From: Some Company <somemail@somemail.com>\r\n"; 
$headers .= "Reply-To: somemail@somemail.com\r\n"; 
$headers .= "Return-path: somemail@somemail.com\r\n"; 
$headers .= "Cc: somemail@somemail.com\r\n"; 
$headers .= "Bcc: somemail@somemail.com\r\n";
switch($flag)
{
case 1:
    if (mail($to, $subject,$body,$headers)) {
        echo("<p>Message successfully sent!</p>");
} 
    else {
echo("<p>Message delivery failed...</p>");
    }
break;
} 

到目前为止一切顺利,我用小体测试,它工作,但是一旦我粘贴一个大的 html 文件,我得到以下错误

<h1>Request-URI Too Large</h1>
<p>The requested URL's length exceeds the capacity
limit for this server.<br />
</p>

将大正文从 .js 发送到 .php 的最佳做法是什么?我在整个互联网上搜索了很多,但我仍然可以找到答案。请帮帮我:S

【问题讨论】:

  • 不是在 url 字符串上发送数据,而是按照预期将其作为数据属性中的对象发送。然后将其设置为发送 Post 而不是获取。
  • 此外,查询字符串可以存储的数据量受到更多限制,因此数据被截断。切换到按照第一条评论中的建议发布数据。
  • 你能附上一个简单的例子吗?我很抱歉,但我是一个非常初学者的程序员。我已经以我的方式学到了所有我所知道的,所以我会很感激你的一点推动呵呵

标签: php javascript ajax email textarea


【解决方案1】:
$.ajax({
 type:'POST',
 url:'sendmail.php',
 data: {
  mails: mails,
  names: names,
  idgroup: idGrupo,
  subject: subject,
  body: body,
  from: from,
  flag:1
 },
 type: "POST",
 success:function(result){

    alert("Mail sent correctly");
 },
 error:function(){
    alert("Mail was not sent");
  }
});

Post 将允许更大的数据集,并且使用 jquery ajax 即使您使用 GET,您也应该使用数据对象发送数据。

我也相信(我是 perl 而不是 php 开发人员,因此您需要查看此内容)您需要更改 php 以从 Post obj 中获取。 $to = $_GET['mails']; 将变为 $to = $_POST['mails']

【讨论】:

  • 好的,我试过了,它成功了!它现在正确发送数据。但是我不确定 .php 是否正确接收数据。我必须使用 $_POST 而不是 $_GET 来访问这些元素?
  • 哈哈我没看到最后一部分!对不起!它现在完美运行。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-01
相关资源
最近更新 更多