javascript购物车电子邮件结帐

时间:2019-07-09 16:17:02

标签: javascript php html css

谢谢您的协助。 我刚刚开始学习HTML / JAVASCRIPT / CSS。

我有一个小的任务/项目要完成。需要在前店存放一些物品(3或4)。必须创建一个JavaScript购物车。单击购买按钮时,将项目添加到购物车。然后即可查看购物车。必须有一个结帐选项,但是不进行付款处理。该订单需要通过电子邮件发送给我和用户,以便人工付款。

我如何才能将购物车中的物品通过电子邮件发送?

谢谢您的帮助

Javascript归功于Burlaka Dmytro

这是链接,这是我正在使用的链接。 https://codepen.io/Dimasion/pen/oBoqBM

<!-- Nav -->
<nav class="navbar navbar-inverse bg-inverse fixed-top bg-faded">
 <div class="row">
     <div class="col">
       <button type="button" class="btn btn-primary" data-toggle="modal" 
        data-target="#cart">Cart (<span class="total-count"></span>) 
       </button><button class="clear-cart btn btn-danger">Clear 
                Cart</button></div>
     </div>
</nav>


<!-- Main -->
<div class="container">
 <div class="row">
  <div class="col">
    <div class="card" style="width: 20rem;">
      <img class="card-img-top" 
      src="http://www.azspagirls.com/files/2010/09/orange.jpg" alt="Card 
      image cap">
      <div class="card-block">
      <h4 class="card-title">Orange</h4>
      <p class="card-text">Price: $0.5</p>
      <a href="#" data-name="Orange" data-price="0.5" class="add-to-cart 
      btn btn-primary">Add to cart</a>
    </div>
  </div>
  </div>
  <div class="col">
    <div class="card" style="width: 20rem;">
    <img class="card-img-top" src="http://images.all-free- 
    download.com/images/graphicthumb/vector_illustration_of_ripe_
    bananas_567893.jpg" alt="Card image cap">
   <div class="card-block">
    <h4 class="card-title">Banana</h4>
     <p class="card-text">Price: $1.22</p>
     <a href="#" data-name="Banana" data-price="1.22" class="add-to-cart 
     btn btn-primary">Add to cart</a>
   </div>
 </div>
  </div>
  <div class="col">
    <div class="card" style="width: 20rem;">
  <img class="card-img-top" src="https://3.imimg.com/data3/IC/JO/MY- 
   9839190/organic-lemon-250x250.jpg" alt="Card image cap">
  <div class="card-block">
  <h4 class="card-title">Lemon</h4>
  <p class="card-text">Price: $5</p>
  <a href="#" data-name="Lemon" data-price="5" class="add-to-cart btn 
 btn-primary">Add to cart</a>
 </div>
 </div>
  </div>
  </div>
  </div>


  <!-- Modal -->
  <div class="modal fade" id="cart" tabindex="-1" role="dialog" aria- 
   labelledby="exampleModalLabel" aria-hidden="true">
   <div class="modal-dialog modal-lg" role="document">
   <div class="modal-content">
    <div class="modal-header">
     <h5 class="modal-title" id="exampleModalLabel">Cart</h5>
    <button type="button" class="close" data-dismiss="modal" aria- 
   label="Close">
      <span aria-hidden="true">&times;</span>
    </button>
  </div>
  <div class="modal-body">
    <table class="show-cart table">

    </table>
    <div>Total price: $<span class="total-cart"></span></div>
  </div>
  <div class="modal-footer">
    <button type="button" class="btn btn-secondary" data- 
dismiss="modal">Close</button>
    <button type="button" class="btn btn-primary">Order now</button>
  </div>
  </div>
 </div>
 </div> 

我无法通过电子邮件发送购物车内容。

***********************************************
This is my PHP
*********************************************
<?php
    $to      = 'my email address is here';
    $subject = 'SolarPlexus Website';
    $txt = $_POST['content'];

    $headers = 'From: admin@solarplexus.co.za' . "\r\n" .
               'Reply-To: info@solarplexus.co.za' . "\r\n" .
               'X-Mailer: PHP/' . phpversion();
    mail($to, $subject, $txt,  $headers);
    Header('Location: thankyou.html');
?>


*******************************
This is html part for the cart, the cart is hidden until cart button is 
clicked. making use of bootstrap modal.
*******************************

 <div class="modal fade" id="cart" tabindex="-1" role="dialog" aria- 
  labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
       <h5 class="modal-title" id="exampleModalLabel">Cart</h5>
       <button type="button" class="close" data-dismiss="modal" aria- 
        label="Close">
      <span aria-hidden="true">&times;</span>
      </button>
    </div>
     <form name="contactform" method="post" action="sendjs.php">    
     <div class="modal-body">

     <input type="text" required="required" placeholder="Enter Name" 
     name="name1"> 
     <input type="email" required="required" placeholder="Enter Email 
     address" name="email1">
     <input type="tel" required="required" placeholder="Enter Telephone 
     number" name="tel1">


     <table id="cartents" class="show-cart table">

     </table>
    <div>Total price: R<span class="total-cart"></span></div>
    </div>
    <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data- 
    dismiss="modal">Close</button>
        <button onClick="SendMail()" type="button submit" class="btn btn- 
     primary">Order now</button>

    </div>
  </div>
  </div>
 </div>
</form> 


*************
 the Javascript file remains unchanged from the codepen link provided

1 个答案:

答案 0 :(得分:1)

您将需要在表内容中添加一个ID: HTML

<div>Total price: $<span class="total-cart" id="cartContent"></span></div>

为按钮创建函数:

<button onclick="SendMail()"type="button" class="btn btn-primary">Order now</button>

然后创建函数SendMail();在脚本中

<script>
 function SendMail()    
     {    
       var tableContent = document.getElementById("cartContent").innerHTML;    
       $.post('mail.php',{content:tablecontent},function(data) {
    });
     }
</script>

您将需要创建一个mail.php文件

<?php
$to = "somebody@example.com";
$subject = "ur subject";
$txt = $_POST['content'];
$headers = "From: webmaster@example.com" . "\r\n" .
"CC: somebodyelse@example.com";

mail($to,$subject,$txt,$headers);
?>