javascript - simpleCart.js send form input and cart item to EMAIL using PHP script. - cannot sent form input -
the cart items sent email not input form.
here code:
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 row (quantity of item * 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> </p> <table> <tr> <th> <p> <label for="firstname">prenume: </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: </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> </p> <table> <tr> <th> <p> <label for="address">adresa de livrare: </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: </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: </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> </p> <table> <tr> <th> <p> <label for="message">mesaj: </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 code
<?php $content = $_post; /* receiving simplecart order array */ $body = ''; /* declaring email body */ $firstname = ''; /* field variable */ $lastname = ''; /* field variable */ $email = ''; /* field variable */ $address = ''; $phone = ''; /* field variable */ $comments = ''; /* field variable */ $body .= '=================================='."\n"; $body .= "prenume: ".$content[$firstname]."\n"; /* using field variable */ $body .= "nume de familie: ".$content[$lastname]."\n"; /* using field variable */ $body .= "adresa: ".$content[$address]."\n"; /* using field variable */ $body .= "email: ".$content[$email]."\n"; /* using field variable */ $body .= "numar de telefon: ".$content[$phone]."\n"; /* using field variable */ $body .= 'has placed following order:'."\n"; $body .= "\n"; $body .= '=================================='."\n"; /* starting loop orders 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 total of 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 semantic format each ordered product */ $body .= '=================================='."\n"; } /* ending loop orders stored array */ $body .= 'total: $'.number_format($grandtotal, 2, '.', '')."\n"; /* total amount of order */ $body .= '=================================='."\n"; $body .= "\n"; $body .= "mesaj(optional): ".$content[$comments]."\n"; /* using 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 mail() function */ header("location: /contact/comanda_trimisa.php"); /* declaring page redirect if mail sent */ ?>
i tried different examples found sendform simplecart.js dos not work. if take out class="simplecart_checkout" submit link don't cart items . if turn in input tag type="submit" no class, form input. how can both using same php script send emails?
i found someplace else, in case interested in solution here is:
from html code posted above remove part:
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, }
also in php script change this
$headers .= "from: $firstname $lastname <$email>\n";
to this:
$headers .= "from: ".$content[$firstname]." ".$content[$lastname]." <".$content[$email].">\n";
now works correctly.
Comments
Post a Comment