delux
delux

Reputation: 1886

Send JavaScript object array via Email

I have the layout of code (attached in this post) so far. In my JavaScript I have different operations and at the end as a result I got array of objects called MyObjects.

The objects have the properties Name and Phone, so the final result have this format:

  MyObjects[0].Name    
  MyObjects[0].Phone    
  MyObjects[1].Name    
  MyObjects[1].Phone 

Now what I need to do is to send this information via email on some email address. This code is situated on PHP script, on the server. So how is it possible to send this data (the array of objects) via email ? Will I need to convert it to PHP format somehow or to send it somehow via ajax to some PHP script ?

<script type="text/javascript">
    $(document).ready(function() {                
       $.ajax({
          url : "some_URL",
          type: 'post',
          dataType : 'json',   // use jsonp for cross origin request
          data: JSON.stringify(jsonData),
          success : function(data){
             //MY CODE FOR AJAX SUCCESS
                 ...
                 ...
                 ...
                 console.log(MyObjects);
                 //MyObjects[0].Name
                 //MyObjects[0].Phone
                 //MyObjects[1].Name
                 //MyObjects[1].Phone                   

           }, error : function(err){
                 console.log('error');
                 //MY CODE FOR AJAX FAILURE
           }
       });
   });    
</script>

I'm really confused because for the first time I am in situation when I need to send some data that is in format of JavaScript objects.

Upvotes: 0

Views: 1247

Answers (1)

benaich
benaich

Reputation: 922

 MyObjects[0].Name = 'ben';  
 MyObjects[0].Phone = '0666'
 MyObjects[1].Name = 'med';  
 MyObjects[1].Phone = '0999';
// the output of JSON.stringify [{"Name":"ben","Phone":"0666"},{"Name":"med","Phone":"0999"}]"

$.ajax({
   type: "POST",
   data: {mydata: JSON.stringify(MyObjects)}, 
   url: "index.php",
   success: function(data){
   }
});

Then in PHP:

$data = $_POST['mydata'];

NB: the dataType option is for parsing the received data only.

Upvotes: 4

Related Questions