计算物品和数量价格,然后发送电子邮件

时间:2012-06-25 09:11:53

标签: php javascript jquery forms

我有一个网站,我们将为会员提供一些产品。 请参阅http://jsfiddle.net/TbeQx/

<form action="" name="priceCalc" method="POST">
  I Am Paying For :
  <select name="product" onchange="price();">
      <option value="15">item 1 - $15.00</option>
      <option value="35">item 2 - $35.00</option>
      <option value="35">item 3 - $35.00</option>
      <option value="29.90">item 4 - $29.90</option>
      <option value="29.90">item 5 - $29.90</option>
      <option value="26.90">item 6 - $26.90</option>
      <option value="32.90">item 7 - $32.90</option>
      <option value="59.90">item 8 - $59.90</option>
      <option value="59.90">item 9 - $59.90</option>
    </select>
    <br />
    <br />

    Quantity :
  <select name="quantity" onchange="price();">
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
    </select>
    <br />
    <br />
    Please deposit the freight free full total of <br />
    <span style="color:red; font-size:18px;" id="prices">$</span> 
    into our account : 
    <br />
    <input type="submit" name="submit" value="BUY NOW!" />
</form>​

/* JavaScript */
    function price() {
        var qty = document.priceCalc.quantity;
        var itm = document.priceCalc.product;
        var price = parseInt(qty.value) * parseInt(itm.value);
        document.getElementById("prices").innerHTML = '$' + price;
    }​

如何自动计算价格并在两个下拉列表中点击id =“price”span标签之间显示。

这向用户显示总数是多少。

然后在提交时,我需要它发送电子邮件到指定的电子邮件地址说.. “quantity”x“item name”=“qty”

(例如,2 x项目4 - $ 29.90 = $ 59.80)

我的php在这里http://jsfiddle.net/8hzNV/

<?php
$quantity = $_POST["quantity"];
$item = $_POST["product"];
$subject = "Website Sale!";
$emailto = "myemail@test.com";


// prepare email body text
$body .= "";
$body .= "To Sales Team";
$body .= "\n";
$body .= "\n";
$body .= "I have purchased  ";
$body .= $quantity;
$body .= "x  ";
$body .= $item;
$body .= "\n";
$body .= "\n";
$body .= "Please look out for my payment in the account over the next few days.";

// send email 
$success = mail($emailto, $subject, $body");

// redirect to success page 
if ($success){
  print "<meta http-equiv=\"refresh\" content=\"0;URL=../order_sent.php\">";
}
else{
  print "<meta http-equiv=\"refresh\" content=\"0;URL=../index.php\">";
}

?>​
我自己也去做了。但这不是我的强项,我希望能找到一些专家建议。 在此先感谢!!

1 个答案:

答案 0 :(得分:0)

首先,如果将jQuery导入到项目中,为什么不使用jQuery选择器,这更简单?

  • 您应该使用parseFloat代替parseInt
  • 您只是发布Item PriceItem Quantity,如果您想将项目名称附加到电子邮件正文,您也应该发布它。所以你可以用intput [type = hidden]
  • 来做到这一点

这是更新的HTML

<form action="" name="priceCalc" method="POST">
  I Am Paying For :
  <select name="product">
      <option>Select Item</option>
       <option value="15">item 1 - $15.00</option>
       <option value="35">item 2 - $35.00</option>
       <option value="35">item 3 - $35.00</option>
       <option value="29.90">item 4 - $29.90</option>
       <option value="29.90">item 5 - $29.90</option>
       <option value="26.90">item 6 - $26.90</option>
       <option value="32.90">item 7 - $32.90</option>
       <option value="59.90">item 8 - $59.90</option>
       <option value="59.90">item 9 - $59.90</option>
    </select>
    <br />
    <br />

    Quantity :
  <select name="quantity" >
      <option value="1">1</option>
      <option value="2">2</option>
      <option value="3">3</option>
      <option value="4">4</option>
      <option value="5">5</option>
    </select>
    <br />
    <br />
    Please deposit the freight free full total of <br />
    <span style="color:red; font-size:18px;" id="prices">$</span> into our account : 
    <br />
    <input type="hidden" id="itemname" name = "itemname" value= "">
    <input type="submit" name="submit" value="BUY NOW!" />
</form>​

您无需设置内联函数price(),

以下是使用jQuery选择器

更新的JavaScript代码
$(function() {
    $("select[name='product'],select[name='quantity']").change(function() {
        var qty = $("select[name='quantity'] option:selected");
        var itm = $("select[name='product'] option:selected");
        var price = parseFloat(qty.val()) * parseFloat(itm.val());
        $("#prices").html('$' + price);
        $("#itemname").val(itm.html());
    });
});
​

在PHP页面中,你应该从POST数组中获取$ itemname,

PHP页面应该是这样的,

<?php
    $quantity = $_POST["quantity"];
    $item = $_POST["product"];
    $itemname = $_POST["itemname"];
    $subject = "Website Sale!";
    $emailto = "myemail@test.com";


    // prepare email body text
    $body .= "";
    $body .= "To Sales Team";
    $body .= "\n";
    $body .= "\n";
    $body .= "I have purchased  ";
    $body .= $quantity;
    $body .= "x  ";
    $body .= $itemname;
    $body .= " = ";
    $body .= $item * $quantity;
    $body .= "\n";
    $body .= "\n";
    $body .= "Please look out for my payment in the account over the next few days.";

    // send email 
    $success = mail($emailto, $subject, $body");

    // redirect to success page 
    if ($success){
      print "<meta http-equiv=\"refresh\" content=\"0;URL=../order_sent.php\">";
    }
    else{
      print "<meta http-equiv=\"refresh\" content=\"0;URL=../index.php\">";
    }

?>​