【问题标题】:Sending js multiple data via Ajax to email通过ajax发送js多条数据到email
【发布时间】:2017-09-02 08:12:51
【问题描述】:

我需要将多个数据作为数组发送,并将输入值通过 Ajax 发送到电子邮件。这段代码有什么问题?数组不返回电子邮件,猜测 php 文件有问题 ***html:

<form id="form-order">
  First name:<br>
  <input type="text" name="firstname">
  <input type="tel" name="phonenumber">
  <input type="submit" value="Submit">
</form>

***js:

 $.ajax({
   type: "POST",
   data: {mydata: JSON.stringify(MyObjects)}, 
   url: "index.php",
   success: function(data){
   }
});
    var array = [{count:1,image:"images/1.jpg",name:"Bouquet 1",price:49},{count:5,image:"images/1.jpg",name:"Bouquet 9",price:77}];
            $("#form-order").submit(function() {

                        var order_data = cart;
                        $.ajax({
                        type: "POST", 
                        url: "../order.php", 
                        data: {form: form_data, 
                        order:JSON.stringify(order_data)},
                        success: function() {                         
                        console.log('OK');
                        });
                });

***PHP:

$to = "mail@mail.ru";
$message = '
        <html>
            <head> 
            </head>
            <body>
               <p>Name: '.$_POST['first name'].'</p>
               <p>Phone: '.$_POST['phone number'].'</p>
               $someArray;
               $extradata = json_decode($_POST['order'], true);
               foreach ($extradata as $key => $value) {
               $someArray .= "<p>".$value["image"] . ", " . 
               $value["name"] . "</p>";
              </body>
        </html>';

【问题讨论】:

  • 您必须解码字符串化的内容,否则您将无法遍历发送到 php 脚本的数组。看看 php 中的 json_decode() 函数,你就知道,你要做什么了。
  • 也显示您的其他表单。
  • @rebru 你能看一下吗,我已经在 php.ini 中添加了 json_decode()。是现在吗?谢谢
  • 看看下面的完整答案 - 但请记住,这是唯一的方法。您正在将 $message 混为一谈。

标签: javascript php jquery ajax html-email


【解决方案1】:

对于您的 PHP 代码,您最好将其截断,以便更好地支持编辑和阅读,例如 .... 请注意,这只是一个方向,它不是有效的解决方案。

/* Defintions */
$to = "mail@mail.ru";
$extradata = json_decode($_POST['order'],true);

/* HTML Structure */
$head = "
        <html>
            <head> 
            </head>
            <body>
";
$footer = "
            </body>
        </html>';
";

/* Content */
$content = "";
$content .= "<p>Name:".$_POST['first name']."</p>";
$content .= "<p>Name:".$_POST['phone number']."</p>";
foreach($extradata as $key => $value)
{
    $content .= "<p>".$key."=".$value['image']."</p>";
}

$htmlEmail = $head.$content.$footer;

【讨论】:

    猜你喜欢
    • 2016-03-15
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多