0

I'm using a javascript shopping cart on a store, and I want to send an order confirmation on checkout. The problem is that the cart isn't saved in database of any kind, and is printed with javascript. How would I attach it to the email? I've included the shopping cart script on the page that sends the mail.

<table class="simpleCart_items"></table> would print the cart, but how would I attach the printed cart to email?

Hidden input or something?

UPDATE

My ajax call looks like this:

var data = $('#yhteystiedot').serialize();
      data.cartContent = $('.simpleCart_items').html(); 
      //alert (dataString);return false;
     $.ajax({
    type: "POST",
    url: "order.php",
    data: data,
    dataType: "text",
    error: function(){ alert("Jotakin meni pahasti pieleen! Yritä uudelleen?");
 },
    success: function() {
 $(document).html("Tilaus lähti.");
        }


  });
1
  • populating hidden field every time cart changes would be a good and simple option Commented Sep 5, 2012 at 12:16

3 Answers 3

2

You can make an ajax call to a php function that sends an email. The argument is the content generated by javascript.

Sign up to request clarification or add additional context in comments.

1 Comment

Yeah, but what I should do? How would I send the output generated by js to PHP?
1

You'll need to post the cart values to serverside PHP script and recreate the HTML for the cart in order to be able to send it through email. You can do direct form post or ajax post based on your need.

2 Comments

I'm sending the form with jquery's ajax function, so what should I do?
First you'll need to retrieve the posted values ($_POST['']) from PHP script. Once you have the values, you can prepare the mail content and send it to the user.
0

I asume your $.ajax() call looks something like this:

$('form').submit(function(){
    var dataTrunk = $(this).serializeArray();
    dataTrunk.push( { name: 'cartContent', value: $(your_table_selector).html()});
    $.ajax({
        url: 'mail.php', // your mail script
        data: dataTrunk,
        type: 'post'
    });
    return false;
});

In php you would trap $_POST['cartContent'] and render it in email and send it.

If you are sending email with html and plain text body, then it would probably be a good idea to strip html elements and replace them with chars that are compatible with plain text.

// edited: I've fixed the error

5 Comments

I have error in my code.... $.serialize() returns string... I'll fix that promptly
Quite same, only the dataTrunk.cartContent part is new.
instead of $.serialize() should be $.serializeArray() - fixed
@ChristianNikkanen yeah, the dataTrunk.cartContent wont work since dataTrunk is string not object... I have now improved my code
Aand it works like magic. Now I'll just have to style the cart.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.