2017-08-26 81 views
0

我有html输入和js数组对象。对象数组与数组不相关。他们是独立的单位。如何发送带有表单值的对象的js数组通过Ajax发送到电子邮件

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 = "[email protected]"; 
    $message = ' 
      <html> 
       <head> 
       </head> 
       <body> 
        <p>Name: '.$_POST['first name'].'</p> 
        <p>Phone: '.$_POST['phone number'].'</p> 
        $someArray; 
        foreach ($extradata as $key => $value) { 
        $someArray .= "<p>".$value["image"] . ", " . 
        $value["name"] . "</p>"; 
        </body> 
      </html>'; 

你能检查,请天气这段代码是正确还是不正确?

+1

你到目前为止尝试过什么? – Dekel

+0

你如何得到这个js数组。您可以使用formData发送 – C2486

+0

我有通过添加项目到购物车形成的购物车数组。然后我得到订单,当我提交时,我想收到所有的电子邮件数据。 –

回答

1
 <form id="formoid" action="" method="post"> 
       <input type="text" id="firstname" name="firstname" > 
       <input type="text" id="lphonenumber" name="lphonenumber" > 
       <input type="submit" id="submitButton" name="submitButton" value="Submit"> 
    </form> 
you could do something like this 

    <script type='text/javascript'> 
     /* attach a submit handler to the form */ 
     $("#formoid").submit(function(event) { 

     var array = [{count:1,image:"images/1.jpg",name:"Bouquet 1",price:49},{count:5,image:"images/1.jpg",name:"Bouquet 9",price:77}]; 
      /* stop form from submitting normally */ 
      event.preventDefault(); 

      /* get the action attribute from the <form action=""> element */ 
      var $form = $(this), 
       url = $form.attr('action'); 

      /* Send the data using post with element id firstname, lphonenumber along with your array*/ 
      var posting = $.post(url, { name: $('#firstname').val(), name2: $('#lphonenumber').val(),extradata: array }); 

      /* Alerts the results */ 
      posting.done(function(data) { 
      alert('success'); 
      }); 
     }); 
    </script> 

and in php you could do something like, 

    <?php 
    if(isset($_POST['name'])){ 
    // To send HTML mail, the Content-type header must be set 
    $headers = 'MIME-Version: 1.0' . "\r\n"; 
    $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n"; 
    // Convert JSON string to Array 
    $extradata = json_decode($_POST['extradata'], true); 
    // print_r($someArray);  // Dump all data of the Array 
    // Loop through Array if necessary 
    $someArray; 
    foreach ($extradata as $key => $value) { 
    $someArray .= "<p>".$value["image"] . ", " . $value["name"] . "</p>"; 
    } 

    $to = "[email protected]"; 
     $message = ' 
       <html> 
        <head> 
        </head> 
        <body> 
         <p>Name: '.$_POST['first name'].'</p> 
         <p>Phone: '.$_POST['phone number'].'</p>'.$someArray.' 
         </body> 
       </html>'; 
    $subject = "Wooo Email!"; 
    mail($to, $subject, $message, "From: [email protected]\r\n", $headers); 
    } else { 
     echo "error"; 
    }?> 

or you could use phpmailer or swift mailer for the same 
+0

非常感谢,但是如何将这些数据发送至电子邮件? –

+0

darshan dj,你能解释一下,我应该怎么把php文件extradata与数组放在一起。发送邮件到 –

+0

编辑(解析extradata) –