簡體   English   中英

simpleCart.js 使用 PHP 腳本將表單輸入和購物車項目發送到 EMAIL。 - 無法發送表單輸入

[英]simpleCart.js send form input and cart item to EMAIL using PHP script. - cannot sent form input

購物車項目被發送到電子郵件,但不是來自表單的輸入。

這是我的代碼:

HTML:

 <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<title>Atelier K - Trimite Comanda</title>
<link rel="icon" href="images/iconAK.ico"/>

<!-- Bootstrap -->
<link rel="stylesheet" href="css/bootstrap.css">
<script src="js/simpleCart.js"></script>

<script src="js/simplecart.min.js"></script>

<script src="js/qunit.js"></script>  
<script src="js/jquery.min.js"></script>
<script src="js/bootstrap.js"></script>

    <script>

    simpleCart({

cartColumns: [
//{ attr: "image", label: false, view: "Imagine"},

{ attr: "name" , label: "Produs" },

{ attr: "quantity", label: "CAntitate", view: "input"},


        { view:'remove', text: "X", label: "Sterge"},

        //Price of item
        //{ attr: "price", label: "Pret", view: "currency"},
        //Subtotal of that row (quantity of that item * the price)
        { attr: "total" , label: "Sub Toalt", view: "currency"  }
    ],
    cartStyle: "table" ,
    checkout: {
    type: "SendForm" ,
    url: "/contact/Comandascript.php" ,
    method: "POST" ,
    success: "/contact/Comanda_trimisa.php" ,
    cancel: "/contact/Comanda_netrimisa.php" ,
    extra_data: {
     Prenume : document.getElementById("firstname").value,
Nume : document.getElementById("lastname").value,
            Address : document.getElementById("address").value,
        email : document.getElementById("email").value,
        Telefon : document.getElementById("phone").value,
        Mesaj : document.getElementById("comments").value,

}

}
});

simpleCart.bind( 'beforeCheckout' , function( data ){
data.first_name = document.getElementById("firstname").value;
data.last_name = document.getElementById("lastname").value;
data.address = document.getElementById("address").value;
data.email = document.getElementById("email").value;
data.phone = document.getElementById("phone").value;
data.comments = document.getElementById("comments").value;
});
    </script>
</head>
<body>
<section>
<div class="container">    
<!-- Form -->
<form name="myform" id="myForm" method="POST" action="/contact/Comandascript.php">
        <fieldset> 
        <h4 style="color: #4682B4"> Persoana de contact</h4>
            <p>&nbsp;</p>
           <table>
 <tr>
        <th> <p>   <label for="firstname">Prenume:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </label>    </th>  
        <th><input type="text" name="firstname" placeholder="" title="Introduceti prenumele" class="required" id="firstname" size="50px" onblur='isEmpty(this.value)' ><font size="1" color="red"> <span id="msg"></span></font></p></th>
</tr>   
 <tr>   
     <th> <p>   <label for="lastname">Nume:&nbsp; </label>    </th>  
        <th><input type="text" name="lastname" placeholder="" title="Introduceti numele de familie" class="required" id="lastname" size="50px" onblur='isEmpty1(this.value)' > <font size="1" color="red"><span id="msg1"></span></font></p></th>
    </tr>   
    </table>
    <hr>
    <h4 style="color: #4682B4">Date de Livrare</h4>
        <p>&nbsp;</p>
    <table>
         <tr>
    <th>    <p>   <label for="address">Adresa de livrare:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </label> </th>  
    <th>     <input type="text"  name="address" placeholder="" title="Introduceti adresa de livrare" class="required" id="address" size="50px" onblur='isEmpty2(this.value)' ><font size="1" color="red"><span id="msg2"></span></font></p> </th>  
         </tr>  
          <tr>
     <th>   <p>   <label for="email">Email:&nbsp; </label> </th>  
    <th>     <input type="email" name="email" placeholder="" title="Introduceti o adresa de email corecta" class="required email" id="email" size="50px" onblur='isEmpty3(this.value)'><font size="1" color="red"><span id="msg3"></span></font></p> </th>  
         </tr>  
          <tr>
     <th>   <p>   <label for="phone">Telefon:&nbsp; </label> </th>  
    <th>    <input type="tel" name="phone" placeholder="" id="phone" size="50px" title="Introduceti numarul dvs. de telefon." onblur='isEmpty4(this.value)' onkeypress='return isNumberKey(event)'><font size="1" color="red"><span id="msg4"></span></font></p> </th>  
        </tr>   
        </table>
        <hr>
        <h4 style="color: #4682B4"> Informatii Suplimentare</h4>
            <p>&nbsp;</p>
        <table>
         <tr>
      <th>  <p>   <label for="message">Mesaj:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; </label> </th>  
     <th> <input type="text" title="Completati aici daca doriti sa ne transmiteti info extra" name="message" placeholder="Completati aici daca doriti sa ne transmiteti info. extra." id="message" size="75px"></p></th>  

</tr>   
</table>

   <hr>
        </fieldset>  
   <a href="javascript:;"  id="submit" name="submit" value="submit" class="simpleCart_checkout" ><span class="checkout_btn"> Trimite comanda</span></a>

        </form>
        </div> <!-- /end #contact-form -->       
 </div>
</section>
<hr>
</body>
</html>

PHP代碼

<?php
$content = $_POST; /* receiving SimpleCart order array */
$body = ''; /* declaring the email body */

$firstname = ''; /* extra field variable */
$lastname = ''; /* extra field variable */
$email = ''; /* extra field variable */
$address = '';
$phone = ''; /* extra field variable */
$comments = ''; /* extra field variable */

$body .= '=================================='."\n";
$body .= "Prenume: ".$content[$firstname]."\n"; /* using extra field variable */
$body .= "Nume de familie: ".$content[$lastname]."\n"; /* using extra field variable */
$body .= "Adresa: ".$content[$address]."\n"; /* using extra field variable */
$body .= "Email: ".$content[$email]."\n"; /* using extra field variable */
$body .= "Numar de telefon: ".$content[$phone]."\n"; /* using extra field variable */
$body .= 'Has placed the following order:'."\n";
$body .= "\n";
$body .= '=================================='."\n";
/* starting the loop to get all orders from the stored array */
for($i=1; $i < $content['itemCount'] + 1; $i++) {
$name = 'item_name_'.$i; /* product name variable */
$quantity = 'item_quantity_'.$i; /* product quantity variable */
$price = 'item_price_'.$i; /* product price variable */
$total = $content[$quantity]*$content[$price]; /* product total price variable (price*quantity) */
$grandTotal += $total; /* accumulating the total of all items */
$body .= 'Comanda#'.$i.': '.$content[$name]."\n".'Cantitate '.$content[$quantity].' --- Pret per bucata $'.number_format($content[$price], 2, '.', '')."\n".'Sub-total $'.number_format($total, 2, '.', '')."\n"; /* creating a semantic format for each ordered product */
$body .= '=================================='."\n";
}
/* ending the loop to get all orders from the stored array */
$body .= 'Total: $'.number_format($grandTotal, 2, '.', '')."\n"; /* total amount of the order */
$body .= '=================================='."\n";
$body .= "\n";
$body .= "Mesaj(optional): ".$content[$comments]."\n"; /* using extra field variable */
    $headers    = "Content-Type: text/plain; charset=iso-8859-1\n";
    $headers    .= "From: $firstname $lastname  <$email>\n";
    $recipient  = "myemail@yahoo.com";
    $subject    = "Comanda noua pentru K";
mail($recipient, $subject, $body, $headers); /* building the mail() function */
header("location: /contact/Comanda_trimisa.php"); /* declaring the page to redirect if the mail is sent successfully */
?>

我嘗試了使用 simpleCart.js 發現的有關 SendForm 的不同示例,但它根本不起作用。 如果我從提交鏈接中取出 class="simpleCart_checkout" 那么我就得不到購物車商品。 如果我在沒有類的輸入標簽 type="submit" 中打開它,那么我會得到表單輸入。 我怎樣才能讓兩者都使用相同的 php 腳本來發送電子郵件?

我在其他地方找到了幫助,但如果有人對這里的解決方案感興趣,那就是:

從我上面發布的 HTML 代碼中刪除這部分:

extra_data: {
     Prenume : document.getElementById("firstname").value,
Nume : document.getElementById("lastname").value,
            Address : document.getElementById("address").value,
        email : document.getElementById("email").value,
        Telefon : document.getElementById("phone").value,
        Mesaj : document.getElementById("comments").value,

}

也在 PHP 腳本中改變這個

$headers    .= "From: $firstname $lastname  <$email>\n";

對此:

$headers    .= "From: ".$content[$firstname]." ".$content[$lastname]."  <".$content[$email].">\n";

現在一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM