1

我有一个网站,我们将提供一些产品供会员购买。请参阅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="prices" 跨度标签之间。

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

然后在提交时,我需要它向指定的电子邮件地址发送一封电子邮件,说..“数量”x“项目名称”=“数量”

(例如,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\">";
}

?>​

我自己也尝试过。但这不是我的强项,希望我能寻求一些专家的建议。提前致谢!!

4

1 回答 1

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\">";
    }

?>​
于 2012-06-25T10:12:55.867 回答